Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Control structures in python

Rating
-
Sold
-
Pages
55
Uploaded on
03-07-2025
Written in
2024/2025

This document helps to learn the basic controls structures of python. It explains the types of control statements, conditional statements and looping statements in a simple manner with programming examples

Institution
Course

Content preview

UNIT II CONTROL STRUCTURES AND FUNCTIONS 9


Selective statements – Iterative statements - Function: definition, call, return
statement,parameters and arguments types (required, keyword, default, variable length),
local and global scope, function composition, recursion, lambda functions. Illustrative
programs: square root, gcd, sum an array of numbers, linear search, binary search.


2.1 INTRODUCTION :
Control structures are essential in Python programming as they direct the flow of
execution in a program. Without them, code would run line by line without any decision-making
capability, meaning that no choices or repetitive tasks could occur.

• Control structures enable programmers to manage how their code behaves based on
specific conditions.

• For example, an if statement allows the program to execute certain actions only if a
condition is true. This ability to make decisions enhances the flexibility and functionality
of programs.

• Additionally, control structures contribute to making code easier to read and maintain.
Ultimately, control structures in Python streamline the process of making decisions during
program execution, making them indispensable tools for developers.


Types of Control Structures :
Control flow refers to the sequence a program will follow during its execution.
Conditions, loops, and calling functions significantly influence how a Python program is
controlled.
There are three types of control structures in Python:
• Sequential - The default working of a program
• Selective - This structure is used for making decisions by checking conditions and
branching
• Iterative/ Repetition - This structure is used for looping, i.e., repeatedly executing a
certain piece of a code block.
Sequential :
Sequential statements are a set of statements whose execution process happens in a
sequence. The problem with sequential statements is that if the logic has broken in any one of the
lines, then the complete source code execution will break.



Code

,# Python program to show how a sequential control structure works

# We will initialize some variables
# Then operations will be done
# And, at last, results will be printed
# Execution flow will be the same as the code is written, and there is no hidden flow
a = 20
b = 10
c=a-b
d=a+b
e=a*b
print("The result of the subtraction is: ", c)
print("The result of the addition is: ", d)
print("The result of the multiplication is: ", e)

Output:
The result of the subtraction is: 10
The result of the addition is :30
The result of the multiplication is: 200

Selection/Decision Control Statements
The statements used in selection control structures are also referred to as branching
statements or, as their fundamental role is to make decisions, decision control statements.
A program can test many conditions using these selection statements, and depending on
whether the given condition is true or not, it can execute different code blocks.
There can be many forms of decision control structures. Here are some most commonly used
control structures:
j* If statement
* If..Else statement
* Nested If..else statement
* If...else Ladder.
Conditional (if):
The if statement contains a logical expression using which data is compared and a
decision
is made based on the result of the comparison.
Syntax:
if expression:
statement(s)
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if

,statement is executed. If boolean expression evaluates to FALSE, then the first set of
code after the end of the if statement(s) is executed.
if Statement Flowchart:




Fig: Operation of if statement
Example: Python if Statement
a=3
if a > 2:
print(a, "is greater")
print("done")
a = -1
if a < 0:
print(a, "a is smaller")
print("Finish")
Output:
C:/Users/SAEC/AppData/Local/Programs/Python/Python38-32/pyyy/if1.py
3 is greater
done
-1 a is smaller

, Finish
--------------------------------
a=10
if a>9:
print("A is Greater than 9")
Output:
C:/Users/SAEC/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py
A is Greater than 9
Alternative if (If-Else):
An else statement can be combined with an if statement. An else statement contains the
block of code (false block) that executes if the conditional expression in the if statement
resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else
Statement following if.
Syntax of if - else :
if test expression:
Body of if stmts
else:
Body of else stmts
If - else Flowchart :

Written for

Institution
Course

Document information

Uploaded on
July 3, 2025
Number of pages
55
Written in
2024/2025
Type
SUMMARY

Subjects

$3.59
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
mohanapriyasubramaniyan

Get to know the seller

Seller avatar
mohanapriyasubramaniyan s.a engineering college
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
10 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions