SlideShare a Scribd company logo
1 of 27
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

Similar to Demo for Class.pptx

Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & ApplicationsEmroz Sardar
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 
Control structures
Control structuresControl structures
Control structuresGehad Enayat
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion StatementsHuda Alameen
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptxFajela
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSclass12sci
 
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHONUNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHONNandakumar P
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Digvijaysinh Gohil
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 

Similar to Demo for Class.pptx (20)

Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
 
Loop
LoopLoop
Loop
 
Control structures
Control structuresControl structures
Control structures
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CS
 
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHONUNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT - 2 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 

Recently uploaded

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Demo for Class.pptx

  • 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