Chapter 17 - if-else Selection Structure
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
IF - ELSE Statement in Python
- Else Statement
- Definition
- else condition can be optionally used to define an alternate block of statements to be executed if the boolean expression in the if condition evaluates to False
- Purpose
- In programming languages, The else keyword is used in conditional statements (if statements), is an alternative statement that is executed if the result of a previous test condition evaluates to false
- Importance
- The else keyword is used in conditional statements (if statements), and decides what to do if the condition is False
- Suitable to Use
- An else statement contains the block of code that executes if the conditional expression in the if statement do not meet
- The if-else structure works well in situations in which you want Python to always execute one of two possible actions. In a simple if-else chain like this, one of the two actions will always be executed
- The if-else statement evaluates test expression and will execute the body of if only when the test condition is True
- Syntax - IF - ELSE Statement
'''
if Condition is True the block will be executed otherwise the control will move to the else statement and execute the else block
'''
if condition:
….
else:
….
endif
- Flowchart – if else Statement
- Example 1 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
print("Before If-Statement")
if 5 > 7:
print("Statement 01 - Condition is True")
else:
print("Statement 01 - Condition is False")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output |
try: | |||
print(“Before If-Statement”) | Before If-Statement | ||
If 5 > 7: | Decision Making – Check Whether Condition is True or False 5 > 7 False(i.e., Statement(s) will be executed after if-Statement) | Before If-Statement | |
print(“Statement 01 – Condition is True”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | Before If-Statement | |
else: | Before If-Statement | ||
print(“Statement 01 – Condition is False”) | This Statement will be executed because Condition is False in above if-Statement (see Output Screen) | Before If-Statement Statement 01 – Condition is False | |
print(“After if-Statement”) | Before If-Statement Statement 01 – Condition is False After if-Statement | ||
except: | Before If-Statement Statement 01 – Condition is False After if-Statement | ||
print(“Runtime Error”) | Before If-Statement Statement 01 – Condition is False After if-Statement |
- Example 2 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
number = 10
print("Before If-Statement")
if 20 >= number:
print("Statement 01 - Condition is True")
else:
print("Statement 01 - Condition is False")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output |
try: | |||
number = 10 | number = 10 | ||
print(“Before If-Statement”) | number = 10 | Before If-Statement | |
if 20 >= number: | Decision Making – Check Whether Condition is True or False 20 >= number 20 >= 10 True (i.e., Statement(s) will be executed after if-Statement) | number = 10 | Before If-Statement |
print(“Statement 01 – Condition is True”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 10 | Before If-Statement Statement 01 – Condition is True |
else: | number = 10 | Before If-Statement | |
print(“Statement 01 – Condition is False”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | number = 10 | Before If-Statement |
print(“After if-Statement”) | number = 10 | Before If-Statement Statement 01 – Condition is True After if-Statement | |
except: | number = 10 | Before If-Statement Statement 01 – Condition is True After if-Statement | |
print(“Runtime Error”) | number = 10 | Before If-Statement Statement 01 – Condition is True After if-Statement |
- Example 3 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
number1 = 100
number2 = 100
print("Before If-Statement")
if number1 != number2:
print("Statement 01 - Condition is True")
print("Statement 02 - number1 and number2 are not equal")
else:
print("Statement 01 - Condition is False")
print("Statement 02 - number1 and number2 are equal")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output |
number1 = 100 | |||
number2 = 100 | number1 = 100 | ||
print(“Before If-Statement”) | number2 = 100 | ||
if number1 != number2: | Decision Making – Check Whether Condition is True or False number1 != number2 100 == 100 False (i.e., Statement(s) will be executed after if-Statement) | number1 = 100 number2 = 100 | Before If-Statement |
print(“Statement 01 – Condition is True”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 100 number2 = 100 | Before If-Statement |
print(“Statement 02 – number1 and number2 are not equal”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 100 number2 = 100 | Before If-Statement |
else: | number1 = 100 number2 = 100 | Before If-Statement | |
print(“Statement 01 – Condition is False”) | This Statement will be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 100 number2 = 100 | Before If-Statement Statement 01 – Condition is False |
print(“Statement 02 – number1 and number2 are equal”) | This Statement will be executed because Condition is False in above if-Statement (see Output Screen) | number1 = 100 number2 = 100 | Before If-Statement Statement 01 – Condition is False Statement 02 – number1 and number2 are equal |
print(“After if-Statement”) | number1 = 100 number2 = 100 | Before If-Statement Statement 01 – Condition is False Statement 02 – number1 and number2 are equal After if-Statement | |
except: | number1 = 100 number2 = 100 | Before If-Statement Statement 01 – Condition is False Statement 02 – number1 and number2 are equal After if-Statement | |
print(“Runtime Error”) | number1 = 100 number2 = 100 | Before If-Statement Statement 01 – Condition is False Statement 02 – number1 and number2 are equal After if-Statement |
- Example 4 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
print("Before If-Statement")
if 20 % 2 == 0:
print("Statement 01 - Condition is True")
print("Statement 02 – Number is even")
else:
print("Statement 01 - Condition is False")
print("Statement 02 – Number is odd")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output |
try: | Before If-Statement | ||
print(“Before If-Statement”) | Before If-Statement | ||
if 20 % 2 == 0: | Decision Making – Check Whether Condition is True or False 20 % 2 == 0 True (i.e., Statement(s) will be executed after if-Statement) | Before If-Statement | |
print(“Statement 01 – Condition is True”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | Before If-Statement Statement 01 – Condition is True | |
print(“Statement 02 – Number is Odd”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even | |
else: | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even | ||
print(“Statement 01 – Condition is False”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even | |
print(“Statement 02 – Number is odd”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even | |
print(“After if-Statement”) | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even After if-Statement | ||
except: | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even After if-Statement | ||
print(“Runtime Error”) | Before If-Statement Statement 01 – Condition is True Statement 02 – Number is even After if-Statement |
- Example 5 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
number1 = 10
number2 = 4
print("Before If-Statement")
exponent = number1 ** number2
if exponent >= 1000:
print("Statement 01 - Condition is True")
print("Statement 02 – Exponent of", number1,"with", number2, "is greater than 1000")
else:
print("Statement 01 - Condition is False")
print("Statement 02 – Exponent of", number1,"with", number2, "is less than 1000")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output |
try: | |||
number1 = 10 | |||
number2 = 4 | number1 = 10 | ||
print(“Before If-Statement”) | number2 = 4 | Before If-Statement | |
exponent = number1 ** number2 | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement | |
if exponent >= 1000: | Decision Making – Check Whether Condition is True or False exponent >= 1000 + == + True (i.e., Statement(s) will be executed after if-Statement) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement |
print(“Statement 01 – Condition is True”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True |
print(“Statement 02 – Exponent of”, number1,”with”, number2, “is greater than 1000”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 |
else: | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 | |
print(“Statement 01 – Condition is False”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 |
print(“Statement 02 – Exponent of”, number1,”with”, number2, “is less than 1000”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 |
print(“After if-Statement”) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 After if-Statement | |
except: | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 After if-Statement | |
print(“Runtime Error”) | number1 = 10 number2 = 4 exponent = 10000 | Before If-Statement Statement 01 – Condition is True Statement 02 – Exponent of 10 with 4 is greater than 1000 After if-Statement |
- Example 6 – IF-ELSE Statement
'''
__author__ = Ms. Samavi Salman
__copyright__= Copyright I 2020 Ms. Samavi Salman
__license__ = Public Domain
__version__ = 1.0
'''
try:
number = -10
print("Before If-Statement")
if number < 0:
print("Statement 01 - Condition is True")
print("Statement 02 – ", number, "is a negative number")
else:
print("Statement 01 - Condition is False")
print("Statement 02 – ", number, "is a positive number")
print("After if-Statement")
except:
print("Runtime Error")
Execution of Code (Dry Run) |
|||
Python Statement |
Calculations |
Variable(s) |
Output |
try: | Before If-Statement | ||
number = -10 | number = -10 | Before If-Statement | |
print(“Before If-Statement”) | number = -10 | Before If-Statement | |
if number < 0: | Decision Making – Check Whether Condition is True or False number < 0 -10 < 0 True (i.e., Statement(s) will be executed after if-Statement) | number = -10 | Before If-Statement |
print(“Statement 01 – Condition is True”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = -10 | Before If-Statement Statement 01 – Condition is True |
print(“Statement 02 – “, number, “is a negative number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number |
else: | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number | |
print(“Statement 01 – Condition is False”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number |
print(“Statement 02 – “, number, “is a positive number”) | This Statement will not be executed because Condition is True in above if-Statement (see Output Screen) | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number |
print(“After if-Statement”) | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number After if-Statement | |
except: | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number After if-Statement | |
print(“Runtime Error”) | number = -10 | Before If-Statement Statement 01 – Condition is True Statement 02 – -10 is a negative number After if-Statement |
- Example 1 – IF - ELSE Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter an Integer Number: 100 100 is a Positive Number Sample Output 02 Enter an Integer Number: -1.5 -1.5 is a Negative Number |
Plan and Design Solution to the Task |
Input-Processing-Output |
|
Pseudo Code |
Step 1: Start Step 2: Input – number Step 3: if number is greater than or equal to 0.0 Print – Positive Number else Print – Negative Number endif-else 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 is a Positive Number or a Negative 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 >= 0.0:
print(number, "is a Positive Number")
else:
print(number, " is a Negative 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 = 100 | Enter an Integer Number: 100 | |
if number > 0: | Decision Making – Check Whether Condition is True or False number > 0 100 > 0 True (i.e., Statement(s) after if-Statement will be executed) | number = 100 | Enter an Integer Number: 100 |
print(number, “is a Positive Number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
print(number, ” is a Negative Number “) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number:100 100 is a Positive Number |
Output of Code | |||
Enter an Integer Number: 100 100 is a Positive Number |
- Example 2 – IF - ELSE 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 are greater than or equal to 50.0 Print – Pass else Print – Fail Endif-else 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
marks = float(input("Enter Marks in Course: "))
# Decision Making - Check whether Condition is True or False
if marks >= 50.0:
print("Congratulations!!! You passed the course")
else:
print("Unfortunately!!! You failed the course")
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 >= 50: | Decision Making – Check Whether Condition is True or False marks >= 50 49.9 >= 50 False (i.e., Statement(s) after if-Statement will not be executed) | Enter Marks in Course: 49.9 | |
print(“Congratulations!!! You passed the course”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | Enter Marks in Course: 49.9 | |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | Enter Marks in Course: 49.9 | |
print(“Unfortunately!!! You failed the course”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | Enter Marks in Course: 49.9 Unfortunately!!! You failed the course | |
Output of Code | |||
Enter marks in Introduction to Python course: 49.99 Unfortunately!!! You failed the course |
- Example 3 – IF - ELSE 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: else 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 is 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")
else:
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 = 7 | Enter an Integer Number: 7 | |
if number % 2 == 0: | Decision Making – Check Whether Condition is True or False number % 2 == 0 7 % 2 == 0 1 == 0 False (i.e., Statement(s) after if-Statement will not be executed) | number = 7 | Enter an Integer Number: 7 |
print(number, “is an Even Number”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 7 | Enter an Integer Number: 7 |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | number = 7
| Enter an Integer Number: 7 |
print(number, “is an Odd Number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 7 | Enter an Integer Number:7 7 is an Odd Number |
Output of Code | |||
Enter an Integer Number: 7 7 is an Odd Number |
- Example 4 – IF - ELSE Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter a Number: 50.45 50.45 is a Positive Number ———————————————- Sample Output 02 Enter a Number: 0 0 is a Positive Number ———————————————- Sample Output 03 Enter a Number: -45.76 -45.76 is a Negative Number ———————————————- Sample Output 04 Enter a Number: -1000 -1000 is a Negative Number ———————————————- Sample Output 05 Enter a Number: courage 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 – number Step 3: if number is greater than or equal to 0.0 Print – Positive Number else Print – Negative 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 a Number (Integer / Float) as Input from User and check whether it’s a Positive Number or a Negative Number?
'''
try:
# Take a Number (Integer / Float) as Input from User
number = float(input("Enter an Integer Number: "))
# Decision Making - Check whether Condition is True or False
if number >= 0.0:
print(number, "is a Positive Number")
else:
print(number, " is a Negative Number ")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer / Float Number")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output Screen |
try: | |||
number = int(input(“Enter an Integer Number: “)) | number = 100 | Enter an Integer Number: 100 | |
if number > 0: | Decision Making – Check Whether Condition is True or False number > 0 100 > 0 True (i.e., Statement(s) will be executed after if-Statement) | number = 100 | Enter an Integer Number: 100 |
print(number, “is a Positive Number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
print(number, ” is a Negative Number “) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number:100 100 is a Positive Number |
except ValueError: | number = 100 | Enter an Integer Number:100 100 is a Positive Number | |
print(“Sorry !!!. Invalid Input. Please enter Integer / Float Number”) | number = 100 | Enter an Integer Number:100 100 is a Positive Number | |
Output of Code | |||
Enter an Integer Number: 100 100 is a Positive Number |
- Example 5 – IF - ELSE Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter a Number: 50.45 50.45 is a Positive Number ———————————————- Sample Output 02 Enter a Number: 0 0 is a Positive Number ———————————————- Sample Output 03 Enter a Number: -45.76 -45.76 is a Negative Number ———————————————- Sample Output 04 Enter a Number: -1000 -1000 is a Negative Number ———————————————- Sample Output 05 Enter a Number: courage 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 – number Step 3: if number is greater than or equal to 0.0 Print – Positive Number else Print – Negative 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 a Number (Integer / Float) as Input from User and check whether it’s a Positive Number or a Negative Number?
'''
try:
# Take a Number (Integer / Float) as Input from User
number = float(input("Enter an Integer Number: "))
# Decision Making - Check whether Condition is True or False
if number >= 0.0:
print(number, "is a Positive Number")
else:
print(number, " is a Negative Number ")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer / Float Number")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output Screen |
try: | |||
number = int(input(“Enter an Integer Number: “)) | number = 100 | Enter an Integer Number: 100 | |
if number > 0: | Decision Making – Check Whether Condition is True or False number > 0 100 > 0 True (i.e., Statement(s) will be executed after if-Statement) | number = 100 | Enter an Integer Number: 100 |
print(number, “is a Positive Number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | number = 100 | Enter an Integer Number: 100 100 is a Positive Number |
print(number, ” is a Negative Number “) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 100 | Enter an Integer Number:100 100 is a Positive Number |
except ValueError: | number = 100 | Enter an Integer Number:100 100 is a Positive Number | |
print(“Sorry !!!. Invalid Input. Please enter Integer / Float Number”) | number = 100 | Enter an Integer Number:100 100 is a Positive Number | |
Output of Code | |||
Enter an Integer Number: 100 100 is a Positive Number |
- Example 6 – IF - ELSE Statement
Completely and Correctly Understand a Task |
Task Description |
|
Sample Outputs |
Sample Output 01 Enter Marks in Course: 89.5 You Passed the Course of Introduction to Python ——————————————————————— Sample Output 02 Enter Marks in Course: 49.9 You Failed the Course of Introduction to Python ——————————————————————— Sample Output 03 Enter Marks in Course: 50 You Passed the Course of Introduction to Python ——————————————————————— Sample Output 04 Enter Marks in Course: 45 You Failed the Course of Introduction to Python ——————————————————————— Sample Output 05 Enter Marks in Course: passion 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 – marks (Integer / Float) Step 3: if marks are greater than or equal to 50.0 Print – Pass else Print – Fail 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 Marks (Integer / Float) as Input from User and check whether the Student passed/ failed the Course?
'''
try:
# Take a Number (Integer / Float) as Input from User
marks = float(input("Enter Marks in Course: "))
# Decision Making - Check whether Condition is True or False
if marks >= 50.0:
print("You Passed the Course of Introduction to Python")
else:
print("You Failed the Course of Introduction to Python")
except ValueError:
print("Sorry !!!. Invalid Input. Please enter Integer / Float Number")
Execution of Code (Dry Run) | |||
Python Statement | Calculations | Variable(s) | Output Screen |
try: | |||
marks = float(input(“Enter Marks in Course: “)) | marks = 70 | Enter Marks in Course: 70 | |
if marks >= 50.0: | Decision Making – Check Whether Condition is True or False marks >= 50 70 >= 50 True (i.e., Statement(s) will be executed after if-Statement) | marks = 70 | Enter Marks in Course: 70 |
print(“You Passed the Course of Introduction to Python”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | marks = 70 | Enter Marks in Course: 70 You Passed the Course of Introduction to Python |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | marks = 70 | Enter Marks in Course: 70 You Passed the Course of Introduction to Python |
print(“You Failed the Course of Introduction to Python”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | marks = 70 | Enter Marks in Course: 70 You Passed the Course of Introduction to Python |
except ValueError: | marks = 70 | Enter Marks in Course: 70 You Passed the Course of Introduction to Python | |
print(“Sorry !!!. Invalid Input. Please enter Integer / Float Number”) | marks = 70 | Enter Marks in Course: 70 You Passed the Course of Introduction to Python | |
Output of Code | |||
Enter Marks in Course: 70 Congratulations!!! You passed the course |
- Example 7 – IF - ELSE 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: 100 100 is an Even Number —————————————— Sample Output 03 Enter an Integer Number: 10.25 Sorry Invalid Input!!! Please Enter an Integer Number —————————————— Sample Output 04 Enter an Integer Number: honest Sorry Invalid Input!!! Please Enter an Integer 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 else Print – Odd Number 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 an Integer Number as Input from User and check whether it is 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")
else:
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 |
try: | |||
number = int(input(“Enter an Integer Number: “)) | number = 7 | Enter an Integer Number: 7 | |
if number % 2 == 0: | Decision Making – Check Whether Condition is True or False number % 2 == 0 7 % 2 == 0 1 == 0 False (i.e., Statement(s) will be executed after if-Statement) | number = 7 | Enter an Integer Number: 7 |
print(number, “is an Even Number”) | This Statement will not be executed because Condition is False in above if-Statement (see Output Screen) | number = 7 | Enter an Integer Number: 7 |
else: | Decision Making – Check Whether Condition is True or False (Statement will be executed when Condition is False in the above if-Statement) | number = 7
| Enter an Integer Number: 7 |
print(number, “is an Odd Number”) | This Statement will be executed because Condition is True in above if-Statement (see Output Screen) | number = 7 | Enter an Integer Number:7 7 is an Odd Number |
except ValueError: | number = 7 | Enter an Integer Number:7 7 is an Odd Number | |
print(“Sorry !!!. Invalid Input. Please enter Integer Number”) | number = 7 | Enter an Integer Number:7 7 is an Odd Number | |
Output of Code | |||
Enter an Integer Number: 7 7 is an Odd Number |
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 – else Statement
- Write Python Programs for all the Tasks given below by following
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Sample Output |
Sample 1 —————————- Enter an Integer Number: 97 97 is a Prime Number Sample 2 —————————- Enter an Integer Number: 96 96 is not a Prime Number |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Sample Output |
Sample 1 —————————- Enter a Character: s s is not a Vowel Sample 2 —————————- Enter a Character: a a is a Vowel |
Task 3 |
Completely and Correctly Understand the Task |
Task Description |
|
Sample Output |
Sample 1 —————————- Enter Student Name: Samavi Enter Yes/No: Yes Samavi is Present Sample 2 —————————- Enter Student Name: Ayesha Enter Yes/No: No Samavi is Absent |
Your Turn Task 1
- Task
- Write at least 4 different Tasks which are very similar to the ones given in the TODO Tasks
- Question
- For each Task write a Python Program for all the Tasks given below by following
- If – else Statement
- For each Task write a Python Program for all the Tasks given below by following