Chapter 18 - if-elif Selection Structure
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
IF - ELIF Statement in Python
- Elif Statement
- Definition
- The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, the body of else is executed
- Purpose
- The main purpose is that the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The elif block is executed if the specified condition evaluates to True
- Importance
- Use the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The elif block is executed if the specified condition evaluates to True
- Suitable to Use
- When there is more than one possibility of a condition to occur
- The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
- Syntax – if-elif Statement
'''
if Condition is True the block will be executed otherwise the control will move to the elif statement and check the condition if condition is true statements inside the block will be executed otherwise next condition will be checked
'''
if condition:
….
elif condition:
….
elif condition:
….
endif
- Flowchart – if-elif Statement
- Example 1 – if-elif Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
print("Before Multiple If-Statement")
operators = int(input("Enter an Arithmetic Operator"))
if operators == '+':
print("Condition 01 is True")
print("Operator is +")
elif operators == '-':
print("Contrition 02 is True")
print("Operator is -")
print("After Multiple if-Statement")
except:
print("Sorry Invalid Operator. Please enter a Valid Operator")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output |
try: | |||
print(“Before Multiple If-Statement”) | Before Multiple If-Statement | ||
operators = int(input(“Enter an Arithmetic Operator”)) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator | |
if operators == ‘+’: | Decision Making – Check Whether Condition is True or False operators == ‘+’ + == + True (i.e., Statement(s) will be executed after if-Statement) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator |
print(“Condition 01 is True”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator |
print(“Operator is +”) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + | |
elif operators == ‘-‘: | Decision Making – Check Whether Condition is True or False operators == ‘-‘ + == – False (i.e., Statement(s) will be executed after if-Statement) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + |
print(“Contrition 02 is True”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + |
print(“Operator is -“) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + | |
print(“After Multiple if-Statement”) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + After Multiple if-Statement | |
except: | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + After Multiple if-Statement | |
print(“Sorry Invalid Operator. Please enter a Valid Operator”) | operators = + | Before Multiple If-Statement Enter an Arithmetic Operator Operator is + After Multiple if-Statement |
- Example 2 – if-elif Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
print("Before Multiple If-Statement")
number1 = int(input("Enter First Integer Number"))
number2 = int(input("Enter Second Integer Number "))
if number1 == number2:
print("number1 is equal to number2")
elif number1 < number2:
print("number1 is less than number2")
elif number1 > number2:
print("number1 is greater than number2")
print("After Multiple if-Statement")
except:
print("Sorry Invalid Operator. Please enter a Valid Operator")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output |
try: | |||
print(“Before Multiple If-Statement”) | Before Multiple If-Statement | ||
number1 = int(input(“Enter First Integer Number”)) | number1 = 230 | Before Multiple If-Statement Enter First Integer Number 230 | |
number2 = int(input(“Enter Second Integer Number”)) | number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 | |
if number1 == number2: | Decision Making – Check Whether Condition is True or False number1 == number2 230 == 333 False (i.e., Statement(s) will be executed after if-Statement) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 |
print(“number1 is equal to number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 |
elif number1 < number2: | Decision Making – Check Whether Condition is True or False number1 < number2 230 < 333 True (i.e., Statement(s) will be executed after if-Statement) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 |
print(“number1 is less than number2”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 |
elif number1 > number2: | Decision Making – Check Whether Condition is True or False number1 > number2 230 > 333 False (i.e., Statement(s) will be executed after if-Statement) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 |
print(“number1 is greater than number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 |
print(“After Multiple if-Statement”) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 After Multiple if-Statement | |
except: | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 After Multiple if-Statement | |
print(“Sorry Invalid Operator. Please enter a Valid Operator”) | number1 = 230 number2 = 333 | Before Multiple If-Statement Enter First Integer Number 230 Enter Second Integer Number 333 number1 is less than number2 After Multiple if-Statement |
- Example 3 – if-elif Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
print("Before Multiple If-Statement")
number = int(input("Enter a Number"))
if number % 2 == 0:
print(number, "is divisible by 2")
elif number % 3 == 0:
print(number, "is divisible by 3")
elif number % 5 == 0:
print(number, "is divisible by 5")
print("After Multiple if-Statement")
except:
print("Sorry Invalid Operator. Please enter a Valid Operator")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output |
try: | |||
print(“Before Multiple If-Statement”) | Before Multiple If-Statement | ||
number = int(input(“Enter a Number”)) | number = 45 | Before Multiple If-Statement | |
if number % 2 == 0: | Decision Making – Check Whether Condition is True or False
number % 2 == 0 45 % 2 == 0 False (i.e., Statement(s) will be executed after if-Statement) | number = 45 | Before Multiple If-Statement |
print(number, “is divisible by 2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 45 | Before Multiple If-Statement |
elif number % 3 == 0: | Decision Making – Check Whether Condition is True or False
number % 3 == 0 45 % 3 == 0 True (i.e., Statement(s) will be executed after if-Statement) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 |
print(number, “is divisible by 3”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 |
elif number % 5 == 0: | Decision Making – Check Whether Condition is True or False
number % 5 == 0 45 % 5 == 0 True (i.e., Statement(s) will be executed after if-Statement) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 |
print(number, “is divisible by 5”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 |
print(“After Multiple if-Statement”) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 After Multiple if-Statement | |
except: | number = 45 | Before Multiple If-Statement 45 is divisible by 3 After Multiple if-Statement | |
print(“Sorry Invalid Operator. Please enter a Valid Operator”) | number = 45 | Before Multiple If-Statement 45 is divisible by 3 After Multiple if-Statement |
- Example 1 – if-elif Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter an Integer Number: 5 5 is an Odd Number ————————————– Sample Output 02 Enter an Integer Number: 12 12 is an Odd Number ————————————– Sample Output 03 Enter an Integer Number: 500 500 is an Even Number |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number (Integer Value) Step 3: if Modulus of number with 2 is equal to 0 Print – Even Number Step 5: elif Modulus of number with 2 is equal to 1 Print – Odd Number endif Step 6: Stop |
Flow Chart |
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take an Integer Number as Input from User and check whether it’s an Even Number or an Odd Number?
'''
try:
# Take an Integer Number as Input from User
number = int(input("Enter an Integer Number: "))
# Decision Making - Check whether Condition is True or False
if number % 2 == 0:
print(number, "is an Even Number")
# Decision Making - Check whether Condition is True or False
elif number % 2 == 1:
print(number, "is an Odd Number")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer Number")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output Screen |
number = int(input(“Enter an Integer Number: “)) | number = 6 | Enter an Integer Number: 6 | |
if number % 2 == 0: | Decision Making – Check Whether Condition is True or False number % 2 == 0 6 % 2 == 0 0 == 0 True (i.e., Statement(s) after if-Statement will be executed) | number = 6 | Enter an Integer Number: 6 |
print(number, “is an Even Number”) | This Statement will be executed because Condition is True in above elif-Statement (see Output Screen) | number = 6 | Enter an Integer Number: 6 |
elif number % 2 == 1: | Decision Making – Check Whether Condition is True or False number % 2 == 1 6 % 2 == 1 0 == 1 False (i.e., Statement(s) after elif-Statement will not be executed) | number = 6 | Enter an Integer Number: 6 |
print(number, “is an Odd Number”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | number = 6 | Enter an Integer Number:6 6 is an Even Number |
Output of Code |
|||
Enter an Integer Number: 6 6 is an Even Number |
- Example 2 – if-elif Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter First Integer Number: 10 Enter Second Integer Number: 5 number1 is greater than number2 —————————————————– Sample Output 02 Enter First Integer Number: 10 Enter Second Integer Number: 50 number1 is less than number2 |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number1 (Integer Value) Step 3: Input – number2 (Integer Value) Step 3: if number1 is greater than number2 Print – number1 is greater than number2 Step 5: elif number1 is less than number2 Print – number1 is less than number2 endif Step 6: Stop |
Flow Chart |
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take two Integer Numbers as Input from User and check which number is Greater?
'''
try:
# Take an Integer Number as Input from User
number1 = int(input("Enter First Integer Number: "))
number2 = int(input("Enter Second Integer Number: "))
# Decision Making - Check whether Condition is True or False
if number1 > number2:
print("number1 is greater than number2")
elif number2 > number1:
print("number2 is greater than number1")
elif number1 == number2:
print("number1 is equal to number2")
except ValueError:
print("Sorry Invalid Input !!! Please enter Integer Number")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output Screen |
number1 = int(input(“Enter First Integer Number: “)) | number1 = 10 | Enter First Integer Number: 5 | |
number2 = int(input(“Enter Second Integer Number: “)) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 | |
if number1 > number2: | Decision Making – Check Whether Condition is True or False 10 > 5 True (i.e., Statement(s) after if-Statement will be executed) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
print(“number1 is greater than number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
elif number1 < number2: | Decision Making – Check Whether Condition is True or False 10 < 5 False (i.e., Statement(s) after elif-Statement will not be executed) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
print(“number1 is less than number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 |
elif number1 == number2: | Decision Making – Check Whether Condition is True or False 10 == 5 False (i.e., Statement(s) after elif-Statement will not be executed) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 |
print(“number1 is equal to number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 |
Output of Code |
|||
Enter First Integer Number: 10 Enter Second Integer Number: 5 number1 is greater than number2 |
- Example 3 – if-elif Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter marks in Introduction to Python course: 60 Congratulations!!! You passed the course ——————————————– Sample Output 02 Enter marks in Introduction to Python course: 49 Sorry!!! You failed the course ——————————————– Sample Output 03 Enter marks in Introduction to Python course: 49.99 Unfortunately!!! You failed the course |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number (Integer Value) Step 3: if marks >= 90 Print – A Grade Step 4: elif 85 <= marks <= 89 Print – A- Grade Step 5: elif 80 <= marks <= 84 Print – B+ Grade Step 6: elif 75 <= marks <= 79 Print – B Grade Step 7: elif 70 <= marks <= 74 Print – B- Grade Step 8: elif 65 <= marks <= 69 Print – C+ Grade Step 9: elif 60 <= marks <= 64 Print – C Grade Step 10: elif 55 <= marks <= 59 Print – C- Grade Step 11: elif 50 <= marks <= 54 Print – D Grade Step 12: elif marks <= 50 Print – F Grade endif Step 13: Stop |
Flow Chart |
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take an Integer Number as Input from User and check whether it’s an Even Number or an Odd Number?
'''
try:
# Take an Integer Number as Input from User
marks = float(input("Enter Marks in Course: "))
# Decision Making - Check whether Condition is True or False
if marks >= 90:
print("A Grade")
elif 85 <= marks <= 89:
print("A- Grade")
elif 80 <= marks <= 84:
print("B+ Grade")
elif 75 <= marks <= 79:
print("B Grade")
elif 70 <= marks <= 74:
print("B- Grade")
elif 65 <= marks <= 69:
print("C+ Grade")
elif 60 <= marks <= 64:
print("C Grade")
elif 55 <= marks <= 59:
print("C- Grade")
elif 50 <= marks <= 54:
print("D Grade")
elif marks <= 50:
print("F Grade")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer Number")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output Screen |
marks = float(input(“Enter Marks in Course: “)) | marks = 49.9 | Enter Marks in Course: 49.9 | |
if marks >= 90: | Decision Making – Check Whether Condition is True or False marks >= 90 49.9 >= 90 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“A Grade”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 85 <= marks <= 89: | Decision Making – Check Whether Condition is True or False 85 <= marks <= 89 85 <= 49.9 <= 89 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“A- Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 80 <= marks <= 84: | Decision Making – Check Whether Condition is True or False 80 <= marks <= 84 80 <= 49.9 <= 84 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“B+ Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 75 <= marks <= 79: | Decision Making – Check Whether Condition is True or False 75 <= marks <= 79 75 <= 49.9 <= 79 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“B Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 70 <= marks <= 74: | Decision Making – Check Whether Condition is True or False 70 <= marks <= 74 70 <= 49.9 <= 74 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“B- Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 65 <= marks <= 69: | Decision Making – Check Whether Condition is True or False 65 <= marks <= 69 65 <= 49.9 <= 69 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“C+ Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 60 <= marks <= 64: | Decision Making – Check Whether Condition is True or False 60 <= marks <= 64 60 <= 49.9 <= 64 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“C Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 55 <= marks <= 59: | Decision Making – Check Whether Condition is True or False 55 <= marks <= 59 55 <= 49.9 <= 59 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“C- Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif 50 <= marks <= 54: | Decision Making – Check Whether Condition is True or False 50 <= marks <= 54 50 <= 49.9 <= 54 False (i.e., Statement(s) after if-Statement will not be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“D Grade”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 |
elif marks <= 50: | Decision Making – Check Whether Condition is True or False marks <= 50 49.9 <= 50 True (i.e., Statement(s) after if-Statement will be executed) | marks = 49.9 | Enter Marks in Course: 49.9 |
print(“F Grade”) | This Statement will be executed because Condition is True in above elif-Statement (see Output Screen) | marks = 49.9 | Enter Marks in Course: 49.9 F Grade |
Output of Code | |||
Enter Marks in Course: 49.9 F Grade |
- Example 4 – if-elif Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter First Number: 2000 Enter Second Number: 1000 number1 is greater than number2 ———————————————————– Sample Output 02 Enter First Number: 4000 Enter Second Number: 6000 number2 is greater than number1 ———————————————————– Sample Output 03 Enter First Number: 60.90 Enter Second Number: 60.90 number1 is equal to number2 ———————————————————– Sample Output 04 Enter First Number: loyal Sorry Invalid Input!!! Please enter Integer / Float Number ———————————————————– Sample Output 05 Enter First Number: 60 Enter Second Number: loyal Sorry Invalid Input!!! Please enter Integer / Float Number |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number1 (Integer / Float Value) Step 3: Input – number2 (Integer / Float Value) Step 3: if number1 is greater than number2 Print – number1 is greater than number2 elif number2 is greater than number2 Print – number2 is greater than number1 elif number1 is equal to number2 Print – number1 is equal to number2 endif Step 4: Stop |
Flow Chart |
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main purpose of this Program is to take two Numbers (Integer / Float) as Input from User and check which number1 is Greater than number2 or vice versa
or both numbers are Equal?
'''
try:
# Take two Numbers (Integer / Float) as Input from User
number1 = float(input("Enter First Number: "))
number2 = float(input("Enter Second Number: "))
# Decision Making - Check whether Condition is True or False
if number1 > number2:
print("number1 is greater than number2")
elif number2 > number1:
print("number2 is greater than number1")
elif number1 == number2:
print("number1 is equal to number2")
except ValueError:
print("Sorry Invalid Input!!! Please enter Integer / Float Number")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output Screen |
try: | |||
number1 = int(input(“Enter First Integer Number: “)) | number1 = 10 | Enter First Integer Number: 5 | |
number2 = int(input(“Enter Second Integer Number: “)) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 | |
if number1 > number2: | Decision Making – Check Whether Condition is True or False 5 > 10 False (i.e., Statement(s) will be executed after if-Statement) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
print(“number1 is greater than number2”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
elif number2 > number1: | Decision Making – Check Whether Condition is True or False 10 > 5 True (i.e., Statement(s) will be executed after elif-Statement) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 |
print(“number2 is greater than number1”) | This Statement will be executed because Condition is True in above elif-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number2 is greater than number1 |
elif number1 == number2: | Decision Making – Check Whether Condition is True or False 10 == 5 False (i.e., Statement(s) will be executed after elif-Statement) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 |
print(“number1 is equal to number2”) | This Statement will not be executed because Condition is False in above elif-Statement (see Output Screen) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 |
except ValueError: | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 | |
print(“Sorry Invalid Input !!! Please enter Integer / Float Number”) | number1 = 10 number2 = 5 | Enter First Integer Number: 5 Enter Second Integer Number: 10 number1 is less than number2 | |
Output of Code | |||
Enter First Integer Number: 10 Enter Second Integer Number: 5 number1 is greater than number2 |
- Example 5 – IF - ELSE Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter First Number: 10 Enter Second Number: 20 Enter an Operator: + 10 + 20 = 30 —————————————— Sample Output 02 Enter First Number: 10 Enter Second Number: 20 Enter an Operator: – 10 – 20 = -10 —————————————— Sample Output 03 Enter First Number: 10 Enter Second Number: 20 Enter an Operator: * 10 * 20 = 200 —————————————— Sample Output 04 Enter First Number: 10 Enter Second Number: 20 Enter an Operator: / 10 / 20 = 0 —————————————— Sample Output 05 Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 5 ** 2 = 25 —————————————— Sample Output 06 Enter First Number: 10.25 Enter Second Number: 20.50 Enter an Operator: + 10.25 + 20.50 = 30.37 —————————————— Sample Output 07 Enter First Number: 10.25 Enter Second Number: 20.50 Enter an Operator: – 10.25 – 20.50 = -10.25 —————————————— Sample Output 08 Enter First Number: 10.25 Enter Second Number: 20.50 Enter an Operator: * 10.25 * 20.50 = 210.12 —————————————— Sample Output 09 Enter First Number: 10.25 Enter Second Number: 20.50 Enter an Operator: / 10.25 / 20.50 = 0.5 —————————————— Sample Output 10 Enter First Number: 10 Enter Second Number: 0 Enter an Operator: / Sorry Invalid Division. Please enter Integer / Float Number other than ZERO —————————————— Sample Output 11 Enter First Number: faith Sorry Invalid Input. Please enter Integer / Float Number —————————————— Sample Output 12 Enter First Number: 10 Enter Second Number: faith Sorry Invalid Input. Please enter Integer / Float Number —————————————— Sample Output 13 Enter First Number: 10 Enter Second Number: 20 Enter an Operator: # Sorry Invalid Input. Please enter a Valid Operator |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number1 Step 3: Input – number2 Step 4: Input – operator (Arithmetic Operator) Step 5: if operator == ‘+’ Print – Sum of number1 and number2 elif operator == ‘-‘ Print – Subtraction of number2 from number1 elif operator == ‘*’ Print – Product (Multiplication) of number1 and number2 elif operator == ‘/’ Print – Division of number1 by number2 elif operator == ‘**’ Print – Exponent of number1 with number2 endif Step 6: Stop |
Flow Chart |
Implementation in Python |
Code |
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
_version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers and an Operator as Input from User and perform Arithmetic Operation on Operands (Two Numbers) based on Value of Operator
'''
try:
# Take Two Numbers (Integers or Float) as Input from User
number1 = float(input("Enter First Number: "))
number2 = float(input("Enter Second Number: "))
# Take Operator as input from User
operator = input("Enter an Operator: ")
# Decision Making Check whether Value of Operator is + or not
if operator == '+':
print(number1,"+",number2,"=", number1 + number2)
elif operator == '-':
print(number1,"-",number2,"=", number1 - number2)
elif operator == '*':
print(number1,"*",number2,"=", number1 * number2)
elif operator == '/':
print(number1,"/",number2,"=", number1 / number2)
elif operator == '**':
print(number1,"**",number2,"=", number1 ** number2)
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer / Float Number")
except ZeroDivisionError:
print("Sorry !!!. Invalid Division. Please enter Integer / Float Number other than ZERO")
Execution of Code | |||
Python Statement | Calculations | Variable(s) | Output Screen |
try: | |||
number1 = float(input(“Enter First Number: “)) | number1 = 5 | Enter First Number: 5 | |
number2 = float(input(“Enter Second Number: “)) | number1 = 5 number2 = 2 | Enter First Number: 5 Enter Second Number: 2 | |
operator = input(“Enter an Operator: “) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** | |
if operator == ‘+’: | Decision Making – Check Whether Condition is True or False operator == + ** == + False (i.e., Statement(s) will be executed after if-Statement) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
print(number1,”+”,number2,”=”, number1 + number2) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
elif operator == ‘-‘: | Decision Making – Check Whether Condition is True or False ** == – False (i.e., Statement(s) will be executed after if-Statement) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
print(number1,”-“,number2,”=”, number1 – number2) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
elif operator == ‘*’: | Decision Making – Check Whether Condition is True or False ** == * False (i.e., Statement(s) will be executed after if-Statement) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
print(number1,”*”,number2,”=”, number1 * number2) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
elif operator == ‘/’: | Decision Making – Check Whether Condition is True or False ** == / False (i.e., Statement(s) will be executed after if-Statement) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
print(number1,”/”,number2,”=”, number1 / number2) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 5 number2 = 2 operator = ** | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
elif operator == ‘**’: | Decision Making – Check Whether Condition is True or False ** == ** True (i.e., Statement(s) will be executed after if-Statement) | number1 = 5 number2 = 2 operator = ** 5 ** 2 = 25 | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** |
print(number1,”**”,number2,”=”, number1 ** number2) | This Statement will be executed because Condition is True in above elif-Statement (see Output Screen) | number1 = 5 number2 = 2 operator = ** 5 ** 2 = 25 | Enter First Number: 5 Enter Second Number: 2 Enter an Operator: ** 5 ** 2 = 25 |
except ValueError: | |||
print(“Sorry !!!. Invalid Input. Please enter Integer / Float Number”) | |||
except ZeroDivisionError: | |||
print(“Sorry !!!. Invalid Division. Please enter Integer / Float Number other than ZERO”) | |||
Output of Code | |||
Enter First Number: 10 Enter Second Number: 30 Enter an Operator: / 10 / 30 = 0.33 |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for all the Tasks given below by following
- If – elif Statement
- Write Python Programs for all the Tasks given below by following
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
· if number is a divisible by 2 then print “Number is divisible by 2” · elif number is a divisible by 4 then print “Number is divisible by 4” · elif number is a divisible by 6 then print “Number is divisible by 6” |
Sample Output |
Sample 1 —————————- Enter an Integer Number: 12 Number is divisible by 2 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
· If Choice == 1 then · print “You have Booked a Bike Ride” · elif Choice == 2 then · print “You have Booked a Car Ride” · elif Choice == 3 then · print “You have Booked a Luxury Car Ride” |
Sample Output |
Enter Choice: 2 You have Booked a Car Ride |
Task 3 |
Completely and Correctly Understand the Task |
Task Description |
· If gender == F and age >= 25 and age <= 40 · print “For Females: You are eligible for doing a Job” · elif gender == F and age > 40 · print “For Females: Age Limit for the Job is between 25 to 40 yrs” · elIf gender == M and age >= 25 and age <= 60 · print “For Males: You are eligible for doing a Job” · elif gender == M and age > 60 · print “For Males: Age Limit for the Job is between 25 to 40 yrs” |
Sample Output |
Enter gender: F Enter age: 30 For Females: You are eligible for doing a Job |
Your Turn Task 1
- Task
- Write at least 4 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program for all the Tasks given below by following
- If – elif Statement