Python Decision Making and Loops
Python Decision Making and Loops
Python Decision Making and Loops
And Loops
Decision making
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
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
The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
Syntax:
if expression:
statement(s)
else :
statement(s)
The if-else statement
if age>=18:
else:
● 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
statement(s)
counter variable
condition
increment or decrement
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
str = "Python"
for i in str:
print(i)
Output
P
y
t
h
o
n
Python for loop
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
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)
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
#block of statements
#Other statements
Nested for loop in python
Output:
# User input for number of rows
rows = int(input("Enter the rows:")) Enter the rows:5
● 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.