“It has to be chocolate, else I won’t accept it!”. If you have said something similar to it at any point in your life, it was said because you had to make an important decision that is meaningful to you. Similarly, in Python, some important decision has to be made when a condition is met. Python if…else is one of the conditional aids that you use to make decisions while Coding.
On this page you’ll learn the following:
- What is Python if statement?
- What is the Python else statement?
- Use is the Python if…else statement?
- The Python Elif Statement
- Python Nested if statement
Three minutes from now you’ll have complete knowledge of the famous Python if…else statement.
What is Python if…else statement?
Before we delve in, let’s get the formation of the “if…else”
The if…else Statement is the joining of two Python distinct conditional statements which are If and else
Python if Statement
The python if statement executes a statement if, and only if a condition is met, otherwise, no execution will be made. In computer tense, python Identifies any non “0” number (1 to 9) as True. and any number less than 1 is taken as False.
The syntax for the python if statement is: if test expression:
statement(s)
Example of Python if Statement
Python Input:
# If the number is positive, we print an appropriate message num = 5 if num > 0: print(num, "is a positive number.") print("This is always printed.") num = -3 if num > 0: print(num, "is a positive number.") print("This is also always printed.")
Output: 5 is a positive number This is always printed This is also always printed.
Python else statement
The python else statement serves as a backup to the if statements. The else statement is used to execute a conditional program when the condition of If proves False.
Example of the else execution
Python Input:
x = 10 try: x > 20 except: print("Something went wrong") else: print("The 'Try' you are all good no errors!")
Output: The 'Try' you are all good, no errors!
From the above returns, the else command executed the else statement: The ‘Try’ you are all good, no errors!
“What is the if…else now?”
What is Python if else Statement?
The python if…else statement is a conditional operation in Python that executes the If code block when the True condition is met. If the False condition comes into play, the else code block will execute.
The syntax to run the of…else statement is: if expression:
statement(s) else: statement(s)
For example, I want to program a machine that will automatically turn on the fire extinguisher if the room temperature rises above 27°C. My code will look like this:
Python Input:
house_temp = 28.9 If house_temp > 27.2 Print("Auto extinguishing 360°") else: Print("you're safe,house_temp is normal") Of my house_temp rise above 27.2°C our Output will be like this:
Output: Auto extinguishing 360
If my house temp is below 27°C, the else will execute: you’re safe, house_temp is normal
Another example is the voter eligibility checker
Python Input:
age = int (input("Enter your age? ")) if age>=18: print("You are eligible to vote !!"); else: print("Sorry! you have to wait !!");
Output: Enter your age? 50 You are eligible to vote !!
If the papa above was below eighteen, the else(“Sorry you have to wait!!”) Would have been re-sorted.
Interestingly, There’s a middle condition that checks if a statement is True before backing down for the else statement to be executed. That takes us to the next condition
The Python Elif Statement
The Python Elif Statement is short for else if statement, and usually written if…elif…else is used to check for multiple expressions. When all the condition of If proves False, it checks the condition of the next elif multiple times. If the condition of the Elif proves False, it executes the Else. It’s a way of saying, “if the first condition isn’t accepted, try this one before we conclude”.
The syntax for the if…elif…else statement is like this: if expression1 :
statement_1 statement_2 .... if expression2 : statement_3 statement_4 .... if expression3 : statement_5 statement_6 .................... else : statement_7 statement_8
The if…elif…else checks multiple conditions. Python will read each block under the Elif one by one if the if statement wasn’t true. If python couldn’t find the True statement in all the Elif blocks, it executes the one in the else block.
Example:
Python Input:
var = 10 if var == 20: print "1 - Got a true expression value" print var Elif var == 15: print "2 - Got a true expression value" print var Elif var == 10: print "3 - Got a true expression value" print var else: print "4 - Got a false expire session value" print var print "Goodbye!"
Output: 3 - Got a true expression value 100 Goodbye!
From the above, if none of the Elif carries the True statement, it would have returned the Else statement (“Got a false expression value”).
Python Nested if Statement
If you are wondering what the word nested is all about in Python, it’s a computer term that connotes layering(like onion layers) or sub-values. In Python an if…elif…else Statement can be inside another if…elif…else statement.
However, nesting the if…elif…else statement can be pretty confusing, it’s strongly advised to avoid it if possible. Hopefully, the way to figure out the nesting Level is by indentation method
Example:
Python Input:
num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")
Output: Enter a number: 10 Positive number Enter a number: -17 Negative number Enter a number: 0 Zero
In the above program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message.
We’ll use a nested if statement
Summary
The python if statement executes statements if, and only if the condition holds, otherwise, no execution will be made.
The python else statement serves as a backup to the if statements. The else statement is used to execute a conditional program when the condition of if proves False.
if…elif…else is used to check for multiple expressions. If the condition of if proves False, it checks the condition of the next elif multiple times.
When all the conditions of the elif prove False, it executes the else.
However, nesting the if…elif…else statement can be pretty confusing, it’s strongly advised to avoid nesting the elif if possible. Hopefully, the only way to figure out the nesting Level is by indentation
In this chapter you’ve learned:
- What is Python if statement?
- What is the Python else statement?
- Use is the Python if…else statement?
- The Python Elif Statement
- Python Nested if statement
Give it all the needed effort. Good luck Coding!