SimpleTuts.com

Python Conditional Statements

Conditional statements in Python, such as if, elif, and else, allow you to execute specific blocks of code based on certain conditions being true or false. They provide a way to control the flow of your program based on logical expressions.


#Conditional statements in Python
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")
                
Python Online Compiler

Using Logical Operators (and, or, not)


#Conditional statements using logical operators
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a==b or b==c or a==c:
  print("Please enter three different numbers")
elif a>b and a>c :
  print(a,"is greatest")
elif b>a and b>c :
  print(b,"is greatest")
else:
  print(c,"is greatest")
  
                
Python Online Compiler

Nested if conditions


#Nested conditions
x = int(input("Enter a number : "))

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")
                
Python Online Compiler