Python provides the statement if..elif..else
conditional in order to evaluate conditions and branching the program executions. The then keyword is not a python keyword but used in other programming languages. Simply the statement if..then
is equal to single if conditional in Python. The statement if
, if..else
are used to check different conditions. They can be used to check if specified values or objects are equal, not equal, less than, less than or equal, greater than, greater than or equal to, etc.
Single if Conditional or if..then
The single if conditional is equal to the if..then. So we will just check the given condition and if it is True
we will execute the if code block. If it is False
there will be nothing. In the following example, we check if the specified age value is greater than 18. If the specified age value is greater than 18 the if statement code block is executed which simply prints “Over 18” to the terminal. If the age value is not greater than 18 the condition does not meet and the if statement code block is not executed.
age = 20
if age>18:
print("Over 18")
Nested if Conditional or Nested if..then
Also, the if or if..then conditional can be used as a nested form where multiple if or if..then can be chained together. In the following nested if..then the age and country will be checked in a nested way if age is over 18 and country is equal to “TR”.
age = 20
country = "TR"
if age>18:
if country == "TR":
print("Over 18 From TR")