Python If ... Else
Python Conditions and If statements
The common logical conditions from mathematics is supported by Python :
- 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
Those conditions are most common in 'if statements' and loops to be used in various ways.
The IF Statement
The if keyword is used to executes a single block of code, if given conditions are true.
Example :- Simple If statement :
a = 33
b = 200
if b > a:
print("b is greater than a")
Output :-
In this example, two variables, a and b, are used to test whether b is greater than a, as part of the if statement. a is 33 and b is 200 and we know that 200 is larger than 33 so we print "b is larger than a" onto the screen.
Indentation
In order to define the scope, Python relies on indentation (whole space at the start of a line). For this purpose, other languages frequently use curly-brackets.
Example :- If an indentation is not provided (an error will be raised) :
a = 33
b = 200
if b > a:
print("b is greater than a")
Output :-
print("b is greater than a")
^
IndentationError: expected an indented block
Elif
The elif keyword is used to executes a single code block from multiple block of codes.
The keyword of elif is pythonic: "Try this condition if previous conditions have not been met".
Example :-
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Output :-
a is equivalent to b, so the first condition is not true but the elif condition is true and therefore we print to screen the following: "a and b are the same".
Else
Anything else that is not covered by the conditions above can be found.
Example 1 :-
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Output :-
a is greater than b, in this example, so the initial condition is not true, also the condition of the elif is not true, so we take the else condition and print to screen "a is greater than b".
Example 2 :- Without the elif, you can have else :
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output :-
Related Links
Short Hand If
You can place it on the same line as if you only have one statement to perform.
Example 1 :- One line if statement :
if a > b: print("a is greater than b")
Output :-
Short Hand If ... Else
If you only have one declaration, one if, and one for others to execute, you can all put it on the same line :
Example 1 :- One line if else statement :
a = 2
b = 330
print("A") if a > b else print("B")
Output :-
This technique is referred to as Ternary operators, or conditional statements.
On the same line you can also make several else statements :
Example 2 :- One line if else statement, with 3 conditions :
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
Output :-
And
The keyword and the logical operator is used to combine the conditions :
Example :- Test if, a is greater than b, AND if c is greater than a :
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Output :-
Related Links
Or
The keyword or is a logical operator and used for combining conditions :
Example :- Test if a is greater than b, OR if a is greater than c :
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Output :-
Nested If
if statements are inside, this is called statements nested if declarations are inside.
Example :-
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Output :-
and also above 20!
The pass Statement
if statements cannot be empty but if you have an if statement without content for any reason, put the pass statement in order to avoid error.
Example :-
a = 33
b = 200
if b > a:
pass
Note:- having an empty if statement like this, would raise an error without the pass statement.