Python If and Else Statements A Full Guide
Python If and Else statements are used to control the flow of our program. using this structure we place some conditions and implement a block of coding style, basically, we have two blocks 1st If block and 2nd Else Block. let us take some problem statements to understand the concept of the If-Else Statement.
Problem Statement: if an employee’s salary is greater than 50000 then he/she will get a 15% bonus on basic salary else they will get a 10% bonus.
name=input("please enter name of employee") bs=int(input("please enter basic salary of an employee")) sal=0 if bs > 50000: bonus=bs*0.15 sal=bs+bonus print(name," ","final salary is=",sal) else: bonus=bs*0.10 sal=bs+bonus print(name," ","final salary is=",sal)
Description: First we are taking the name and basic salary as input from the user then our program will go inside if block to check the condition bs > 50000 or not so if this condition is true say ( 60000>50000) then if block will execute and else block will not execute and if the condition is false (30000 >50000) the else block will execute and if block will not execute. this is the way we are controlling the structure of our program using Python If and Else statements.
Problem Statement: Separate even and odd numbers in the different lists from the main list.
li=[1,2,3,4,5,6,7,8,9,11,22,23,35,2,7] even= odd= for item in li: if item%2==0: even.append(item) else: odd.append(item) print("even no=",even) print("odd no=",odd)
Description: First we are taking a default list of various number and defining two blank list one for even numbers and 2nd for odd number then for loop is getting all number from the li list and placing them to item variable and item%2==0 is the condition that is checking a number is even of odd so if this condition is true then we are appending item in even list else we are appending item inside the odd list. finally, we are printing both even and odd lists.
Problem Statement: Take 3 numbers from users and find which one is the greatest number among the three.
num1=int(input("please enter 1st number")) num2=int(input("please enter 2nd number")) num3=int(input("please enter 3rd number")) if num1>num2 and num1>num3: print("1st number is greater") elif num2>num1 and num2>num3: print("2nd number is greater") else: print("3rd number is greater")
Description: This is 2nd style of block-oriented programming where we can use multiple elif statements to define multiple conditions in our program as you can see inside 1st if block we are checking condition-1 that is if num1>num2 and num1>num2 the obviously num1 is the greatest number but now again we want to define some more conditions so we can define elif statement where we are checking condition-2 and final else statement will execute if both conditions-1and condition-2 both are not true.
Problem Statement: Print the below pattern using an if-else statement.
for i in range(1,5): for j in range(1,11): if i>1 and i<4 and j>1 and j<10: print(" ",end=" ") else: print("*",end=" ") print()
Problem Statement: Check whether a number is prime or not inside a given list of numbers.
li=[11,17,22,30,2,1,1,22,17,3,97,89,90] for data in li: num=data count=0 for z in range(0, num): # all prime numbers are greater than 1 if num > 1: for i in range(2, z): if (num % i) == 0: count=count+1 break if count==0: print(num,"= is a prime no") else: print(num,"= is not a prime no")