In any programming language, we will have few cases where we have to decide based on input to choose one of the paths. This is achieved using conditional statements or (if..else) blocks.

Conditional statements (conditionals) are a way to make decisions. It offers opportunity to have multiple options for your program flow.

We can use if...else structure for different decisions. The structure of this conditional code blocks look like this.

1if condition:
2    # ... .. ...
3    # perform some actions
4    # ... .. ...
5else:
6    # ... .. ...
7    # perform other actions
8    # ... .. ...

If you’re coming from different programming language background, Python uses indentation for defining code blocks. Unlike other languages it does not use set of curly braces ({}) for this. So, in case of Python on line 1 above, if condition: starts a new block and everything within that if block is indented by 4 spaces. The number of spaces to indent can be whichever developer prefers but 4 spaces is the convention and it has to be consistent in a code file. This convention also helps in code readability.

Practical Example

Let’s take a very trivial example of if..else block.

1age = 10
2if age > 18:
3    print("You can drive")
4else:
5    print("You can't drive yet.")

In above code, we define variable on line 1. On line 2, we check if age variable is above 18. Depending on the value assigned to age variable, we print whether this person can drive or not.

You can't drive yet.

We can even skip else block if it’s not necessary.

1age = 22
2if age > 18:
3    print("You can drive")
4print("End of If Block")

Output

You can drive.
End of If Block

This if..else construct is useful for situations where we have either or kind of situation. However, if we have to decide between more options, we can also specify more conditions using if..elif..else construct.

1num = -2
2if num > 0:
3    print("It's a positive number")
4elif num < 0:
5    print("It's a negative number.")
6else:
7    print("It's zero")

In above example, we have total three different options.

It's a negative number.

If we have more than two choices, the order in which we specify these conditions do matter. Basically, this conditional block will execute the first block where the condition holds True even though there may be other blocks which may be True. Consider below example, in this case, the student has got 88 marks and we should ideally get a message saying “You’ve got A!”. However, when we execute, it prints incorrect grade

 1marks = 88
 2if marks < 35:
 3    print("Sorry, you've failed.")
 4elif marks > 35:
 5    print("You've got D grade.")
 6elif marks > 60:
 7    print("You've got C!")
 8elif marks > 75:
 9    print("Nice! You've got B grade.")
10else: 
11    print("Congratulations, You've got A!")

Output

You've got D grade.

In order to fix this, we have to reshuffle the order in which the conditions are specified as shown below.

 1marks = 88
 2if marks < 35:
 3    print("Sorry, you've failed.")
 4elif marks > 85:
 5    print("Congratulations, You've got A!")
 6elif marks > 75:
 7    print("Nice! You've got B grade.")
 8elif marks > 70:
 9    print("You've got C!")
10else: 
11    print("You've got D grade.")
Congratulations, You've got A!

We can also nest these if..else statements and create complex conditional logic. Basically inside if or else block, we can have additional if..else conditional checks.

Conditionals with None

In earlier lesson, we learned that None is not 0, False or empty string. Let’s test those out.

1none_value = None
2empty_string = ""
3false_value = False
4zero_value = 0
5print(f"none_value == empty_string: {none_value == empty_string}")
6print(f"none_value == false_value: {none_value == false_value}")
7print(f"none_value == zero_value: {none_value == zero_value}")
8print(f"none_value == None: {none_value == None}")
9print(f"none_value is None: {none_value is None}")

Output

none_value == empty_string: False
none_value == false_value: False
none_value == zero_value: False
none_value == None: True
none_value is None: True

As you can see from the output, None value can be compared with another None value will return True otherwise it will return False.

Ternary operator

Python also provides Ternary operator which reads like a plain English and can be used to assign values to a variable.

1age = 17
2is_adult = "Yes" if age > 18 else "No"
3print(is_adult) # No

In above example, is_adult will be assigned No because age variable has value less than 18.

Pass keyword

Whenever we create a new block using indentation, we have to fill that block. The block cannot be left empty. If we leave the block empty, it will give SyntaxError. There may be scenario where you have not decided on the implementation of the block of code. In such cases, we can use pass keyword to skip that block and Python interpreter will not give SyntaxError if we write pass keyword. This can be useful for even function implementations which you may want to fill in later. Below is the syntax for using this keyword.

1if 1 == 1:
2    pass