Conditional Statements in Python#

Python is processing statements that are similar to other languages like C, C++, Java, etc. The conditional statements are used to execute a particular section of code based on certain conditions to change the flow of the program. To control the flow of the program, we use conditional statements in Python. Conditional statements are also known as selection statements. The conditional statements are if, if-else, if-elif-else, nested if, and nested if-else. Python supports the usual logical conditions from mathematics. These conditions are used in several ways, most commonly in “if statements” and loops:

  • Equals: a == b

  • Not Equals: a != b

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

The most basic form of the if statement is shown below where the condition is evaluated, and if the condition is true, the statement is executed. If the condition is false, the statement is skipped.

Example about using if-then#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 33
 5    b = 200
 6    if b > a:
 7        print("b is greater than a")
 8
 9
10if __name__ == "__main__":
11    main()

The output of the above code is that b is greater than a is printed to the screen. The condition is evaluated, and since b is greater than a, the statement is executed on the next line. This statement is indented by four spaces to indicate a block of code that is executed if the condition is true. In the example below the indentation is incorrect, and the code will not run.

Example about indentation#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 33
 5    b = 200
 6    if b > a:
 7    print("b is greater than a") # you will get an error
 8
 9
10if __name__ == "__main__":
11    main()

The example above shows that indentation is important and that the flow of the program is controlled by the indentation. In the example below the indentation is correct, and the code will run. Only the first print statement is executed if the condition is true and the second print statement is executed regardless of the condition.

Example about indentation#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 33
 5    b = 200
 6    if b > a:
 7        print("b is greater than a")
 8    print("Hello World")
 9
10
11if __name__ == "__main__":
12    main()

Using if-then-else within Python#

With if-then a statement is executed if the condition is true. With if-then-else a statement is executed if the condition is true, and another statement is executed if the condition is false. The example below shows the if-then-else statement.

Example about if-then-else#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 200
 5    b = 33
 6    if b > a:
 7        print("b is greater than a")
 8    else:
 9        print("b is not greater than a")
10
11
12if __name__ == "__main__":
13    main()

The output of the above code is that b is not greater than a is printed to the screen. The condition is evaluated, and since b is not greater than a, the statement is executed on the next line. This statement is indented by four spaces to indicate a block of code that is executed if the condition is true. In the example below the indentation is incorrect, and the code will not run.

Nested if-then-else#

The if-then-else statement is used to execute a block of code if the condition is true and another block of code if the condition is false. A common pattern is to have a series of if-then-else statements. The example below shows a nested if-then statement in then-section of an if-then-else statement.

Example about nested if-then-else#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 33
 5    b = 33
 6    if b > a:
 7        print("b is greater than a")
 8    else:
 9        if a == b:
10            print("a and b are equal")
11        else:
12            print("a is greater than b")

if-then-elif#

The example above can also be written as shown below with the if-then-elif-else statement which is a shorter version of the nested if-then-else statement and makes the code easier to read.

Example how to use if-then-elif-else#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 33
 5    b = 33
 6    if b > a:
 7        print("b is greater than a")
 8    elif a == b:
 9        print("a and b are equal")
10    else:
11        print("a is greater than b")
12
13if __name__ == "__main__":
14    main()

Logical operators#

Logical operators are used to combine conditional statements. The logical operators are and, or, and not. The example below shows the and operator what otherwise should be two if statements to test if both conditions are True.

Example about AND operator#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 200
 5    b = 33
 6    c = 500
 7    if a > b and c > a:
 8        print("Both conditions are True")
 9
10
11if __name__ == "__main__":
12    main()

The same can be done with the or operator. The example below shows the or operator what otherwise should be two if statements to test if at least one of the conditions is True.

Example about OR operator#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 200
 5    b = 33
 6    c = 500
 7    if a > b or a > c:
 8        print("At least one of the conditions is True")
 9
10
11if __name__ == "__main__":
12    main()

A complete overview of the logical operators and the rules can be found in Boolean operators basics.

Shorthand and Conditional Expressions#

Python has a shorthand syntax for if-then-else statements. The example below shows the shorthand syntax fo the if-then statement.

Example#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 2
 5    b = 330
 6    if a > b: print("a is greater than b")
 7
 8
 9if __name__ == "__main__":
10    main()

Besides the shorthand syntax for if-then statements, Python also supports the ternary conditional operator what can be used to assign a value to a variable based on a logical expression. The example below shows the ternary conditional operator and if very similar to the if-then-else statement.

Example of ternary conditional operator#
 1#!/usr/bin/env python3
 2
 3def main():
 4    a = 2
 5    b = 330
 6    print("A") if a > b else print("B")
 7
 8
 9if __name__ == "__main__":
10    main()

The ternary conditional operator can be used in a nested form. The example below shows the nested ternary conditional operator, but here directly it shows that the readability of the code is not very good and is not advised to use.

Example of nested ternary conditional operators#
1#!/usr/bin/env python3
2
3def main():
4    b = 330
5    print("A") if a > b else print("=") if a == b else print("B")
6
7
8if __name__ == "__main__":
9    main()