Python Conditions
In this tutorial you will learn:
- Conditional statements
- Indentation in Python
- If statement in Python
- Else statement in Python
- Elif statement in Python
- Nested if statements in Python
Conditional statements
Conditional statements as the name suggests define some kind of condition before an action is taken. In terms of programming conditional statements constrains whether a part of code should be executed or not. These decision making statements are an integral part of any programming language as most of the time we don’t want all parts of our code to run at once rather we wan’t some parts to execute if certain conditions are satisfied.
Indentation in Python
Indentation is empty space before the code starts. It’s important to learn that in Python indentation matters a lot unlike other programming languages. In Python indentation indicates a block of code while in other programming languages it’s there just for code readability.
If statement in Python
This statement will only be executed if the result of a condition is true. It doesn’t execute in case of false condition.
For example:
myVar1 = 45
myVar2 = 88
print("Output Result")
if myVar1 > myVar2: #condition evaluates to false so won't execute
print(“This statement won't be executed")
if myVar1 myVar2: #condition evaluates to true so will execute
print("45 is less than 88")
Another good example of if statements is to use and, or and equality operator in if statement
myVar3 = 98
myVar4 = 98
print("\nOutput Result")
if myVar3 == myVar4:
print("Numbers are equal")
var1 = 3
var2 = 5
var3 = 9
print("\nUsing and in if")
if var1 var2 and var2 var3:
print(" 3 is less than 5 is less than 9")
print("\nUsing or in if")
if var1 var2 or var2 > var3:
print(" 3 is less than 5 but 5 is not greater than 9")
Else statement in Python
This statement will only be executed if the result of a condition doesn’t satisfy any other condition. Note that in the code above the first print statement isn’t executed and in this case we could have used else statement to show the user the right output. So let’s re write the above program with else statement.
myVar1 = 45
myVar2 = 88
print("Output Result")
if myVar1 > myVar2: #condition evaluates to false so won't execute
print("This statement won't be executed")
else:
print("45 is less than 88”)
The output using else statement is the same in this case. Another example is using multiple if statements and then using the else statement.
a = 10
b = 5
print("\nOutput Result using else")
if a == b:
print("", a, "is less than", b)
if a > b:
print("", a, "is greater than", b)
else:
print("",a, "is equal to", b)
Elif statement in Python
This statement is commonly known as else if statement in most the programming languages. This statements is used to provide an alternate option to the if statement. If the if statement is false then go to else if and if it’s false as well then go to the next elif and then finally to else. It’s different from using subsequent if statements because elif code will only be executed if the condition above it evaluates to false. If we use multiple if statements instead then each if condition will be executed regardless whether the first condition is true or false.
myVar1 = 45
myVar2 = 88
print("Output Result")
if myVar1 > myVar2: #condition evaluates to false so won't execute
print(myVar1, "is greater than ",myVar2)
elif myVar1 myVar2: #condition evaluates to true so will execute
print(myVar1, "is less than ", myVar2)
else:
print(myVar1, "is equal to ", myVar2)
Nested if statements
Nested if statements are used when we want to satisfy multiple conditions subsequently after one condition is satisfied. You can do nesting in depth but it’s not recommended better approach is to use and, or and not operators to keep nesting level to a minimum.
name = "Jack"
age = 19
print("Output Result")
if age >= 18:
if name == 'Jack':
print('Yes you use this car')
else:
print("Sorry you can't use this car")
print("\nAnother solution")
if age >= 18 and name == 'Jack':
print('Yes you use this car')
else:
print("Sorry you can't use this car")
Add new comment
- 326 views