0% found this document useful (0 votes)
88 views

Python Programming: Control Structures

The document discusses various Python control structures including decision making statements like if, if-else, if-elif-else statements and examples. It also covers loop structures like for loop, while loop, nested loops. It describes the break and continue statements used to control loop execution and the pass statement which is a null operation.

Uploaded by

Keerthana P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python Programming: Control Structures

The document discusses various Python control structures including decision making statements like if, if-else, if-elif-else statements and examples. It also covers loop structures like for loop, while loop, nested loops. It describes the break and continue statements used to control loop execution and the pass statement which is a null operation.

Uploaded by

Keerthana P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Python Programming

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)

• Sometimes there are more than


two possibilities and we need
more than two branches. One
way to express a computation
like that is a chained conditional
• Syntax:
If statement:
Body
elif statement:
Body
else:
Body
Example of if...elif...else
if x < y:
print( 'x is less than y')
elif x > y:
print( 'x is greater than y')
else:
print( 'x and y are equal')
Example of if...elif...else
Code:
if choice == 'a':
print ('Bad guess')
elif choice == 'b':
print ('Good guess')
elif choice == 'c':
print('Close, but not correct')
Python Nested if statements
• Syntax
if expression1:
statement(s)
if expression2:
• In a nested if construct, you can have statement(s)

an if...elif...else construct inside elif expression3:

another if...elif...else construct. statement(s)


else
statement(s)
elif expression4:
statement(s)
else:
statement(s)
Python Nested if Example
if x == y:
print ('x and y are equal')
else:
if x < y:
print ('x is less than y')
else:
print ('x is greater than y')
Python Nested if Example
Code:
num = int(input("enter number"))
if num%2 == 0:
if num%3 == 0:
print ("Divisible by 3 and 2")
else:
print ("divisible by 2 not divisible by 3")
else:
if num%3 == 0:
print ("divisible by 3 not divisible by 2")
else:
print ("not Divisible by 2 not divisible by 3")
Check out these examples
• Check if a Number is Positive, Negative or 0

• Check if a Number is Odd or Even

• Check Leap Year


Python for Loop

• It has the ability to iterate


over the items of any
sequence, such as a list or a
string.

• 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

• It is used to iterate over the specific number of times within


the given range in steps/intervals.
• Syntax
– range(lowerlimit,upperlimit,increment/decrement)
Examples of range()
Iterating by Sequence Index

• An alternative way of iterating through each item


is by index offset into the sequence itself.
• Example
>>> fruits = ['banana', 'apple', 'mango’]
>>> for index in range(len(fruits)):
print( 'Current fruit :', fruits[index])
>>> print("Good bye!")
Nested for Loops
• Loops defined within another Loop is called Example
Nested Loop. for i in range(1,6):
• Syntax for nested for loop: for j in range (1,i+1):
print( i)
for iterating_var in sequence: print ()
for iterating_var in sequence:
statements(s)
statements(s)
Using else Statement with Loops

• 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

• Print the Fibonacci sequence


• Check Armstrong Number
• Find Armstrong Number in an Interval
Loop Control Statements
• Python break statement
• Python continue statement
• Python pass statement
Python break statement

• The break statement terminates the loop containing


it. Control of the program flows to the statement
immediately after the body of the loop.
• If break statement is inside a nested loop (loop
inside another loop), break will terminate the
innermost loop.

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

You might also like