0% found this document useful (0 votes)
48 views48 pages

If-Else - Loop

Uploaded by

dveditor98
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)
48 views48 pages

If-Else - Loop

Uploaded by

dveditor98
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/ 48

Python Flow

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

• Python interprets any non-zero value as True. None and 0 are


interpreted as False.
Flowchart of while
Loop
Exam
ple
while loop with
else
Python break and
continue
• What is the use of break and continue in Python?
• Python break statement
• Syntax of break
• Flowchart of break
• Example of break
• Python continue statement
• Syntax of Continue
• Flowchart of continue
• Example: Python continue
What is the use of break and
continue in Python?
• break and continue statements can alter the flow of a normal loop
• Loops iterate over a block of code until test expression is false, but
sometimes we wish to terminate the current iteration or even the
whole loop without checking test expression
• break and continue statements are used in these cases
Python 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 of break
Flowchart of
break
Exam
ple
Python 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 of Continue
Flowchart of
continue
Exam
ple
Python pass
statement
• What is pass statement in Python?
• Syntax of pass
• Example: pass Statement
What is pass statement in
Python?
• pass is a null statement
• The difference between a comment and pass statement in Python is
that, while the interpreter ignores a comment entirely, pass is not
ignored
• However, nothing happens when pass is executed
• It results into no operation (NOP)
• Syntax of pass
Exam
ple
Exam
ple

You might also like