Python If ... Else

Python If ... Else


You can also search for these topics, python if else syntax, python if else syntax continue, between two number using python if else, python if else syntax brackets, how to use code if else using python, syntax how to use and short hand, python working of if...else statements.

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 :-

b is greater than a

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.

You can also search for these topics, python condition and if statements append, if statements contains python, example for python condition and if statements, check the key error in if statements using python, python practice problem conditions and if statements.

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 :-

File "demo_if_error.py", line 4
print("b is greater than a")
^
IndentationError: expected an indented block

You can also search for these topics, python if else indentation condition, python if else indentation difference, definition for indentation in python if else, indentation doesn't work using python, check the indentation error with python if else, define the identation key in python, how to python replace identation, python validation if else statement.

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 and b are equal

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".

You can also search for these topics, python elif invalid syntax, elif error in python, elif not equal in python, define python condition elif, python elif statement multiple condition, how to use alternative python condition using python, python apply elif, what is the code of python elif codition, continue the python elif, python elif default value, example for python elif condition.

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

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 :-

b is not greater than a



You can also search for these topics, python else invalid syntax, python else without if, how to count the python Else statement, else doesn't work in python, check the error in python else statement, how to end the python else program, else keyword in python, Example for python else statement.

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 :-

"a is greater than b"

You can also search for these topics, python short hand if assignment, check the python shorthand if, python does the short hand if, keywords python short hand if, find the python missing short hand if, how to return the short hand if using python, Example for short hand if using python.

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 :-

B

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 :-

=

You can also search for these topics, python short hand if else statement, short hand if else before in python, how to get value of python short hand if else statement, how to use the python short hand if else, python keywords with short hand if else statement, check the python property and perfomance using short hand if else, Example for short hand if else in python.

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 :-

Both conditions are True



You can also search for these topics, python And operator, And big data using python, python And bash, python And condition, python And data analytics, And operator python function, use of python And logical operator, Example for python And operator.

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 :-

At least one of the conditions is True

You can also search for these topics, python Or condition, python Or operator, python Or but not both, check the bytes in python Or operator, python Or bitwise, example for python Or operator, check the evaluation of Or operator, check the python invalid syntax, Example for python Or operator.

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 :-

Above ten,
and also above 20!

You can also search for these topics, python nested if statement, how to use the nested if condition, nested if statement example in python, python continue nested if, how to use python nested if, nested if methods using python, how to use main function with nested if, check the working of python nested if, example for python nested if statement.

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.

You can also search for these topics, what is the purpose of the pass statement python, what is the use of the pass statement in python, example for the python pass statement, explain the significance of pass statement in python, pass statement by value using python, how to check python pass statement, how to use python conditional pass statement, check the pass continuation using python.