Skip to content
Home / Fundamentals

Python if else statements

What is an if statement?

An if statement in Python allows you to specify a block of code that will be executed only if a certain condition is true. This is useful for controlling the flow of your program and only running certain code blocks if certain conditions are met.

Here's an example of a simple if statement:

x = 10

if x > 5:
    print("x is greater than 5")

In this example, the code block that is indented under the if statement will only be executed if the condition x > 5 is true. In this case, it is true, so the code block will be executed and the message "x is greater than 5" will be printed.

Syntax of an if statement

The syntax for an if statement in Python is as follows:

if condition:
    code block

The condition is any expression that evaluates to a boolean value (True or False). If the condition is True, the code block will be executed. If the condition is False, the code block will be skipped.

Here's an example of an if statement that checks whether a number is even or odd:

num = 7

if num % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

In this example, the condition num % 2 == 0 checks whether the remainder of num divided by 2 is equal to 0. If it is, the number is even and the first code block is executed. If it's not, the number is odd and the second code block is executed.

elif and else clauses

You can use the elif (short for "else if") clause to specify additional conditions that should be checked if the previous condition was False. You can use as many elif clauses as you want in an if statement.

Here's an example of an if statement with multiple elif clauses:

x = 10

if x < 0:
    print("x is negative")
elif x == 0:
    print("x is zero")
elif x > 0:
    print("x is positive")

In this example, the first condition checks whether x is less than 0. If it is, the first code block is executed. If it's not, the second condition is checked, and so on. If none of the conditions are met, the code block following the else clause will be executed.

The else clause is optional, and it should be used to specify a code block that should be executed if none of the previous conditions are met.

Here's an example of an if statement with an else clause:

x = 10

if x < 0:
    print("x is negative")
elif x == 0:
    print("x is zero")
else:
    print("x is positive")

In this example, the first two conditions check whether x is less than 0 or equal to 0. If neither of

Shortcuts for if statements

In Python, certain data types can be used in an if statement and will be evaluated as either True or False. Here are the rules for evaluating different data types as boolean values:

  • Integers: An integer will be evaluated as True if it is non-zero, and False if it is zero.
  • Floats: A float will be evaluated as True if it is non-zero, and False if it is zero.
  • Strings: A string will be evaluated as True if it is non-empty, and False if it is empty.
  • Lists: A list will be evaluated as True if it is non-empty, and False if it is empty.
  • Tuples: A tuple will be evaluated as True if it is non-empty, and False if it is empty.
  • Sets: A set will be evaluated as True if it is non-empty, and False if it is empty.
  • Dictionaries: A dictionary will be evaluated as True if it is non-empty, and False if it is empty.
  • None: The special value None will be evaluated as False.

Here are some examples of how these data types can be used in an if statement:

# Integers
x = 1
if x:
    print("x is True")
else:
    print("x is False")

# Floats
y = 0.0
if y:
    print("y is True")
else:
    print("y is False")

# Strings
z = "hello"
if z:
    print("z is True")
else:
    print("z is False")

# Lists
l = []
if l:
    print("l is True")
else:
    print("l is False")

# Tuples
t = ()
if t:
    print("t is True")
else:
    print("t is False")

# Sets
s = set()
if s:
    print("s is True")
else:
    print("s is False")

# Dictionaries
d = {}
if d:
    print("d is True")
else:
    print("d is False")

# None
n = None
if n:
    print("n is True")
else:
    print("n is False")

Best practices for using if statements

Here are a few best practices for using if statements in your code:

  • Use meaningful variable names to make your code easier to understand.
  • Use clear and concise condition statements.
  • Use the elif and else clauses to avoid deeply nested if statements.
  • Use the shortcut syntax for simple if statements with a single line of code in the code block.
  • Use whitespace to visually separate different code blocks and make your code easier to read.