Flow Control in Python 3

2304

In this article, we will discuss if block, if-else block, if-elif-else block. Then we will discuss, While and For loop in Python 3 Programming.

Python Programming with if construct

You must have studied comparison operators in previous article Python Operators. With the help of a comparison operator, we can build expressions that will result in either True or False value. Thus, using if conditional construct we can take action or execute a line of codes based on the returned value is True or False.

Note: The use of operators in the conditional expression is not limited to comparison operators.

Code: 
a = 6
b = 4
c = 4
if a > b:
    print("{} is greater than {}".format(a, b))

if a > c:
    print("{} is less than {}".format(c, a))

if b == c:
    print("{} is equal to {}".format(b, c))

if a != c:
    print("{} is not equal to {}".format(a, c))

if a >= b:
    print("{} is greater than equal to {}".format(a, b))

if a > c:
    print("{} is less than equal to {}".format(c, a))

 

Output: 
6 is greater than 4
4 is less than 6
4 is equal to 4
6 is not equal to 4
6 is greater than equal to 4
4 is less than equal to 6

 

Python Programming with if-else block

We will make a program to find the lowest integer number out of two unique integers.

Code: 
a = int(input("Enter first integer: "))
b = int(input("Enter Second integer: "))

if a < b:
    print("{} is the lowest integer".format(a))
else:
    print("{} is the lowest integer".format(b))

 

Output: 
Enter first integer: 4
Enter Second integer: 3
3 is the lowest integer

Python Programming with if-elif-else block

Code: 
a = b = 4
c = 5
if a > b:
    print("{} is greater than {}".format(a, b))
elif b == c:
    print("{} is equal to {}".format(b, c))
else:
    print("Neither a is greater than b Nor b is equal to c ")

 

Output: 
Neither a is greater than b Nor b is equal to c


Python Programming with Nested if

We will make a program to find the greatest number out of three unique integer numbers.

Test Cases: 
# a b c
# 3 4 5
# 3 5 4
# 4 3 5
# 4 5 3
# 5 3 4
# 5 4 3
Code: 
a = int(input("Enter first integer: "))
b = int(input("Enter Second integer: "))
c = int(input("Enter Third integer: "))
if a > b:
    if a > c:
        print("{} is the greatest element".format(a))
    else:
        print("{} is the greatest element".format(c))

elif b > c:
    print("{} is the greatest element".format(b))

else:
    print("{} is the greatest element".format(c))

 

Output: 
Enter first integer: 3
Enter Second integer: 4
Enter Third integer: 5
5 is the greatest element

 

Python Programming with for loop

 

You need a loop construct when you need to perform the operation which is to be repeated over a list or range of data. In this section, we will discuss the possible uses of for loop in Python 3 programming.

Programming for loop over a list of Data

Code: 
a = [0, 1, 2, 3, 4]
for i in a:
    a[i] += 1

print("List after increment: {}".format(a))

 

Output: 
List after increment: [1, 2, 3, 4, 5]

 

Programming for loop over a range of numbers

We will also use the range data type, which we discussed in an earlier article  Data Types available in Python

Code: 
a = ["one", "two", "three", "four", "five"]
for i in range(0, 5):
    print("Value of i at {} step is {}".format(a[i], i + 1))

 

Output: 
Value of i at one step is 1
Value of i at two step is 2
Value of i at three step is 3
Value of i at four step is 4
Value of i at five step is 5

 

Python Programming using while loop

Code: 
a = ["one", "two", "three", "four", "five"]
i = 0
while i < 5:
    print(a[i])
    i += 1

 

Output: 
one
two
three
four
five