A program’s control flow is the order in which the program’s code executes.
The control flow of a Python program is regulated by conditional statements, loops, and function calls.
Python has three types of control structures:
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.
## This is a Sequential statementa=20b=10c=a-bprint("Subtraction is : ",c)
In Python, the selection statements are also known as Decision control statements or branching statements.
The selection statement allows a program to test several conditions and execute instructions based on which condition is true.
Some decision control statements are:
if
if-else
nested if
if-elif-else
if
It help us to run a particular code, but only when a certain condition is met or satisfied. A if
only has one condition to check.
In the following code example, we're checking the if
condition, e.g., if n
modulo 2
equals to 0
then the print
statement will execute.
n = 10if n % 2 == 0:print("n is an even number")
if-else
The if-else
statement evaluates the condition and will execute the body of if
if the test condition is True
, but if the condition is False
, then the body of else
is executed.
In the following code example, the else
body will execute as 5
modulo 2
is not equal to 0
.
n = 5if n % 2 == 0:print("n is even")else:print("n is odd")
if
Nested if
statements are an if
statement inside another if
statement.
In the following code example, we can see first if
condition checks a
is greater than b
. If yes, then we've another if
condition that checks a
is also greater than c
. If yes, then if
body will be executed.
a = 20b = 10c = 15if a > b:if a > c:print("a value is big")else:print("c value is big")elif b > c:print("b value is big")else:print("c is big")
if-elif-else
The if-elif-else
statement is used to conditionally execute a statement or a block of statements.
In the code provided, the initial if
statement checks whether 15 is equal to 12. If not, the elif
condition will verify that 15 is greater than 12. If this condition is true, the code block inside elif
will be executed. Otherwise, the code in the else
block will be executed if none of the preceding conditions are met.
x = 15y = 12if x == y:print("Both are Equal")elif x > y:print("x is greater than y")else:print("x is smaller than y")
If you're eager to deepen your understanding of Python and sharpen your problem-solving skills, our Learn to Code: Become a Software Engineer path is the perfect learning resource. With this structured path, you'll go beyond fundamental concepts like generators and dive into advanced topics that will prepare you for a successful career in software engineering.
A repetition statement is used to repeat a group(block) of programming instructions.
In Python, we generally have two loops/repetitive statements:
for
loopwhile
loopfor
loopA for
loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set. We can execute a set of statements once for each item in a list, tuple, or dictionary.
In the following code example, the first example demonstrates how to use a for loop to iterate over a list and access its elements, while the second example shows how to iterate over a range of numbers.
print("1st example")lst = [1, 2, 3]for i in range(len(lst)):print(lst[i], end = " \n")print("2nd example")for j in range(0,5):print(j, end = " \n")
while
loopIn Python, while
loops are used to execute a block of statements repeatedly until a given condition is satisfied. Then, the expression is checked again and, if it is still true, the body is executed again. This continues until the expression becomes false.
The following code iterates from 0 to 4 and prints each value using a while
loop. It prints End
to signify the end of the program after the loop is completed.
m = 5i = 0while i < m:print(i, end = " ")i = i + 1print("End")