SlideShare a Scribd company logo
1 of 39
Download to read offline
Python Flow Control
if…else
for Loop
while loop
Break and continue
Pass statement
if...else
 Decision making is required when we want to
execute a code only if a certain condition is satisfied.
 The if…elif…else statement is used in Python for
decision making.
 In Python, the body of the if statement is indicated by
the indentation.
 Body starts with an indentation and the first
unindented line marks the end.
 Python interprets non-zero values
asTrue. None and 0 are interpreted as False.
Exercise – if
 Write a program to give a discount of
10% if the total bill amount exceeds 1000.
if...else Statement
 The if..else statement evaluates test expression and will
execute body of if only when test condition is True.
 If the condition is False, body of else is executed.
Indentation is used to separate the blocks.
Exercise
 Write a program to check if a given
number is a multiple of 5.
if...elif...else
 The elif is short for else if. It allows us
to check for multiple expressions.
 If the condition for if is False, it checks
the condition of the next elif block
and so on.
 If all the conditions are False, body of
else is executed.
 Only one block among the
several if...elif...else blocks is executed
according to the condition.
 The if block can have only
one else block. But it can have
multiple elifblocks.
Programming Task
 Minimum age to Cast yourVote : 18
 Minimum age to Contest an Election: 25
 Given the age verify if the person can
vote and can s(he) contest an election.
Exercise
 Write a program to check if a given year is leap year or not.
 Logic:
 if a year is not divisible by 4, its not a leap year.
 If a year is divisible by 4 and not divisible by 100, it’s a leap year.
 If a year is divisible by 4 and 100 then it should be divisible by 400
to be a leap year
Loops
 Loops are used in programming to
repeat a specific block of code.
 Looping Constructs in Python
◦ while
◦ for
While loop
 The while loop in Python is used to iterate over
a block of code as long as the test expression
(condition) is true.
 We generally use this loop when we don't know
beforehand, the number of times to iterate.
 Write a program to print the number of
digits of a given number using while loop.
For loop
 For loops iterate over a given sequence.
 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.
Range Function
 We can generate a sequence of numbers
using range() function.
 range(10) will generate numbers from 0 to 9 (10
numbers).
 To force this function to output all the items, we can
use the function list().
Range function
 We can also define the start, stop and
step size as range(start,stop,step size).
 step size defaults to 1 if not provided.
Exercise
 Write a program to calculate the factorial
of a given number.
break and continue
 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 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.
Exercise
 Write a program to check if a given
number is prime or not.
 If a number is divisible by any number
between 2 and n-1, its not prime,
otherwise prime
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.
for loop with else
 A for loop can have an optional else block as well.
The else part is executed if the items in the sequence
used in for loop exhausts.
 break statement can be used to stop a for loop. In such
case, the else part is ignored.
 Hence, a for loop's else part runs if no break occurs.
for loop with else
Nested Loop
while loop with else
 Same as that of for loop, we can have an
optional else block with while loop as well.
Pass statement
 Suppose we have a loop or a function that is not
implemented yet, but we want to implement it in the
future.
 They cannot have an empty body.
 We use the pass statement to construct a body that
does nothing.

More Related Content

What's hot

What's hot (20)

Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
What is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | EdurekaWhat is Python Lambda Function? Python Tutorial | Edureka
What is Python Lambda Function? Python Tutorial | Edureka
 
Strings
StringsStrings
Strings
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Python If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | EdurekaPython If Else | If Else Statement In Python | Edureka
Python If Else | If Else Statement In Python | Edureka
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python list
Python listPython list
Python list
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Python strings
Python stringsPython strings
Python strings
 
Python set
Python setPython set
Python set
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
List in Python
List in PythonList in Python
List in Python
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 

Similar to Python Flow Control

FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
jaychoudhary37
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
SukhpreetSingh519414
 

Similar to Python Flow Control (20)

dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Programming in python - Week 4
Programming in python  - Week 4Programming in python  - Week 4
Programming in python - Week 4
 
Python session3
Python session3Python session3
Python session3
 
Control Structures Python like conditions and loops
Control Structures Python like conditions and loopsControl Structures Python like conditions and loops
Control Structures Python like conditions and loops
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMS
 
PRESENTATION.pptx
PRESENTATION.pptxPRESENTATION.pptx
PRESENTATION.pptx
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
 
This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptx
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Ch-4.pdf
Ch-4.pdfCh-4.pdf
Ch-4.pdf
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Looping statements
Looping statementsLooping statements
Looping statements
 
07 flow control
07   flow control07   flow control
07 flow control
 

More from Mohammed Sikander

More from Mohammed Sikander (16)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 

Recently uploaded

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 

Python Flow Control

  • 1. Python Flow Control if…else for Loop while loop Break and continue Pass statement
  • 2. if...else  Decision making is required when we want to execute a code only if a certain condition is satisfied.  The if…elif…else statement is used in Python for decision making.  In Python, the body of the if statement is indicated by the indentation.  Body starts with an indentation and the first unindented line marks the end.  Python interprets non-zero values asTrue. None and 0 are interpreted as False.
  • 3.
  • 4. Exercise – if  Write a program to give a discount of 10% if the total bill amount exceeds 1000.
  • 5.
  • 6. if...else Statement  The if..else statement evaluates test expression and will execute body of if only when test condition is True.  If the condition is False, body of else is executed. Indentation is used to separate the blocks.
  • 7.
  • 8. Exercise  Write a program to check if a given number is a multiple of 5.
  • 9. if...elif...else  The elif is short for else if. It allows us to check for multiple expressions.  If the condition for if is False, it checks the condition of the next elif block and so on.  If all the conditions are False, body of else is executed.  Only one block among the several if...elif...else blocks is executed according to the condition.  The if block can have only one else block. But it can have multiple elifblocks.
  • 10.
  • 11.
  • 12. Programming Task  Minimum age to Cast yourVote : 18  Minimum age to Contest an Election: 25  Given the age verify if the person can vote and can s(he) contest an election.
  • 13. Exercise  Write a program to check if a given year is leap year or not.  Logic:  if a year is not divisible by 4, its not a leap year.  If a year is divisible by 4 and not divisible by 100, it’s a leap year.  If a year is divisible by 4 and 100 then it should be divisible by 400 to be a leap year
  • 14.
  • 15.
  • 16.
  • 17. Loops  Loops are used in programming to repeat a specific block of code.  Looping Constructs in Python ◦ while ◦ for
  • 18. While loop  The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.  We generally use this loop when we don't know beforehand, the number of times to iterate.
  • 19.
  • 20.  Write a program to print the number of digits of a given number using while loop.
  • 21.
  • 22. For loop  For loops iterate over a given sequence.  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.
  • 23.
  • 24. Range Function  We can generate a sequence of numbers using range() function.  range(10) will generate numbers from 0 to 9 (10 numbers).  To force this function to output all the items, we can use the function list().
  • 25. Range function  We can also define the start, stop and step size as range(start,stop,step size).  step size defaults to 1 if not provided.
  • 26.
  • 27. Exercise  Write a program to calculate the factorial of a given number.
  • 28.
  • 29.
  • 30. break and continue  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 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.
  • 31.
  • 32. Exercise  Write a program to check if a given number is prime or not.  If a number is divisible by any number between 2 and n-1, its not prime, otherwise prime
  • 33.
  • 34. 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.
  • 35. for loop with else  A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts.  break statement can be used to stop a for loop. In such case, the else part is ignored.  Hence, a for loop's else part runs if no break occurs.
  • 38. while loop with else  Same as that of for loop, we can have an optional else block with while loop as well.
  • 39. Pass statement  Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future.  They cannot have an empty body.  We use the pass statement to construct a body that does nothing.