SlideShare a Scribd company logo
1
Python Programming
Using Problem Solving Approach
Reema Thareja
1
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
2
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
CHAPTER 4
Decision Control Statements
3
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Control Statements
A control statement is a statement that determines the control flow of a set of
instructions, i.e., it decides the sequence in which the instructions in a program
are to be executed.
Types of Control Statements —
• Sequential Control: A Python program is executed sequentially from the first
line of the program to its last line.
• Selection Control: To execute only a selected set of statements.
• Iterative Control: To execute a set of statements repeatedly.
4
If Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
:
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 5
# Get user input for a number
number = float(input("Please enter a
number: "))
# Check if the number is positive
if number > 0:
print("The number is positive.")
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 6
7
If-Else Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 8
# Input: Get the user's age
age = int(input("Enter your age: "))
# Check for voting eligibility
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 9
10
Nested if Statements
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
A statement that contains other statements is called a
compound statement. To perform more complex checks, if
statements can be nested, that is, can be placed one inside
the other. In such a case, the inner if statement is the
statement part of the outer one. Nested if statements are
used to check if more than one conditions are satisfied.
Example:
11
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 12
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 13
# Input: Get age and student status from the user
age = int(input("Enter your age: "))
is_student = input("Are you a student? (yes/no): ")
# Check for discounts
if age < 18:
print("You qualify for a child discount.")
if age >= 18 and age < 65:
if is_student == "yes":
print("You qualify for a student discount.")
else:
print("You do not qualify for a discount.")
if age >= 65:
print("You qualify for a senior discount.")
14
If-elif-else Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Python supports if-elif-else statements to test additional
conditions apart from the initial test expression.The if-elif-
else construct works in the same way as a usual if-else
statement. If-elif-else construct is also known as nested-if
construct.
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 15
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 16
17
While Loop
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Example:
18
For Loop
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
For loop provides a mechanism to repeat a task until a particular condition isTrue. It is usually known as a
determinate or definite loop because the programmer knows exactly how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of objects.
19
For Loop and Range() Function
The range() function is a built-in function in Python that is used to iterate over a sequence of numbers.The
syntax of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending with one less than the
number end.The step argument is option (that is why it is placed in brackets). By default, every number in
the range is incremented by 1 but we can specify a different increment using step. It can be both negative and
positive, but not zero.
Examples:
20
Range() Function
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
If range() function is given a single argument, it produces an object with values from 0 to argument-1. For
example: range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the second. For example,
range(0,10).
• If range() has three arguments then the third argument specifies the interval of the sequence produced. In
this case, the third argument must be an integer. For example, range(1,20,3).
Examples:
21
Condition-controlled and Counter-controlled Loops
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
22
Nested Loops
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Python allows its users to have nested loops, that is, loops that can be placed inside other loops.Although this
feature will work with any loop like while loop as well as for loop.
A for loop can be used to control the number of times a particular set of statements will be executed.
Another outer loop could be used to control the number of times that a whole loop is repeated.
Loops should be properly indented to identify which statements are contained within each for statement.
Example:
23
The Break Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
The break statement is used to terminate the execution of the nearest enclosing loop in which it appears.
The break statement is widely used with for loop and while loop. When compiler encounters a break
statement, the control passes to the statement that follows the loop in which the break statement appears.
Example:
24
The Continue Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Like the break statement, the continue statement can only appear in the body of a loop.When the compiler
encounters a continue statement then the rest of the statements in the loop are skipped and the control is
unconditionally transferred to the loop-continuation portion of the nearest enclosing loop.
Example:
25
The Pass Statement
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Pass statement is used when a statement is required syntactically but no command or code has to be
executed. It specified a null operation or simply No Operation (NOP) statement. Nothing happens when the
pass statement is executed.
Difference between comment and pass statements In Python programming, pass is a null statement. The
difference between a comment and pass statement is that while the interpreter ignores a comment
entirely, pass is not ignored. Comment is not executed but pass statement is executed but nothing happens.
Example:
26
The Else Statement Used With Loops
© OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else
statement is used with a for loop, the else statement is executed when the loop has completed iterating. But
when used with the while loop, the else statement is executed when the condition becomes false.
Examples:

More Related Content

Similar to PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT 2 (20)

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
class12sci
 
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
Syed Farjad Zia Zaidi
 
Unit 1
Unit 1Unit 1
Unit 1
Sowri Rajan
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
Review Python
Review PythonReview Python
Review Python
ManishTiwari326
 
Ch-3.pdf
Ch-3.pdfCh-3.pdf
Ch-3.pdf
R.K.College of engg & Tech
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPT_203105211_3.pptx
PPT_203105211_3.pptxPPT_203105211_3.pptx
PPT_203105211_3.pptx
SaurabhNage1
 
Ch04
Ch04Ch04
Ch04
Arriz San Juan
 
Python
PythonPython
Python
Aashish Jain
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
Anjali127411
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
Prakash Jayaraman
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdf
RanjanaThakuria1
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
Ni
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptxsession-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
RujanTimsina1
 
python BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptxpython BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 
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
class12sci
 
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
Syed Farjad Zia Zaidi
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
 
Chapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptxChapter 9 Conditional and Iterative Statements.pptx
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPT_203105211_3.pptx
PPT_203105211_3.pptxPPT_203105211_3.pptx
PPT_203105211_3.pptx
SaurabhNage1
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Introduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptxIntroduction& Overview-to-C++_programming.pptx
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
divyadhanwani67
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdf
RanjanaThakuria1
 
PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
Ni
 
session-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptxsession-1_Design_Analysis_Algorithm.pptx
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
RujanTimsina1
 
python BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptxpython BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 

Recently uploaded (20)

“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Ad

PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT 2

  • 1. 1 Python Programming Using Problem Solving Approach Reema Thareja 1 © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 2. 2 © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. CHAPTER 4 Decision Control Statements
  • 3. 3 © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Control Statements A control statement is a statement that determines the control flow of a set of instructions, i.e., it decides the sequence in which the instructions in a program are to be executed. Types of Control Statements — • Sequential Control: A Python program is executed sequentially from the first line of the program to its last line. • Selection Control: To execute only a selected set of statements. • Iterative Control: To execute a set of statements repeatedly.
  • 4. 4 If Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. :
  • 5. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 5 # Get user input for a number number = float(input("Please enter a number: ")) # Check if the number is positive if number > 0: print("The number is positive.")
  • 6. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 6
  • 7. 7 If-Else Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 8. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 8 # Input: Get the user's age age = int(input("Enter your age: ")) # Check for voting eligibility if age >= 18: print("You are eligible to vote.") else: print("You are not eligible to vote.")
  • 9. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 9
  • 10. 10 Nested if Statements © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. A statement that contains other statements is called a compound statement. To perform more complex checks, if statements can be nested, that is, can be placed one inside the other. In such a case, the inner if statement is the statement part of the outer one. Nested if statements are used to check if more than one conditions are satisfied. Example:
  • 11. 11
  • 12. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 12
  • 13. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 13 # Input: Get age and student status from the user age = int(input("Enter your age: ")) is_student = input("Are you a student? (yes/no): ") # Check for discounts if age < 18: print("You qualify for a child discount.") if age >= 18 and age < 65: if is_student == "yes": print("You qualify for a student discount.") else: print("You do not qualify for a discount.") if age >= 65: print("You qualify for a senior discount.")
  • 14. 14 If-elif-else Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Python supports if-elif-else statements to test additional conditions apart from the initial test expression.The if-elif- else construct works in the same way as a usual if-else statement. If-elif-else construct is also known as nested-if construct.
  • 15. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 15
  • 16. © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. 16
  • 17. 17 While Loop © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Example:
  • 18. 18 For Loop © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. For loop provides a mechanism to repeat a task until a particular condition isTrue. It is usually known as a determinate or definite loop because the programmer knows exactly how many times the loop will repeat. The for...in statement is a looping statement used in Python to iterate over a sequence of objects.
  • 19. 19 For Loop and Range() Function The range() function is a built-in function in Python that is used to iterate over a sequence of numbers.The syntax of range() is range(beg, end, [step]) The range() produces a sequence of numbers starting with beg (inclusive) and ending with one less than the number end.The step argument is option (that is why it is placed in brackets). By default, every number in the range is incremented by 1 but we can specify a different increment using step. It can be both negative and positive, but not zero. Examples:
  • 20. 20 Range() Function © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. If range() function is given a single argument, it produces an object with values from 0 to argument-1. For example: range(10) is equal to writing range(0, 10). • If range() is called with two arguments, it produces values from the first to the second. For example, range(0,10). • If range() has three arguments then the third argument specifies the interval of the sequence produced. In this case, the third argument must be an integer. For example, range(1,20,3). Examples:
  • 21. 21 Condition-controlled and Counter-controlled Loops © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED.
  • 22. 22 Nested Loops © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Python allows its users to have nested loops, that is, loops that can be placed inside other loops.Although this feature will work with any loop like while loop as well as for loop. A for loop can be used to control the number of times a particular set of statements will be executed. Another outer loop could be used to control the number of times that a whole loop is repeated. Loops should be properly indented to identify which statements are contained within each for statement. Example:
  • 23. 23 The Break Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. The break statement is widely used with for loop and while loop. When compiler encounters a break statement, the control passes to the statement that follows the loop in which the break statement appears. Example:
  • 24. 24 The Continue Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Like the break statement, the continue statement can only appear in the body of a loop.When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop. Example:
  • 25. 25 The Pass Statement © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Pass statement is used when a statement is required syntactically but no command or code has to be executed. It specified a null operation or simply No Operation (NOP) statement. Nothing happens when the pass statement is executed. Difference between comment and pass statements In Python programming, pass is a null statement. The difference between a comment and pass statement is that while the interpreter ignores a comment entirely, pass is not ignored. Comment is not executed but pass statement is executed but nothing happens. Example:
  • 26. 26 The Else Statement Used With Loops © OXFORD UNIVERSITY PRESS 2017.ALL RIGHTS RESERVED. Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else statement is used with a for loop, the else statement is executed when the loop has completed iterating. But when used with the while loop, the else statement is executed when the condition becomes false. Examples: