Python If Statement :
The ‘If’ statement is a conditional statement that is used to determine whether or not a specific expression is true. The program control first checks the condition written with’if ‘, and if the condition is true, the if block is executed. Otherwise, the program control is transferred to the else block and executed.
Syntax:
if(condition): block code 1 else: block code 2
If the condition is met, block code 1 is executed. If this is not the case, block code 2 is executed.
We all use a basic if statement, which is an if statement with only one condition. This is used to compare one variable to another or to determine whether a variable is true or false.
For Example:
gvn_num = int(input("Enter some random number = ")) # condition to check whether the given number is even or odd. if((gvn_num % 2) == 0): print("The given number is an Even Number") else: print("The given number is an Odd Number")
Output:
Enter some random number = 10 The given number is an Even Number
Multiple Conditions in If Statement
Multiple conditions can be used in a single if statement by using AND, OR, or BOTH.
Syntax:
if ((condition1) AND/OR (condition2)) : Â Â block code 1 else : Â Â block code 2
Program for Multi-Conditional If Statement
Method #1: Using If statement (Static Input)
1)Using AND
Approach:
- Give the age as static input and store it in a variable.
- Give the experience as static input and store it in another variable.
- Check if the given age is greater than 25 and less than 55 and experience is greater than 3 using the if conditional statement.
- If it is true, then print “congrats you are selected”.
- Else print “You are not qualified sorry”.
- The Exit of the Program.
Below is the implementation:
# Give the age as static input and store it in a variable. gvn_age = 32 # Give the experience as static input and store it in another variable. gvn_expernce = 5 # Check if the given age is greater than 25 and less than 55 and experience is # greater than 3 using the if conditional statement if (gvn_age > 25 and gvn_age < 55) and (gvn_expernce > 3): # If it is true, then print "congrats you are selected". print("congrats you are selected") else: # Else print "You are not qualified sorry". print("You are not qualified sorry")
Output:
congrats you are selected
2) Using OR
When you want at least one condition to be satisfied, you use the OR condition.
Approach:
- Give the number as static input and store it in a variable.
- Get the remainder by dividing the given number by 10 and store it in another variable.
- Check if the above-obtained remainder is equal to 0 or 5 using the if conditional statement and or condition.
- If it is true, then print “The given number is divisible by 5”.
- Else, print “The given number is NOT divisible by 5”.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_num = 45 # Get the remainder by dividing the given number by 10 and store it in another # variable. remaindr = gvn_num % 10 # Check if the above-obtained remainder is equal to 0 or 5 using the if # conditional statement and or condition. if (remaindr == 0) or (remaindr == 5): #If it is true, then print "The given number is divisible by 5". print("The given number{", gvn_num, "} is divisible by 5") else: #Else, print "The given number is NOT divisible by 5". print("The given number{", gvn_num, "} is NOT divisible by 5")
Output:
The given number{ 45 } is divisible by 5
Method #2: Using If statement (User Input)
1)Using ANDÂ
Approach:
- Give the age as user input using the int(input()) function and store it in a variable.
- Give the experience as user input using the int(input()) function and store it in another variable.
- Check if the given age is greater than 25 and less than 55 and experience is greater than 3 using the if conditional statement.
- If it is true, then print “congrats you are selected”.
- Else print “You are not qualified sorry”.
- The Exit of the Program.
Below is the implementation:
# Give the age as user input using the int(input()) function and store it in a variable. gvn_age = int(input("Enter some random number(age) = ")) # Give the experience as user input using the int(input()) function and # store it in another variable. gvn_expernce = int(input("Enter some random number(experience) = ")) # Check if the given age is greater than 25 and less than 55 and experience is # greater than 3 using the if conditional statement if (gvn_age > 25 and gvn_age < 55) and (gvn_expernce > 3): # If it is true, then print "congrats you are selected". print("congrats you are selected") else: # Else print "You are not qualified sorry". print("You are not qualified sorry")
Output:
Enter some random number(age) = 22 Enter some random number(experience) = 3 You are not qualified sorry
2) Using OR
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Get the remainder by dividing the given number by 10 and store it in another variable.
- Check if the above-obtained remainder is equal to 0 or 5 using the if conditional statement and or condition.
- If it is true, then print “The given number is divisible by 5”.
- Else, print “The given number is NOT divisible by 5”.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_num = int(input("Enter some random number = ")) # Get the remainder by dividing the given number by 10 and store it in another # variable. remaindr = gvn_num % 10 # Check if the above-obtained remainder is equal to 0 or 5 using the if # conditional statement and or condition. if (remaindr == 0) or (remaindr == 5): #If it is true, then print "The given number is divisible by 5". print("The given number{", gvn_num, "} is divisible by 5") else: #Else, print "The given number is NOT divisible by 5". print("The given number{", gvn_num, "} is NOT divisible by 5")
Output:
Enter some random number = 28 The given number{ 28 } is NOT divisible by 5