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

PPTX
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
jaychoudhary37
 
PPTX
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
 
PPT
UNIT II_python Programming_aditya College
Ramanamurthy Banda
 
PPTX
Introduction to Python Part-1
Devashish Kumar
 
PPTX
Unit - 2 CAP.pptx
malekaanjum1
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
DOCX
INTERNSHIP REPORT.docx
21IT200KishorekumarI
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
jaychoudhary37
 
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
 
UNIT II_python Programming_aditya College
Ramanamurthy Banda
 
Introduction to Python Part-1
Devashish Kumar
 
Unit - 2 CAP.pptx
malekaanjum1
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
INTERNSHIP REPORT.docx
21IT200KishorekumarI
 

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

PPTX
Python Revision Tour 1 Class XII CS
class12sci
 
PPTX
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PDF
Unit 1
Sowri Rajan
 
PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PPTX
Review Python
ManishTiwari326
 
PPTX
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPTX
PPT_203105211_3.pptx
SaurabhNage1
 
PPTX
Python
Aashish Jain
 
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
PPT
slides03.ppt
Anjali127411
 
PPT
Control structures pyhton
Prakash Jayaraman
 
PPTX
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
PPTX
Introduction to C++ programming language
divyadhanwani67
 
PDF
ch2 Python flow control.pdf
RanjanaThakuria1
 
DOCX
PYTHON NOTES
Ni
 
PPTX
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
PPT
introduction to python in english presentation file
RujanTimsina1
 
Python Revision Tour 1 Class XII CS
class12sci
 
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
Unit 1
Sowri Rajan
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
Review Python
ManishTiwari326
 
Chapter 9 Conditional and Iterative Statements.pptx
XhelalSpahiu
 
PPT_203105211_3.pptx
SaurabhNage1
 
Python
Aashish Jain
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
slides03.ppt
Anjali127411
 
Control structures pyhton
Prakash Jayaraman
 
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
divyadhanwani67
 
ch2 Python flow control.pdf
RanjanaThakuria1
 
PYTHON NOTES
Ni
 
session-1_Design_Analysis_Algorithm.pptx
chandankumar364348
 
introduction to python in english presentation file
RujanTimsina1
 
Ad

Recently uploaded (20)

PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
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: