SlideShare a Scribd company logo
Python Decision Making
And Loops
Decision making
ā— Decision making is the most important aspect of almost all the
programming languages.
ā— Decision structures evaluate multiple logical expressions which produce
TRUE or FALSE as outcome.
ā— non-zero and non-null values as TRUE, otherwise FALSE
Decision making
Decision making statements in python
1. If
2. If else
3. nested if
Indentation in Python
ā— For the ease of programming and to achieve simplicity, python doesn't allow
the use of parentheses for the block level code.
ā— In Python, indentation is used to declare a block.
ā— If two statements are at the same indentation level, then they are the part of
the same block.
ā— Generally, four spaces are given to indent the statements which are a typical
amount of indentation in python.
The if statement
The syntax of the if-statement is given below.
if expression:
statement
E.g.
a=10
if a :
print(ā€œ value is ā€,a)
or
a=10
if a : print(ā€œ value is ā€,a)
The if statement
e.g. X= int(input(ā€œEnter a numberā€))
r=X%2
if (r==0):
print(ā€œEvenā€)
print(ā€œByeā€)
o/p:
Enter a number : 3
bye
Enter a number : 50
Even
Bye
The if-else statement
The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
If the condition is true, then the if-block is executed.
Otherwise, the else-block is executed.
The if-else statement
Syntax:
if expression:
statement(s)
else :
statement(s)
The if-else statement
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! You are not eligible to vote !!");
The elif statement
ā— The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them.
ā— We can have any number of elif statements in our program depending upon
our need.
ā— However, using elif is optional.
ā— The elif statement works like an if-else-if ladder statement in C. It must be
succeeded by an if statement.
The elif statement
Nested if
• Can use one if or else if statement inside another if or else if statement(s).
• Can have an if...elif...else construct inside another if...elif...else construct.
LOOPS
ā— Allows us to execute a statement or group of statements multiple times.
1. While loop
2. For loop
3. Nested loop
while loop
ā— Repeatedly executes target statement(s) as long as a given condition is true
Syntax: while condition:
statement(s)
ā— Python uses indentation as its method of grouping statements
ā— In Python, all the statements indented by the same number of character
spaces after a programming construct( if, for , while etc….) are considered to
be part of a single block of code
ā— While loop needs,
counter variable
condition
increment or decrement
ā— Infinite loop – Cntl + C
while loop
i=1
number = int(input("Enter the number:"))
while i<=10:
print("%d X %d = %d n" %(number,i,number*i))
i = i+1
Output:
1
2
3
4
5
6
7
8
9
10
While with Else
ā— Python supports to have an else statement
associated with a loop statement.
ā— If the else statement is used with a while
loop, the else statement is executed when
the condition becomes false.
ā—
Print a message once the condition is false:
i = 0
while i < 5:
print("%d is less than 5" %i)
i += 1
else:
print("%d is not less than 5" %i)
Output
Python for loop
The for loop in Python is used to iterate the statements or a part of the program
several times.
It is frequently used to traverse the data structures like list, tuple, or dictionary.
āž” The syntax of for loop in python is given below.
for iterating_var in sequence:
statement(s)
Python for loop
Example-1: Iterating string using for
loop
str = "Python"
for i in str:
print(i)
Output
P
y
t
h
o
n
Python for loop
Program to print the table of the given number .
list = [1,2,3,4,5,6,7,8,9,10]
n = 5
for i in list:
c = n*i
print(c)
For loop Using range() function
The range() function is used to generate the sequence of the numbers.
If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the
range() function is given below.
Syntax:
range(start,stop,step size)
ā— The start represents the beginning of the iteration.
ā— The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
ā— The step size is used to skip the specific numbers from the iteration. It is optional
to use. By default, the step size is 1. It is optional.
For loop Using range() function
Program to print numbers in sequence.
for i in range(10):
print(i,end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9
For loop Using range() function
Program to print table of given number.
n = int(input("Enter the number "))
for i in range(1,11):
c = n*i
print(n,"*",i,"=",c)
For loop Using range() function
Program to print even number using step size in
range().
n = int(input("Enter the number "))
for i in range(2,n,2):
print(i)
Output:
Enter the number 20
2
4
6
8
10
12
14
16
18
Nested for loop in python
Python allows us to nest any number of for loops inside a for loop.
The inner loop is executed n number of times for every iteration of the outer loop.
The syntax is given below.
Syntax
for iterating_var1 in sequence: #outer loop
for iterating_var2 in sequence: #inner loop
#block of statements
#Other statements
Nested for loop in python
# User input for number of rows
rows = int(input("Enter the rows:"))
# Outer loop will print number of rows
for i in range(1,rows+1):
# Inner loop will print number of Astrisk
for j in range(i):
print("*",end = '')
print()
Output:
Enter the rows:5
*
**
***
****
*****
Using else statement with for loop
ā— Unlike other languages like C, C++, or Java, Python allows us to use the else
statement with the for loop which can be executed only when all the iterations
are exhausted.
ā— Here, we must notice that if the loop contains any of the break statement then
the else statement will not be executed.
Using else statement with for loop
for i in range(0,5):
print(i)
else:
print("for loop completely exhausted, since there is no break.")
Output :
0
1
2
3
4
for loop completely exhausted, since there is no break.

More Related Content

What's hot (20)

PPTX
Presentation on python
william john
Ā 
PPTX
Python OOPs
Binay Kumar Ray
Ā 
PDF
Python exception handling
Mohammed Sikander
Ā 
PPTX
PYTHON PPT.pptx
AbhishekMourya36
Ā 
PPTX
introduction to Python (for beginners)
guobichrng
Ā 
PPTX
Decision Making Statement in C ppt
MANJUTRIPATHI7
Ā 
PDF
Strings in python
Prabhakaran V M
Ā 
PPTX
Input output statement in C
Muthuganesh S
Ā 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
Ā 
PPTX
Operators in Python
Anusuya123
Ā 
PPTX
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
Ā 
PDF
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
Ā 
PPTX
Regular expressions in Python
Sujith Kumar
Ā 
PPTX
Python Scipy Numpy
Girish Khanzode
Ā 
PPTX
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
Ā 
PPTX
Looping statement in python
RaginiJain21
Ā 
PPSX
python Function
Ronak Rathi
Ā 
PPTX
Python PPT
Edureka!
Ā 
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
Ā 
Presentation on python
william john
Ā 
Python OOPs
Binay Kumar Ray
Ā 
Python exception handling
Mohammed Sikander
Ā 
PYTHON PPT.pptx
AbhishekMourya36
Ā 
introduction to Python (for beginners)
guobichrng
Ā 
Decision Making Statement in C ppt
MANJUTRIPATHI7
Ā 
Strings in python
Prabhakaran V M
Ā 
Input output statement in C
Muthuganesh S
Ā 
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
Ā 
Operators in Python
Anusuya123
Ā 
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
Ā 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
Ā 
Regular expressions in Python
Sujith Kumar
Ā 
Python Scipy Numpy
Girish Khanzode
Ā 
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
Ā 
Looping statement in python
RaginiJain21
Ā 
python Function
Ronak Rathi
Ā 
Python PPT
Edureka!
Ā 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
Ā 

Similar to Python Decision Making And Loops.pdf (20)

PPT
Control structures pyhton
Prakash Jayaraman
Ā 
PPTX
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
Ā 
PPTX
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
Ā 
DOCX
controlstatementspy.docx
manohar25689
Ā 
PPTX
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
Ā 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
Ā 
PPTX
ControlStructures.pptx5t54t54444444444444444
pawankamal3
Ā 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
Ā 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
Ā 
PPTX
Programming in python - Week 4
Priya Nayak
Ā 
PDF
Python unit 2 M.sc cs
KALAISELVI P
Ā 
PDF
basic of desicion control statement in python
nitamhaske
Ā 
PPTX
Week 4.pptx computational thinking and programming
cricketfundavlogs
Ā 
PPTX
Python programming workshop session 2
Abdul Haseeb
Ā 
PPTX
Python Flow Control & use of functions.pptx
pandyahm47
Ā 
PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
Ā 
PPTX
python ppt.pptx
ssuserd10678
Ā 
PPTX
module 2.pptx
mahendranaik18
Ā 
PPTX
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
Ā 
PDF
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
Ā 
Control structures pyhton
Prakash Jayaraman
Ā 
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
Ā 
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
Ā 
controlstatementspy.docx
manohar25689
Ā 
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
Ā 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
Ā 
ControlStructures.pptx5t54t54444444444444444
pawankamal3
Ā 
1. control structures in the python.pptx
DURAIMURUGANM2
Ā 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
Ā 
Programming in python - Week 4
Priya Nayak
Ā 
Python unit 2 M.sc cs
KALAISELVI P
Ā 
basic of desicion control statement in python
nitamhaske
Ā 
Week 4.pptx computational thinking and programming
cricketfundavlogs
Ā 
Python programming workshop session 2
Abdul Haseeb
Ā 
Python Flow Control & use of functions.pptx
pandyahm47
Ā 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
Ā 
python ppt.pptx
ssuserd10678
Ā 
module 2.pptx
mahendranaik18
Ā 
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
Ā 
E-Notes_3721_Content_Document_20250107032537PM.pdf
aayushihirpara297
Ā 
Ad

Recently uploaded (20)

PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
Ā 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
Ā 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
Ā 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
Ā 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
Ā 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
Ā 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
Ā 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
Ā 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
Ā 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
Ā 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
Ā 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
Ā 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
Ā 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
Ā 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
Ā 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
Ā 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
Ā 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
Ā 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
Ā 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
Ā 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
Ā 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
Ā 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
Ā 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
Ā 
Import Data Form Excel to Tally Services
Tally xperts
Ā 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
Ā 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
Ā 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
Ā 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
Ā 
Human Resources Information System (HRIS)
Amity University, Patna
Ā 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
Ā 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
Ā 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
Ā 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
Ā 
Ad

Python Decision Making And Loops.pdf

  • 2. Decision making ā— Decision making is the most important aspect of almost all the programming languages. ā— Decision structures evaluate multiple logical expressions which produce TRUE or FALSE as outcome. ā— non-zero and non-null values as TRUE, otherwise FALSE
  • 3. Decision making Decision making statements in python 1. If 2. If else 3. nested if
  • 4. Indentation in Python ā— For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. ā— In Python, indentation is used to declare a block. ā— If two statements are at the same indentation level, then they are the part of the same block. ā— Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.
  • 5. The if statement The syntax of the if-statement is given below. if expression: statement E.g. a=10 if a : print(ā€œ value is ā€,a) or a=10 if a : print(ā€œ value is ā€,a)
  • 6. The if statement e.g. X= int(input(ā€œEnter a numberā€)) r=X%2 if (r==0): print(ā€œEvenā€) print(ā€œByeā€) o/p: Enter a number : 3 bye Enter a number : 50 Even Bye
  • 7. The if-else statement The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
  • 8. The if-else statement Syntax: if expression: statement(s) else : statement(s)
  • 9. The if-else statement age = int (input("Enter your age? ")) if age>=18: print("You are eligible to vote !!"); else: print("Sorry! You are not eligible to vote !!");
  • 10. The elif statement ā— The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. ā— We can have any number of elif statements in our program depending upon our need. ā— However, using elif is optional. ā— The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
  • 12. Nested if • Can use one if or else if statement inside another if or else if statement(s). • Can have an if...elif...else construct inside another if...elif...else construct.
  • 13. LOOPS ā— Allows us to execute a statement or group of statements multiple times. 1. While loop 2. For loop 3. Nested loop
  • 14. while loop ā— Repeatedly executes target statement(s) as long as a given condition is true Syntax: while condition: statement(s) ā— Python uses indentation as its method of grouping statements
  • 15. ā— In Python, all the statements indented by the same number of character spaces after a programming construct( if, for , while etc….) are considered to be part of a single block of code ā— While loop needs, counter variable condition increment or decrement ā— Infinite loop – Cntl + C
  • 16. while loop i=1 number = int(input("Enter the number:")) while i<=10: print("%d X %d = %d n" %(number,i,number*i)) i = i+1 Output: 1 2 3 4 5 6 7 8 9 10
  • 17. While with Else ā— Python supports to have an else statement associated with a loop statement. ā— If the else statement is used with a while loop, the else statement is executed when the condition becomes false. ā— Print a message once the condition is false: i = 0 while i < 5: print("%d is less than 5" %i) i += 1 else: print("%d is not less than 5" %i) Output
  • 18. Python for loop The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary. āž” The syntax of for loop in python is given below. for iterating_var in sequence: statement(s)
  • 19. Python for loop Example-1: Iterating string using for loop str = "Python" for i in str: print(i) Output P y t h o n
  • 20. Python for loop Program to print the table of the given number . list = [1,2,3,4,5,6,7,8,9,10] n = 5 for i in list: c = n*i print(c)
  • 21. For loop Using range() function The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below. Syntax: range(start,stop,step size) ā— The start represents the beginning of the iteration. ā— The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional. ā— The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
  • 22. For loop Using range() function Program to print numbers in sequence. for i in range(10): print(i,end = ' ') Output: 0 1 2 3 4 5 6 7 8 9
  • 23. For loop Using range() function Program to print table of given number. n = int(input("Enter the number ")) for i in range(1,11): c = n*i print(n,"*",i,"=",c)
  • 24. For loop Using range() function Program to print even number using step size in range(). n = int(input("Enter the number ")) for i in range(2,n,2): print(i) Output: Enter the number 20 2 4 6 8 10 12 14 16 18
  • 25. Nested for loop in python Python allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax is given below. Syntax for iterating_var1 in sequence: #outer loop for iterating_var2 in sequence: #inner loop #block of statements #Other statements
  • 26. Nested for loop in python # User input for number of rows rows = int(input("Enter the rows:")) # Outer loop will print number of rows for i in range(1,rows+1): # Inner loop will print number of Astrisk for j in range(i): print("*",end = '') print() Output: Enter the rows:5 * ** *** **** *****
  • 27. Using else statement with for loop ā— Unlike other languages like C, C++, or Java, Python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. ā— Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed.
  • 28. Using else statement with for loop for i in range(0,5): print(i) else: print("for loop completely exhausted, since there is no break.") Output : 0 1 2 3 4 for loop completely exhausted, since there is no break.