SlideShare a Scribd company logo
PYTHON
SESSION: CONTROL STRUCTURES IN PYTHON
PREPARED BY:
Anub A
CONTENTS
REVIEW OF LAST CLASS
CONDITIONAL STATEMENTS
ITERATIVE STATEMNETS
CONTROL STRUCTURES
IF , IF ELSE , IF ELIF
IF , IF ELSE , IF ELE IF
Prepared By : Anub A
Review
• Keywords and Identifier
• Python Comments
• Python Variables
• Python Data Types
• Python Type Conversion
• Python I/O and Import
• Python Operators
• Python Namespace
Prepared By : Anub A
CONTROL STRUCTURES
• Python provides control structures to manage the
order of execution of a program
• In a python program, statements executes in
following three manners:
Prepared By : Anub A
1. SEQUENTIAL MANNER
2. SELECTIVE OR CONDITIONAL MANNER
3. ITERATIVE OR LOOP MANNER
SEQUENTIAL MANNER
Prepared By : Anub A
• Sequence is the default control structure
• Instructions executed one after other
# This program adds two numbers
def sum_of_two_no():
num1 = 1.5
num2 = 6.3
sum = float(num1) + float(num2)
print('The sum is =‘, sum)
sum_of_two_no():
SELECTIVE OR CONDITIONAL
Prepared By : Anub A
• Conditional constructs (also known as if
statements) provide a way to execute a chosen block
of code based on the run-time evaluation of one or
more Boolean expressions.
• A selection statement causes the program control
to be transferred to a specific flow based upon
whether a certain condition is true or not.
• IF
• IF ELSE
• IF ELIF
Each condition is a Boolean expression, and each body
contains one or more commands that are to be executed
conditionally.
If the first condition succeeds, the first body will be
executed; no other conditions or bodies are evaluated in that
case.
CONSTRUCT – if STATEMENT
if first condition:
Statements
Statements
.
.
.
: Colon Must
If the first condition succeeds, the first body will be
executed; no other conditions or bodies are evaluated in that
case.
Syntax:
EXAMPLES – if STATEMENT
OUT PUT
else is missing, it
is an optional
statement
CONSTRUCT – if else STATEMENT
if first condition:
first body
else:
second body
: Colon Must
• If the first condition succeeds, the first body will be executed;
• If the first condition fails(false case), then the process continues with
the else block.
Syntax:
CONDITIONAL CONSTRUCT – if else STATEMENT
FLOW CHART
Condition ? Statement 1 Statement 2
Statement 1
Statement 2
False
True
else
body
Main
Body
EXAMPLE – if else STATEMENT
OUT PUT
else is
used
: Colon Must
CONDITIONAL CONSTRUCT – if elif STATEMENT
If the first condition fails, then the process continues in
similar manner with the evaluation of the second condition.
The execution of this overall construct will cause precisely one
of the bodies to be executed.
There may be any number of elif clauses (including zero),
and
 The final else clause is optional.
CONDITIONAL CONSTRUCT – if else STATEMENT
second
Condition ?
Statement 1 Statement 2
Statement 1
Statement 2
True
elif
body
Main
Body
first
Condition ?
False
Statement 1
Statement 2
True
CONDITIONAL CONSTRUCT – if else STATEMENT
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
: Colon Must
• If the first condition succeeds, the first body will be executed;
• If the first condition fails, then the process continues with next
conditions.
Syntax:
EXAMPLES – if elif STATEMENT
READ AS
18 is less than
age
and
18 is less than
60
OUTPUT
ITERATIVE / LOOPING
Prepared By : Anub A
• Loops can execute a block of code number
of times until a certain condition is met.
• OR
• The iteration statement allows
instructions to be executed until a certain
condition is to be fulfilled.
• The iteration statements are also
called as loops or Looping statements
ITERATION OR LOOPING
Python provides two kinds of loops &
they are,
for loop
while loop
while loop
A while loop allows general repetition based upon the repeated
testing of a Boolean condition
The syntax for a while loop in Python is as follows:
while condition:
body
Where, loop body contain the single statement or set of statements
(compound statement) or an empty statement.
: Colon Must
while loop
The loop iterates while the expression evaluates to
true, when expression becomes false the loop terminates.
FLOW CHART
while loop - programs
OUTPUT
Natural Numbers generation
for LOOP
 Python’s for-loop syntax is a more convenient alternative to a
while loop when iterating through a series of elements.
 The for-loop syntax can be used on any type of iterable structure,
such as a list, tuple str, set, dict, or file
Syntax or general format of for loop is,
for element in iterable:
body
for LOOP
OUTPUT
Till the list
exhaust for loop
will continue to
execute.
for LOOP - range KEYWORD
The range() function returns a sequence of numbers,
starting from 0 by default, and increments by 1 (by default),
and ends at a specified number.
Syntax:
range(start, stop, step)
----------------------------------------------------------------------
Eg 1:
for n in range(3,6):
print(n)
Eg 2:
x = range(3, 6)
for n in x:
print(n)
for LOOP - range KEYWORD
OUTPUT
#Generating series of numbers
Q & A
Prepared By : Anub A
ThankYou
Prepared By : Anub A

More Related Content

PPTX
python.pptx
Poornima116356
 
PPTX
Control Structures Python like conditions and loops
ramireddyobulakondar
 
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
maheshnanda14
 
PPTX
Conditional and control statement
narmadhakin
 
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
PDF
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
sdvdsvsdvsvds
 
PPTX
Python statements
nuripatidar
 
python.pptx
Poornima116356
 
Control Structures Python like conditions and loops
ramireddyobulakondar
 
Chapter 9 Conditional and Iterative Statements.pptx
maheshnanda14
 
Conditional and control statement
narmadhakin
 
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
 
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
sdvdsvsdvsvds
 
Python statements
nuripatidar
 

Similar to python (20)

PDF
loops.pdf
AmayJaiswal4
 
PPTX
Python for Beginners(v2)
Panimalar Engineering College
 
PPTX
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
PDF
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
PDF
Python unit 2 M.sc cs
KALAISELVI P
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
Conditional_and_Recursion.pptx
Yagna15
 
PDF
basic of desicion control statement in python
nitamhaske
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PPTX
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
PPTX
Control_Statements.pptx
Koteswari Kasireddy
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPTX
PPT_203105211_3.pptx
SaurabhNage1
 
PPTX
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
PPTX
Python Flow Control & use of functions.pptx
pandyahm47
 
PPTX
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 
PPTX
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
loops.pdf
AmayJaiswal4
 
Python for Beginners(v2)
Panimalar Engineering College
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
FabMinds
 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
 
Week 4.pptx computational thinking and programming
cricketfundavlogs
 
Python unit 2 M.sc cs
KALAISELVI P
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
Conditional_and_Recursion.pptx
Yagna15
 
basic of desicion control statement in python
nitamhaske
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
Control_Statements.pptx
Koteswari Kasireddy
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPT_203105211_3.pptx
SaurabhNage1
 
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
Python Flow Control & use of functions.pptx
pandyahm47
 
RaspberryPi & Python Workshop Day - 02.pptx
ShivanshSeth6
 
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 

Recently uploaded (20)

PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 

python

  • 1. PYTHON SESSION: CONTROL STRUCTURES IN PYTHON PREPARED BY: Anub A
  • 2. CONTENTS REVIEW OF LAST CLASS CONDITIONAL STATEMENTS ITERATIVE STATEMNETS CONTROL STRUCTURES IF , IF ELSE , IF ELIF IF , IF ELSE , IF ELE IF Prepared By : Anub A
  • 3. Review • Keywords and Identifier • Python Comments • Python Variables • Python Data Types • Python Type Conversion • Python I/O and Import • Python Operators • Python Namespace Prepared By : Anub A
  • 4. CONTROL STRUCTURES • Python provides control structures to manage the order of execution of a program • In a python program, statements executes in following three manners: Prepared By : Anub A 1. SEQUENTIAL MANNER 2. SELECTIVE OR CONDITIONAL MANNER 3. ITERATIVE OR LOOP MANNER
  • 5. SEQUENTIAL MANNER Prepared By : Anub A • Sequence is the default control structure • Instructions executed one after other # This program adds two numbers def sum_of_two_no(): num1 = 1.5 num2 = 6.3 sum = float(num1) + float(num2) print('The sum is =‘, sum) sum_of_two_no():
  • 6. SELECTIVE OR CONDITIONAL Prepared By : Anub A • Conditional constructs (also known as if statements) provide a way to execute a chosen block of code based on the run-time evaluation of one or more Boolean expressions. • A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not. • IF • IF ELSE • IF ELIF
  • 7. Each condition is a Boolean expression, and each body contains one or more commands that are to be executed conditionally. If the first condition succeeds, the first body will be executed; no other conditions or bodies are evaluated in that case.
  • 8. CONSTRUCT – if STATEMENT if first condition: Statements Statements . . . : Colon Must If the first condition succeeds, the first body will be executed; no other conditions or bodies are evaluated in that case. Syntax:
  • 9. EXAMPLES – if STATEMENT OUT PUT else is missing, it is an optional statement
  • 10. CONSTRUCT – if else STATEMENT if first condition: first body else: second body : Colon Must • If the first condition succeeds, the first body will be executed; • If the first condition fails(false case), then the process continues with the else block. Syntax:
  • 11. CONDITIONAL CONSTRUCT – if else STATEMENT FLOW CHART Condition ? Statement 1 Statement 2 Statement 1 Statement 2 False True else body Main Body
  • 12. EXAMPLE – if else STATEMENT OUT PUT else is used : Colon Must
  • 13. CONDITIONAL CONSTRUCT – if elif STATEMENT If the first condition fails, then the process continues in similar manner with the evaluation of the second condition. The execution of this overall construct will cause precisely one of the bodies to be executed. There may be any number of elif clauses (including zero), and  The final else clause is optional.
  • 14. CONDITIONAL CONSTRUCT – if else STATEMENT second Condition ? Statement 1 Statement 2 Statement 1 Statement 2 True elif body Main Body first Condition ? False Statement 1 Statement 2 True
  • 15. CONDITIONAL CONSTRUCT – if else STATEMENT if first condition: first body elif second condition: second body elif third condition: third body else: fourth body : Colon Must • If the first condition succeeds, the first body will be executed; • If the first condition fails, then the process continues with next conditions. Syntax:
  • 16. EXAMPLES – if elif STATEMENT READ AS 18 is less than age and 18 is less than 60 OUTPUT
  • 17. ITERATIVE / LOOPING Prepared By : Anub A • Loops can execute a block of code number of times until a certain condition is met. • OR • The iteration statement allows instructions to be executed until a certain condition is to be fulfilled. • The iteration statements are also called as loops or Looping statements
  • 18. ITERATION OR LOOPING Python provides two kinds of loops & they are, for loop while loop
  • 19. while loop A while loop allows general repetition based upon the repeated testing of a Boolean condition The syntax for a while loop in Python is as follows: while condition: body Where, loop body contain the single statement or set of statements (compound statement) or an empty statement. : Colon Must
  • 20. while loop The loop iterates while the expression evaluates to true, when expression becomes false the loop terminates. FLOW CHART
  • 21. while loop - programs OUTPUT Natural Numbers generation
  • 22. for LOOP  Python’s for-loop syntax is a more convenient alternative to a while loop when iterating through a series of elements.  The for-loop syntax can be used on any type of iterable structure, such as a list, tuple str, set, dict, or file Syntax or general format of for loop is, for element in iterable: body
  • 23. for LOOP OUTPUT Till the list exhaust for loop will continue to execute.
  • 24. for LOOP - range KEYWORD The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax: range(start, stop, step) ---------------------------------------------------------------------- Eg 1: for n in range(3,6): print(n) Eg 2: x = range(3, 6) for n in x: print(n)
  • 25. for LOOP - range KEYWORD OUTPUT #Generating series of numbers
  • 26. Q & A Prepared By : Anub A