Day12-If Else Statements

Day12-If Else Statements

if-else, elif, nested if statements

Conditional Operators:

The Python ternary operator (or conditional operator), tests if a condition is true or false and, depending on the outcome, returns the corresponding value — all in just one line of code. Below are some of the conditional operators:

Conditional OperatorsConditional OperatorsConditional Operators
Greater than >Greater than equal to >=Equal to ==
Less than <Less than equal to <=Not equal to !=

Example:

a = int(input("Enter your age: "))  #output: Enter your age: 18
print("Your age is: ", a)  #output: Your age is:  18
print(a>18)   #output: False
print(a<=18)  #output: True
print(a==18)  #output: True
print(a!=18)  #output: False

Based on this, the conditional statements are further classified into following types:

  • if

  • if-else

  • if-else-elif

  • nested if-else-elif.

if-else statement:

Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s) evaluate to True or False.

If the expression evaluates to False, then the program execution follows a different path than it would have if the expression had evaluated to True.

Syntax:

if LOGIC:
    TRUE_body
else:
    FALSE_Body
# Note: LOGIC = TRUE | False

Example 01:

a = int(input("Enter your age: "))
print("Your age is: ", a)

if(a>18):
    print("You can drive")
    print("Yes")
else:
    print("You cannot drive")
    print("No")
print("Yay!") #no indentation here - means it prints every time

Output :

#if input = 9 
Enter your age: 9
Your age is:  9
You cannot drive
No
Yay!

#if input = 76
Enter your age: 76
Your age is:  76
You can drive
Yes
Yay!

Example 02:

applePrice = 210
budget = 200
if (applePrice <= budget):
    print("Alexa, add 1 kg Apples to the cart.")
else:
    print("Alexa, do not add Apples to the cart.")

Output:

Alexa, do not add Apples to the cart.

elif Statements:

Sometimes, the programmer may want to evaluate more than one condition, this can be done using an elif statement.

Example 01:

applePrice = 10
budget = 200
if (budget - applePrice):
    print("Alexa, add 1 kg Apples to the cart.")
elif(budget - applePrice > 70):
    print("Its okay you can buy")
else:
    print("Alexa, do not add Apples to the cart.")

Output:

Alexa, add 1 kg Apples to the cart.

Example 02:

num = int(input("Enter the value of num: "))
if (num < 0):
    print("Number is negative.")
elif (num == 0):
    print("Number is Zero.")
elif (num == 999):
    print("Number is Special.")
else:
    print("Number is positive.")

print("I am happy now")

Output:

If input = 7

Enter the value of num: 7
Number is positive.
I am happy now

if input = -8

Enter the value of num: -8
Number is negative.
I am happy now

if input = 0

Enter the value of num: 0
Number is Zero.
I am happy now

if input = 999

Enter the value of num: 999
Number is Special.
I am happy now

Nested if Statements:

We can use if, if-else, elif statements inside other if statements as well.

Example:

num = 18
if (num < 0):
    print("Number is negative.")
elif (num > 0):
    if (num <= 10):
        print("Number is between 1-10")
    elif (num > 10 and num <= 20):
        print("Number is between 11-20")
    else:
        print("Number is greater than 20")
else:
    print("Number is zero")

Output:

Number is between 11-20

Quiz Questions:

Create odd and even number program using if else statements.

Create a program of Grading System using elif statements.

Create username and password system using if else statements.

Answer: Will provided in next lecture...


Key Points:

  • Indentation in python - means we enter in a 'block' line.

  • Level of indentation - means under indentation there is another indentation.