Python Looping Statement and Decision Making

Narayanan
3 min readJun 27, 2022

--

Here we are gonna to discuss about Decision making and Looping statement.

Decision making

The statement will be executed if the condition is True.

Normal If

If condition is true then the statement inside the condition will be executed.

age = 21

if (age>18):
print(“You are teen”)

If Else

If condition is true then the statement inside the condition will be executed if condition is false else statement will be executed.

age = 21
if(age>40):
print(“You are citizen”)
else:
print(“You are below 40”)

Else If

Elif is used for multiple if condition.

if(age>40):
print(“You are citizen”)
elif(age<18):
print(“You are kid”)
else:
print(“You are adult”)

Nested If

You can if statement inside an if statement means the condition will executed if both statement is true.


if(age<=40):
if(age>18):
print(“You are adult”)
else:
print(“you are kid”)
else:
print(“You are Citizen”)

Ternary operator

You can also use ternary operator it act as normal if else statement.


print(“You are kid” if age<18 else “you are adult”)
print(“You are kid” if age<18 else “you are citizen” if age>40 else “you are adult”)

Multiple conditions

Multiple if statement there are two methods and and or. In and it need to true on both condition to print a statement. In or method any one condition should be true to execute the statement.


if((age>18) and (age<40)):
print(“you are allowed to ride”)
else:
print(“you are not allowed to ride”)
if((age>40)or(age<18)):
print(“you are not allowed to ride”)
else:
print(“you are allowed to ride”)

Looping Statement

The loop will run continuously until make the condition False.

While loop

i=5
while i>1:
print(i)
#you need to reduce the element to make the condition false
i-=1
print("Rest of code")

While else

while i<10:
print(i)
#Here i'm incrementer for making the condition false
i+=1
else:
print("Value is greater than 10")

Simple example

savings = int(input())
#on command prompt enter value in 100's
robot_toy = 1500
total = 0
while total<robot_toy:
total = total+savings
print(“You save ”,total)
else:
print(“You purchased”)

with conditional statement statement

savings = int(input())
#on command prompt enter value in 100's
robot_toy = 1500
total = 0
while total<robot_toy:
total = total+savings
print(“You are depostied”,total)
if (total==robot_toy):
print(“You purchased toy”)
elif (total>robot_toy):
print(“You purchased toy with chocolates”)

For loop

i=[1,2,3,4,5,6,7,8,9]
for value in i:
print(value)
#you can use range in for loop
for value in range(10,20):
print(value)
#The third element on range in gap between both range 20,25,30
for value in range(20,41,5):
print(value)
#It will query string element
for value in ‘Hello World’:
print(value)
word = ‘Hello World’
for value in word:
print(value)
#You can use length element also to run within range
for value in range(len(word)):
print(value)

For else

for x in range(1,10):
print(x)
else:
print(“loop finished”)

For if

for x in range(0,10):
if x%2 ==0:
print(“its even number”,x)
if x%2 ==1:
print(“its odd number”,x)

Multiple for

for a in range(1):
for b in range(0,21,2):
print(a+b)

Continue pass break

for x in range(0,10):
if x==0:
continue #It will not execute but program will run
if x==1:
print(“pass executed”) #(when there is no function on particular value then you use pass it will do nothing)
pass
if x%2 ==0:
print(“its even number”,x)
if x%2 ==1:
print(“its odd number”,x)
if x ==7:
break #Stop the program

Follow for more interesting topics :-)

Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Github:- https://github.com/Coder033
Skillshare:- https://www.skillshare.com/profile/Coder033/26410885 Youtube:-https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg

--

--

Narayanan

Front-end Development | Back-end development | Data Science | Data Visualization | Hodophile | Booklover