CoachFullStack

Python Guide #7: If Statements

Python, as most other programming languages, enables you to control flow of program execution with the help of if statements.

If statements are code structures, that execute code within the block, if the condition returns True.

If you don’t know how conditions work, go to Logical Conditions in Python

if condition_1:
    # instruction 1
elif condition_2:
    # instruction 2
    # it is executed, if
    # previous statement
    # was False
elif condition_3:
    # instruction 3
    # it is executed, if
    # previous statement
    # was False
else:
    # instruction 4
    # executed, if no
    # conditions in previous
    # blocks were met

Lets use some example

a = 10
b = 30

if a > b:
    print("a is greater than b")
elif a < b:
    print("a is smaller than b")
else:
    print("a is equal to be")

Execute the program above and see what happens, then modify values a and b and check again.



Next: Python Guide #8: Loops

Previous: Python Guide #6: Logical Conditions