Python if else



The Python if else statement executes a block of code, if a specified condition holds true. If the condition is false, another block of code can be executed using the else statement.

Python if else is commonly used with operators, like the comparison or logical operators to create the conditional statements.


Syntax

if (condition):
     #Statement to be executed
elif (condition):    # Optional
     #Statement to be executed
else:                # Optional
     #Statement to be executed

A short example demonstrating it’s use, in it’s simplest form without any optional statements.

a = 5
b = 2
if a > b:
    print("a greater than b") 
# Output 
"a greater than b"

Else:

The else statement triggers if all conditions in the if statement were false. (This includes elif statements). In other words, it’s a block that runs if all else has failed.

b = 2
if a > b:
    print("a greater than b") 
else:
    print("b is greater than a")

We haven’t specified a value for a here under the assumption that the user will be inputting a value.


Elif

We aren’t restricted to just one condition. Using the elif statement we can define multiple conditions, thus multiple routes that the program flow might take.

Keep in mind however that whatever happens, only one route can be taken. Only one out of the many code blocks will be executed. This applies to all the code blocks in a Python if else statements, including if and else code blocks.


Using Comparison Operators

Comparison operators are by far the most common operator used in if else statements. Below are is an example of a if else statement using various comparison operators. (The examples shown above also use comparison operators)

num1 = int(input("Input num1"))
num2 = int(input("Input num2"))

if num1 == num2:
    print("They are equal")
elif num1 > num2:
   print("num1 is greater")
else:
   print("num2 is greater")
   

Using Logical Operators

Logical operators are used often to chain together several conditions to create a more complex if else statement. The two most commonly used are the and operator and the or operator.

and

Linking two conditions together with an and statement will cause it to return false unless both conditions are true.

num1 = int(input("Input num1"))
num2 = int(input("Input num2"))

if num1 < 0 and num2 < 0:
    print("Both numbers are less than 0")

or

king two conditions together with an or statement will cause it to return false unless at least one of the two conditions are true.

num1 = int(input("Input num1"))
num2 = int(input("Input num2"))

if num1 < 0 or num2 < 0:
    print("At least one numbers is less than 0")

Nested if statements

You can easily create nested if statements as shown in example below.

num1 = int(input("Input num1"))
num2 = int(input("Input num2"))

if num1 < 0:
    print("num1 is less than 0")
    if num2 < 0:
        print("num2 is less than 0 as well")

Using Boolean

If you remember that if statements execute their statements when a given condition comes true, then you’ll find this interesting.

flag = True

if flag:
   print("Flag was true")
   #Other statements

Since we replaced the condition with a variable with the value “True”, the loop ran. Likewise, if it was false, it would not have run. We can shorter this down even further by simply writing the Boolean value in place of the condition.

if True:
    print("True")

Extra Examples

In Python, there’s a way to write a “quick” or “short-hand” if else statement, comprising of just one line. Good for readability and conserving number of lines. Known as the “short hand” if else statement.

if a > b: print("a greater than b")

You can also the pass to define a if else block that you want to leave empty for the time being. Python will throw errors on any empty code block otherwise.

if a > b:
     pass

This marks the end of the Python if else statements article. Any suggestions or contributions for CodersLegacy are more than welcome. You can ask any relevant questions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *