PMDS508L - Python Programming
Control Structures
Dr. B.S.R.V. Prasad
Department of Mathematics
School of Advanced Sciences
Vellore Institute of Technology
Vellore
[Link]@[Link] (Personal)
[Link]@[Link] (Official)
+91-8220417476
Python Conditions 1
Python supports the usual logical conditions from mathematics:
▶ Equals: a == b
▶ Not Equals: a ! = b
▶ Less than: a < b
▶ Less than or equal to: a <= b
▶ Greater than: a > b
▶ Greater than or equal to: a >= b
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
If Statements in Python 2
1 if < condition >:
2 statements to be executed if condition is true
3 elif < conditon >:
4 statements to be executed if else condition is true
5 else :
6 statements to be executed when all the above
conditions fails
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Short Hand If and If...Else 3
▶ If you have only one statement to execute, one for if, and one for else, you
can put it all on the same line:
1 a = 20
2 b = 330
3 print ( " a > b") if a > b else print ("b > a")
▶ You can also have multiple else statements on the same line:
1 a = 330
2 b = 330
3 print ( " a > b") if a > b else print ("a = b") if a ==
b else print ("b > a")
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Combining the conditions and Nested If 4
▶ To check if two conditions are satisfied or not then we need to use the
keyword and
▶ To check if any one of the wto conditions are satisfied or not then we need to
use the keyword or
▶ Nested Loops can also be used in the Python but we need to take care of the
spacing.
▶ pass statement.
▶ The if statements cannot be empty. If it empty then we need to use pass
statement to avoid getting an error.
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Multi-branching 5
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Multi-branching
if...elif...else 6
1 no = int ( input ( ' Enter the number '))
2 if no == 0:
3 print ( ' Sunday ')
4 elif no == 1:
5 print ( ' Monday ')
6 .
7 .
8 .
9 elif no == 6:
10 print ( ' Saturday ')
11 else :
12 print ( ' Invalid Input ')
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Multi-branching
match-case 7
▶ Developers coming from languages like C/C++ or Java know that there is a
conditional statement known as a Switch Case using which above code can
be coded.
▶ In Python this is match-case and was introduced in Python 3.10.
▶ The match case statement in Python is initialized with the match keyword
followed by the parameter to be matched.
▶ Then various cases are defined using the case keyword and the pattern to
match the parameter.
▶ The “_” is the wildcard character that runs when all the cases fail to match
the parameter value.
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Multi-branching
match-case 8
1 match parameter :
2 case pattern1 :
3 # code for pattern 1
4 case pattern2 :
5 # code for pattern 2
6 .
7 .
8 .
9 case patterN :
10 # code for pattern N
11 case _ :
12 # default code block
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Multi-branching
match-case 9
1 no = int ( input ( ' Enter the number '))
2 match no :
3 case 0:
4 print ( ' Sunday ')
5 case 1:
6 print ( ' Monday ')
7 .
8 .
9 .
10 case 6:
11 print ( ' Saturday ')
12 case _:
13 print ( ' Invalid Input ')
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops 10
Python has two primitive loop commands:
▶ while loops
▶ for loops
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
While Loops 11
1 i =1
2 while i <10:
3 print ( i )
4 i += 1
break Statement can be used to come out of the while loop even if the condition
is true
1 i =1
2 while i <10:
3 print ( i )
4 if i ==3:
5 break
6 i += 1
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
While Loops 12
continue Statement can be used to stop the current iteration and go to next
iteration.
1 i =1
2 while i <10:
3 print ( i )
4 if i ==3:
5 continue
6 i += 1
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
While Loops 13
With the else statement we can run a block of code once when the condition no
longer is true:
1 i =1
2 while i <10:
3 print ( i )
4 i += 1
5 else :
6 print ( " i is no longer greater than 10 ")
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops 14
▶ A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string)
▶ This is much like the for keyword in other programming languages, and
works more like an iterator method as found in other object-orientated
programming languages.
▶ With the for loop we can execute a set of statements, once for each item in
a list, tuple, set etc.
▶ The for loop does not require an indexing variable to set beforehand.
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops
The range() Function 15
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number (excluding the
specified number).
▶ range(5) - Returns the values starting from 0 to 5 increment by 1.
▶ range(a,b) - Returns the values starting from a to b − 1 increment by 1.
▶ range(a,b,n) - Returns the values starting from a to b − 1 increment by n.
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops 16
1 for x in range (1 ,5) :
2 print ( x )
1 for x in " Banana ":
2 print ( x )
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops
break and continue statements 17
1 for x in range (1 ,10 ,2) :
2 if x == 5:
3 break
4 print ( x )
1 for x in range (1 ,10 ,2) :
2 if x == 5:
3 continue
4 print ( x )
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops
Else in For Loop 18
The else keyword in a for loop specifies a block of code to be executed when the
loop is finished:
1 for x in range (6) :
2 print ( x )
3 else :
4 print ( ' For loop finished ! ')
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Nested For Loops 19
1 for x in range (1 ,10) :
2 for y in (x ,x +2) :
3 print (x , y)
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python For Loops
The pass Statement 20
for loops cannot be empty, but if you for some reason have a for loop with no
content, put in the pass statement to avoid getting an error.
1 for x in range (1 ,5) :
2 pass
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Sample Programs 21
Program
Print all the numbers which are divisible by 2 between any two given numbers
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Sample Programs 22
1 num1 = int ( input (" Enter the first number : "))
2 num2 = int ( input (" Enter the second number : "))
3
4 for x in range ( num1 , num2 +1) :
5 if x %2 == 0:
6 print (x ," is divisible by 2")
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Sample Programs 23
Program
Check whether a given number is prime or not?
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Sample Programs 24
1 num = int ( input (" Enter the number : "))
2
3 if num <= 1:
4 print ( " Entered number should be greater than 1")
5 else :
6 for x in range (2 , num //2+1) :
7 if num %x == 0:
8 print ( num ," is not prime ")
9 break
10 else :
11 print ( num ," is prime ")
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Exercise Programs 25
Program
Write a Python program which accepts two numbers from the user and prints all the prime
numbers between them.
Program
Write a Python program which accepts a number from the user and checks whether it is
palindrome or not?
Program
A number N is said to Armstrong number if N equal to sum of the cubes of each digit in that
number. For example 153 is Armstrong number as 153 = 13 + 53 + 33 .
Write a Python program which checks whether a given number is Armstrong number or not?
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming
Python Loops
Exercise Programs 26
Program
A positive integer N is said to be perfect if N is equal to the sum of its proper
divisors.
Write a Python program to check whether a given positive integer is prefect or
not?
Program
Write a Python program to find the factorial of a given number.
Program
Write a Python program to which accepts number of lines and then prints one ∗ in
the first line, two ∗’s in the second line, etc...
Dr. B.S.R.V. Prasad | PMDS508L - Python Programming