• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Python | Conditional Statements (if, elif, else)
The if statement evaluates expressions to either True or False and executes different code blocks accordingly.
If the expression evaluates to True, then the code block of the if statement will be executed.

Basic if statement structure:
if <if_condition>:
  # code to execute if <if_condition> evaluates to True
elif <elif_condition>:
  # code to execute if <elif_condition> evaluates to True
else:
  # code to execute if none of the above conditions evaluate to True
Values that evaluate to False by default:
These values evaluate to False: None, False, 0, 0.0, '' (empty string), [] (empty list), () (empty tuple), {} (empty dictionary), set() (empty set)
x = None
if not x:
  print(f'Not True: {x}')

x = False
if not x:
  print(f'Not True: {x}')

x = 0
if not x:
  print(f'Not True: {x}')

x = 0.0
if not x:
  print(f'Not True: {x}')

# empty string
x = ''
if not x:
  print(f'Not True: {x}')

# empty list
x = []
if not x:
  print(f'Not True: {x}')

# empty tuple
x = ()
if not x:
  print(f'Not True: {x}')

# empty dictionary
x = {}
if not x:
  print(f'Not True: {x}')

# empty set
x = set()
if not x:
  print(f'Not True: {x}')
Simple if statements:
x = True
if x:
  print('x is True')
if-else statements:
x = 0
if x == 0:
  print('zero')
else:
  print('not zero')
if-elif-else statements:
x = 0
if x == 0:
  print('zero')
elif x > 0:
  print('positive')
else:
  print('negative')
Nested if statements:
x = 0
if x == 0:
  print('zero')
elif x > 0:
  if x > 1_000_000:
    print('big positive number')
  else:
    print('small positive number')
else:
  if x < -1_000_000:
    print('big negative number')
  else:
    print('small negative number')
Better Practices for None Comparisons:
x = None
if x is None: # don't use x == None
  print('x is None')
The not operator:
x = False
if not x:
  print('x is False')
Ternary operator (conditional expression):
x = True
y = 'true' if x else 'false'
print(f'x is {y}')
Equality operator (==):
x = 0
if x == 0:
  print('zero')
Inequality operator (!=):
x = 1
if x != 0:
  print('not zero')
Comparison operators:
x = 0

# greater than
if x > 0:
  print('positive number')

# greater than or equal to
if x >= 0:
  print('positive number including zero')

# less than
if x < 0:
  print('negative number')

# less than or equal to
if x <= 0:
  print('negative number including zero')
Logical and operator:
x = 1
y = 1
if x > 0 and y > 0:
  print('both x and y are positive numbers')
Logical or operator:
x = 1
y = 1
if x > 0 or y > 0:
  print('either or both x and y are positive numbers')
Complex conditions with parentheses:
x = 1
y = 1
if (x > 0 and y > 0) or (x < 0 and y < 0):
  print('either both x and y are positive numbers or both negative numbers')
Membership operator (in):
Checking if a value is in a list.
x = [0, 1, 2, 3]
y = 0
if y in x:
  print(f'{y} is in the list {x}')
Checking if a value is in a dictionary.
x = {'a': 1, 'b': 2}
y = 'a'
if y in x:
  print(f'{y} is in the dictionary {x}')
Checking if a value is in a string.
x = "hello python!"
y = "hello"
if y in x:
  print(f'{y} is in the string {x}')
Negative membership operator (not in):
Checking if a value is not in a list.
x = [0, 1, 2, 3]
y = 5
if y not in x:
  print(f'{y} is not in the list {x}')
Checking if a value is not in a dictionary.
x = {'a': 1, 'b': 2}
y = 'c'
if y not in x:
  print(f'{y} is not in the dictionary {x}')
Checking if a value is not in a string.
x = "hello python!"
y = "world"
if y not in x:
  print(f'{y} is not in the string {x}')
Checking if a list is empty:
x = [0, 1, 2, 3]
if x: # python return True if the list contains at least one item
  print(f'{x} is not empty')

# using len method
if len(x) > 0:
  print(f'{x} is not empty')
Using if statements with any/all methods:
x = [0, 1, 2, 3]

# any
if any(i > 0 for i in x):
  print(f'{x} contain a positive number')

# all
if not all(i < 0 for i in x):
  print(f'not all numbers are negative in {x}')
© 2025  mtitek