Python – Using If Statement
If statement in Python is used to control the flow of execution within a program based on some condition, evaluated to be True or False.
Comparison operator and logical operator are used to evaluate condition.
A comparison operator is an operator that performs some form of test and returns True of False.
Logical operators can be used to combined Boolean expressions together. They are used with comparison operators to create more complex conditions. Three logical operators in Python are listed below:
If Statement in Python
Basic form of if-statement is as below,
if <condition-evaluating-to-boolean>: statement
The condition must evaluate to True or False. If the condition is True then we will execute the indented statement. For example,
num = int(input('Enter a number: ')) if num < 0: print(num, 'is negative')
If user inputs -1, the output of above snippet will be as below,
-1 is negative
Else in an If Statement
We can also define an else part of an if statement; this is an optional element that can be run if the conditional part of the if statement returns False. For example:
num = int(input('Enter yet another number: ')) if num < 0: print('Its negative') else: print('Its not negative')
If the user inputs 1, the output of above code snippet will be as below,
Enter yet another number: 1 Its not negative
The Use of elif
In some cases there may be several conditions you want to test, with each condition being tested if the previous one failed. This else-if scenario is supported in Python by the elif element of an if statement. For example,
savings = float(input("Enter how much you have in savings: ")) if savings == 0: print("Sorry no savings") elif savings < 500: print('Well done!') elif savings < 1000: print('Good Job!') elif savings < 10000: print('Excellent!') else: print('Thank you')
If the user inputs 500, the output of above code snippet will be as below,
Enter how much you have in savings: 500 Well done!
When one or more boolean condition are grouped in If statement in python, it is called If-expression.
Let’s see an basic example using if-statement,
age = 15 status = None if (age > 12) and age < 20: status = 'teenager' else: status = 'not teenager' print(status)
Output of above code snippet will be as below,
However, this is quite long and it may not be obvious that the real intent of this code was to assign an appropriate value to status.
An alternative is an if expression. The format of an if expression is <result1> if <condition-is-met> else <result2>. For example,
status = ('teenager' if age > 12 and age < 20 else 'not teenager') print(status)
Again, the result printed out is ‘teenager‘ however now the code is more concise and clear that the purpose of the test is to determine the result to assign to status.
A Note on True and False
Python is actually quite flexible when it comes to what actually is used to represent True and False, in fact the following rules apply
- 0, ” (empty strings), None equate to False
- Non zero, non empty strings, any object equate to True.
However, we would recommend sticking to just True and False as it is often a cleaner and safer approach.
In this article, we saw about comparison, logical operator’s use in forming if-statement, use of elif -statement and about If-expression.