If Statement
Python supports the usual logical conditions from mathematics:
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
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
You can also have an else
without the elif
:
Short Hand If
Short Hand If ... Else
logical operators
And
The and keyword is a logical operator, and is used to combine conditional statements:
Or
Nested If
The pass Statement
if
statements cannot be empty, but if you for some reason have an if
statement with no content, put in the pass
statement to avoid getting an error.
Last updated