IF Else Statements

In Python, If-Else is a fundamental conditional statement used for decision-making in programming. If…Else statement allows to execution of specific blocks of code depending on the condition is True or False.

i = 10

 # Checking if i is greater than 15
if i > 15:
    print("10 is less than 15")
    
print("I am Not in if")

If else example

i = 20

 # Checking if i is greater than 0
if i > 0:
    print("i is positive")
else:
    print("i is 0 or Negative")

elif example

i = 10

 # Checking if i is less than 10
if i < 10:
    print("i is less than 10")
elif i == 10:
    print("i is equal to 10")
else:
    print("i greateer than 10")

Nested IF else

age = 30
gender = "male"

if age > 18:
    if gender == "male":
        print("Ticket price is $12.")
    else:
        print("Ticket price is $20.")
else:
    if gender == "female":
        print("Ticket price is $8.")
    else:
        print("Ticket price is $10.")

Practise Programs

  1. Input a number and check if its a positive or negative number
  2. Input two numbers if they are positive intergers add them else multiple them
  3. Input one strings, if string len is greater than 5 then check if it is a palindrome