Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are:
if statement
if..else statements
if-elif ladder
If statement
if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Example 1:
#syntax
#if condition:
#statement1
#statement2
#program to illustrate If statement
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
I am not in if
if- else
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t.
## But what if we want to do something else if the condition is false.
Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
syntax
if (condition):
Executes this block if
# condition is true
else:
#Executes this block # if condition is false
Example 2:
#program to illustrate If else statement
i = 20;
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print ("i'm in else Block")
i is greater than 15
i'm in else Block
Example 3:
#EVEN ODD
x = int(input("Enter a number : "))
if x % 2 == 0 :
print("Even")
else :
print("Odd")
Enter a number : 689
Odd
Example 4:
#find greatest among three
x = int(input("Enter x : "))
y = int(input("Enter y : "))
z = int(input("Enter z : "))
if x >= y and x >= z :
print("{} is greater than {} and {} ".format(x,y,z))
else :
if y >= x and y >= z :
print("{} is greater than {} and {}".format(y,x,z))
else :
print("{} is greater than {} and {}".format(z,x,y))
Enter x : 45
Enter y : 67
Enter z : 12
67 is greater than 45 and 12
if-elif-else
Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax
Example 6:
if (condition):
statement:
elif (condition):
statement:
else:
statement:
x = int(input("x : "))
y = int(input("y : "))
z = int(input("z : "))
if x >= y and x >= z :
if x == y :
print("{} is equals to {} and greater {}".format(x,y,z))
elif x == z :
print("{} is equals to {} and greater {}".format(x,z,y))
else :
print("{} is greater than {} and {}".format(x,y,z))
elif y >= x and y >= z :
if x == y :
print("{} is equals to {} and greater {}".format(y,x,z))
elif y == z :
print("{} is equals to {} and greater {}".format(y,z,x))
else :
print("{} is greater than {} and {}".format(y,x,z))
else :
if z == y :
print("{} is equals to {} and greater {}".format(z,y,x))
elif z == x :
print("{} is equals to {} and greater {}".format(z,x,y))
else :
print("{} is greater than {} and {}".format(z,x,y))
x : 45
y : 67
z : 12
67 is greater than 45 and 12
Example 7:
#Greatest among Three numbers
a = int(input("A : "))
b = int(input("B : "))
c = int(input("C : "))
if a >= b :
if a >= c :
print("A is greatest ")
else :
print("C is greatest ")
elif b >= c :
print("B is Greatest")
else :
print("C is Greatest ")
A : 45
B : 68
C : 93
C is Greatest
Example 8:
#calculator
x = int(input("X : "))
y = int(input("Y : "))
print("Choose one of these : ")
print("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division")
print("5.Modules\n6.Floor Division\n7.Exponent(Power)")
choice = int(input("Your Choice : "))
if choice == 1 :
print("Addition of {} and {} is {}.".format(x,y,x+y))
elif choice == 2 :
print("Subtraction of {} and {} is {}.".format(x,y,x-y))
elif choice == 3 :
print("Multiplication of {} and {} is {}.".format(x,y,x*y))
elif choice == 4 :
result = "%.2f"%(x/y)
print("Division of {} by {} is {}.".format(x,y,result))
elif choice == 5 :
print("Modules of {} by {} is {}.".format(x,y,x%y))
elif choice == 6 :
print("Floore Division of {} by {} is {}.".format(x,y,x//y))
elif choice == 7 :
print("Exponent (Power) of {} to the {} is {}.".format(x,y,x**y))
else :
print("Error : Invalid Choice ")
X : 45
Y : 67
Choose one of these :
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modules
6.Floor Division
7.Exponent(Power)
Your Choice : 1
Addition of 45 and 67 is 112.
Example 9:
#Game Rock Paper Scissor
from getpass import getpass #to take hiddin inputs works as input function
from time import sleep #used to delay the program by seconds
print("Welcome to Rock Paper Scissor Program ")
sleep(1)
print("Enter Choice as ")
sleep(1)
s = "rock,paper,scissor"
print(s.center(80,'*'))
sleep(1)
p1 = getpass("Player1 : ").strip()
p1 = p1.lower()
if p1 == 'rock' or p1 == 'paper' or p1 == 'scissor' :
pass
else :
print("Error!!Invalid Choice")
exit(0)
sleep(1)
p2 = getpass("Player2 : ").strip()
p2 = p2.lower()
if p2 == 'rock' or p2 == 'paper' or p2 == 'scissor' :
pass
else :
print("Error!!Invalid Choice")
exit(0)
sleep(1)
s = "Processing"
print(s.center(100,"*"))
sleep(2)
print("Player1 Choice was : {} ".format(p1.upper()))
print("Player2 Choice was : {} ".format(p2.upper()))
s = "Processing Result"
print(s.center(100,'*'))
sleep(2)
if p1 == p2 :
print("\n\n")
print("Match is Tie",s.center(100,'#'))
print("\n\n")
elif ( p1 == 'rock' and p2 == 'scissor' ) or ( p1 == 'paper' and p2 == 'rock' ) or ( p1 == 'scissor' and p2 == 'paper') :
print("\n\n")
print("Player1 is The Winner".center(100,'#'))
print("\n\n")
else :
print("\n\n")
print("Player2 is The Winner".center(100,'#'))
print("\n\n")
sleep(2)
print("Bye Bye".center(100,'*'))
sleep(2)
Output:
Welcome to Rock Paper Scissor Program
Enter Choice as
*******************************rock,paper,scissor*******************************
Player1 : ········
Player2 : ········
*********************************************Processing*********************************************
Player1 Choice was : PAPER
Player2 Choice was : ROCK
*****************************************Processing Result******************************************
#######################################Player1 is The Winner########################################
**********************************************Bye Bye***********************************************