Python Programming: Control Structures
Python Programming: Control Structures
CONTROL STRUCTURES
Decision Making Statements
• Python if...else
• Python for Loop
• Python while Loop
• Python break and continue
• Python Pass
Python if Statements
• 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
Example: Python if Statement
if x > 0 :
print( 'x is positive')
Note:
The Boolean expression after the if statement is called the condition. We end the if statement with a colon character (:)
and the line(s) after the if statement are indented.
Python if...else
• An else statement contains the block of code
that executes if the conditional expression in
the if statement resolves to 0 or a FALSE value.
• Syntax
if expression:
statement(s)
else:
statement(s)
Example of if...else
if x%2 == 0 :
print( 'x is even')
else :
print ('x is odd')
Note:
If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that
effect. If the condition is false, the second set of statements is executed.
Python if...elif...else Statemen
(Chained Conditionals)
• Syntax
for iterating_var in sequence:
statements(s)
Python For Examples
Code:
>>> for letter in 'Python':
print ('Current Letter :', letter)
>>> fruits = ['banana', 'apple', 'mango’]
>>> for fruit in fruits:
print ('Current fruit :', fruit)
>>> print ("Good bye!")
The range() function
• Example
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print ('%d equals %d * %d' % (num,i,j))
break #to move to the next number, the #first FOR
else: # else part of the loop
print( num, 'is a prime number')
Check out these examples
• Check Prime Number
• Print all Prime Numbers in an Interval
• Find the Factorial of a Number
While Loop Statements
• Repeats a statement or group of
statements while the given condition is
TRUE.
• Syntax:
while (condition):
statements
Example: Python while Loop
Code:
count = 0 Output:
while (count < 9): The count is: 0
The count is: 1
print ('The count is:', count) The count is: 2
count = count + 1 The count is: 3
print ("Good bye!") The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
Using else Statement with Loops
1. If the else statement is used with a for loop, the else statement is executed when the loop has
exhausted iterating the list.
2. If the else statement is used with a while loop, the else statement is executed when the condition
becomes false.
Output:
Code: 0 is less than 5
count = 0 1 is less than 5
while count < 5: 2 is less than 5
print(count, " is less than 5") 3 is less than 5
count = count + 1 4 is less than 5
else: 5 is not less than 5
print(count, " is not less than 5")
Check out these examples
Syntax: break
Cont.….
• The working of break statement in for loop and while loop is
shown below.
Example: Python break
Code:
for val in "string":
if val == "i":
break
print(val)
print("The end")
Python continue statement
• The continue statement is used to skip the rest of
the code inside a loop for the current iteration only.
• Loop does not terminate but continues on with the
next iteration.
Syntax: continue
• The working of continue statement in for and while loop is
shown below.
Example: Python continue
Code:
for val in "string":
if val == "i":
continue
print(val)
print("The end")
Python pass statement
• It is used when a statement is required syntactically but you do not want any command or code to
execute.
• The pass statement is a null operation; nothing happens when it executes.
• The pass statement is also useful in places where your code will eventually go, but has not been
written yet i.e. in stubs.
Code: Output:
for letter in 'Python': Current Letter : P
if letter == 'h': Current Letter : y
pass Current Letter : t
print ('This is pass block') This is pass block
print ('Current Letter :', letter) Current Letter : h
Current Letter : o
print ("Good bye!") Current Letter : n
Good bye!
Take Quiz
Answer
Work outs
• Write a Python program which iterates the integers from 1 to
50. For multiples of three print "Fizz" instead of the number
and for the multiples of five print "Buzz". For numbers which
are multiples of both three and five print "FizzBuzz".
Take Quiz
Answer
Take Quiz
Answer