If-Else - Loop
If-Else - Loop
Control
Python if...else
Python for Loop
Python while Loop
Python break and continue
Python Pass
Python
if...else
• What are if statement in Python?
• Python if Statement Syntax
• Python if Statement Flowchart
• Example: Python if Statement
• Python if...else Statement
• Syntax of if...else
• Python if..else Flowchart
• Example of if...else
• Python if...elif...else Statement
• Syntax of if...elif...else
• Flowchart of if...elif...else
• Example of if...elif...else
• Python Nested if statements
Python if Statement
Syntax
• Synta
x
Python if Statement
Flowchart
Examp
le:
Outp
ut
Syntax of
if...else
• Synta
x
Python if..else
Flowchart
Example of
if...else
Outp
ut
Syntax of
if...elif...else
• Synta
x
Flowchart of
if...elif...else
Example of
if...elif...else
Outp
ut
Python Nested if
statements
Outp
ut
Python for
Loop
• What is for loop in Python?
• Syntax of for Loop
• Flowchart of for loop
• Example: Python for Loop
• The range() function
• for loop with else
What is for loop in
Python?
• Is used to iterate over a sequence (list, tuple, string) or other iterable
objects
• Iterating over a sequence is called traversal
• Syntax of for Loop:
• Here, val is the variable that takes the value of the item inside the
sequence on each iteration
• Loop continues until we reach the last item in the sequence. The body
of for loop is separated from the rest of the code using indentation.
Flowchart of for
Loop
Exam
ple
The range()
function
• Can generate a sequence of numbers using range() function
• range(10) will generate numbers from 0 to 9 (10 numbers)
• Can also define the start, stop and step size as
range(start,stop,stepsize)
• Step size defaults to 1 if not provided.
• Does not store all the values in memory, it
would be inefficient
• So it remembers the start, stop, step size and generates the next
number on the go
Exam
ple
for loop with
else
Python while
Loop
• What is while loop in Python?
• Syntax of while Loop in Python
• Flowchart of while loop
• Example: Python while Loop
• while loop with else
What is while loop in
Python?
• Is used to iterate over a block of code as long as the test expression
(condition) is true
• Generally this loop is used when we don't know beforehand, the
number of times to iterate
Syntax of while Loop in
Python