Python Flow Control

Python If Statement explained with examples

Syntax of If statement in Python

The syntax of if statement in Python is pretty simple.

if condition:
   block_of_code

Python – Syntax of if..else statement

if condition:
   block_of_code_1
else:
   block_of_code_2

Syntax of if elif else statement in Python

This way we are checking multiple conditions.
if condition:
   block_of_code_1
elif condition_2:
   block_of_code_2
elif condition_3:
   block_of_code_3
..
..
..
else:
  block_of_code_n

Nested if..else statement example

Here we have a if statement inside another if..else statement block. Nesting control statements makes us to check multiple conditions.

num = -99
if num > 0:
    print("Positive Number")
else:
    print("Negative Number")
    #nested if
    if -99<=num:
        print("Two digit Negative Number")
Output:
Negative Number
Two digit Negative Number

Python for Loop explained with examples

Syntax of For loop in Python

for <variable> in <sequence>:  
   # body_of_loop that has set of statements
   # which requires repeated execution

Here <variable> is a variable that is used for iterating over a <sequence>. On every iteration it takes the next value from <sequence>  until the end of sequence is reached.

Python – For loop example

The following example shows the use of for loop to iterate over a list of numbers. In the body of for loop we are calculating the square of each number present in list and displaying the same.

# Program to print squares of all numbers present in a list

# List of integer numbers
numbers = [1, 2, 4, 6, 11, 20]

# variable to store the square of each num temporary
sq = 0

# iterating over the given list
for val in numbers:
    # calculating square of each number
    sq = val * val
    # displaying the squares
    print(sq)

Function range()

In the above example, we have iterated over a list using for loop. However we can also use a range() function in for loop to iterate over numbers defined by range().

range(n): generates a set of whole numbers starting from 0 to (n-1).

For example:

range(8) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7]

range(start, stop): generates a set of whole numbers starting from start to stop-1.

For example:

range(5, 9) is equivalent to [5, 6, 7, 8]

range(start, stop, step_size): The default step_size is 1 which is why when we didn’t specify the step_size, the numbers generated are having difference of 1. However by specifying step_size we can generate numbers having the difference of step_size.

For example:

range(1, 10, 2) is equivalent to [1, 3, 5, 7, 9]

For loop with else block

Unlike Java, In Python we can have an optional ‘else’ block associated with the loop. The ‘else’ block executes only when the loop has completed all the iterations. Lets take an example:

for val in range(5):
	print(val)
else:
	print("The loop has completed execution")
Output:
0
1
2
3
4

The loop has completed execution

Note: The else block only executes when the loop is finished.

Nested For loop in Python

When a for loop is present inside another for loop then it is called a nested for loop. Lets take an example of nested for loop.

for num1 in range(3):
	for num2 in range(10, 14):
		print(num1, ",", num2)
Output:
0 , 10
0 , 11
0 , 12
0 , 13
1 , 10
1 , 11
1 , 12
1 , 13
2 , 10
2 , 11
2 , 12
2 , 13

Python While Loop

Syntax of while loop

while condition:
    #body_of_while

Python – while loop with else block

We can have a ‘else’ block associated with while loop. The ‘else’ block is optional. It executes only after the loop finished execution.

num = 10
while num > 6:
   print(num)
   num = num-1
else:
   print("loop is finished")

Python break Statement

The break statement is used to terminate the loop when a certain condition is met

Python Continue Statement

The continue statement is used inside a loop to skip the rest of the statements in the body of loop for the current iteration and jump to the beginning of the loop for next iteration. 

Python pass Statement

  • The pass statement acts as a placeholder and usually used when there is no need of code but a statement is still required to make a code syntactically correct. 
  • For example we want to declare a function in our code but we want to implement that function in future, which means we are not yet ready to write the body of the function. 
  • In this case we cannot leave the body of function empty as this would raise error because it is syntactically incorrect, in such cases we can use pass statement which does nothing but makes the code syntactically correct.

Pass statement vs comment

You may be wondering that a python comment works similar to the pass statement as it does nothing so we can use comment in place of pass statement. Well, it is not the case, a comment is not a placeholder and it is completely ignored by the Python interpreter while on the other hand pass is not ignored by interpreter, it says the interpreter to do nothing.

Python pass statement example

If the number is even we are doing nothing and if it is odd then we are displaying the number.

for num in [20, 11, 9, 66, 4, 89, 44]:
    if num%2 == 0:
        pass
    else:
        print(num)