Conditional statements are also known as decision-making statements. We use these statements when we want to execute a block of code when the given condition is true or false. One can achieve decision making by using the below statements:

  • If statements
  • If-else statements
  • Elif statements

Example

num = -7
if (num > 0):
  print(“Number is positive”)
elif (num < 0):
  print(“Number is negative”)
else:
  print(“Number is Zero”)

 

Conditional Expressions

num = 7
a = 10
b = 30

# If statement in one line
if (num > 0) : print("Greater than Zero")

# If-else statements in One line
if (num > 0): print("Greater than Zero") 
else: print("Smaller than Zero")

# elif Statements in One Line
if (num < 0): print("Smaller than Zero") 
elif (num > 0): print("Greater than Zero")
else: print("Number is Zero")

# a if a < b else b 
min = a if a < b else b

 

Multiple Conditions

num1 = 10
num2 = 20
num3 = 30

# Logical operators and, or, not
if (num1 == 10 and num2 == 20 and num3 == 30):
  print("All conditions are true")

if (num1 == 10 or num2 == 20 or num3 == 30):
  print("Atleast one conditions are true")

print('Not x is', not num1)