Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Menu
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Search
Close this search box.
Explore Courses

Introduction to Python

  • September 25, 2022
  • No Comments
  • Home
  • Free Book

Table of Contents

Chapter 16 - if Selection Structure

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

IF Statement in Python

  • If Statement
  • Definition
    • if statement is an expression that can be evaluated as True or False and is called a conditional test
    • It executes a set of statements conditionally, based on the value of a logical expression
  • Purpose
    • The main purpose if statement is that it takes the form of an if test, followed by one or more optional elif (“else if”) tests and a final optional else block
      • Python if Statement is used for decision-making operations
  • Importance
    • When the if statement runs, Python executes the block of code associated with the first test that evaluates to true, or the else block if all tests prove false
    • Python uses the if keyword to implement decision control
  • Strengths
    • If statement is used to satisfy the given condition(s) and then execute the particular set of code(s)
  • Weakness
    • If statement checks all the conditions mentioned in a program which results in slow compilation and increases computational cost 
  • Suitable to Use
    • if statement to perform operations when there are multiple possibilities and each must be evaluated differently

Single if Statement

  • Syntax – Single if Statement
				
					'''
if Condition is True the block will be executed otherwise the control will move to the endif statement
'''

if condition: 
    <statement(s)01>
    <statement(s)02>
    <statement(s)03>
    ….
    <statement(s)N>
endif 
				
			
  • Flowchart – Single if Statement
  • Note 
    • If Condition is True, then
      • Statements after if-Statement will be executed
    • If Condition is False, then
      • Statements after if-Statement will not be executed
  • Conclusion
    • In Decision Making
      • Some Statements will be executed and
      • Some statements will be skipped

Multiple if Statement

  • Syntax – Multiple if Statement
				
					'''
if Condition is True the block will be executed otherwise the control will move to the next if statement. If the condition is True, the block of if condition will be executed.
'''

if condition: 
    <statement(s)01>
    <statement(s)02>
    <statement(s)03>
    ….
    <statement(s)N>
if condition: 
    <statement(s)01>
    <statement(s)02>
    <statement(s)03>
    ….
    <statement(s)N>
if condition: 
    <statement(s)01>
    <statement(s)02>
    <statement(s)03>
    ….
    <statement(s)N>
endif 
				
			
  • Flowchart – Multiple if Statement
  • Note 
    • If Condition is True, then
      • Statements after if-Statement will be executed
    • If Condition is False, then
      • Statements after if-Statement will not be executed and the control will be moved to next if condition
  • Conclusion
    • In Decision Making
      • Some Statements will be executed and
      • Some statements will be skipped
  • Example 1 – IF Statement

 

Completely and Correctly Understand a Task
Task Description
  • Two students are playing a game. One student rolled a dice and the other have to identify if the number rolled is even or odd. Write a Python Program which takes an Integer Number as Input (from User) and makes a decision whether it’s an Even Number or an Odd Number? 
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
  • Input
    • Data
      • An Integer Number
    • Instruction(s)
      • Make a Decision – Whether the Input Integer Number is Odd or Even? 
  • Output
    • Display a Message (on the Output Screen) whether the Input Integer Number is an Even Number or Odd Number 
  • Processing
    • Use Conditions for Decision Making – To check whether the Integer Number is Odd or Even?
      • Condition for Even Number – number % 2 == 0
      • Condition for Odd Number – number % 2 == 1
      • Use print() Function  – To display the Message (on the Output Screen) whether the Integer Number is Even or Odd?
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

             endif

             if Modulus of number with 2 is equal to 1

                    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’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
    if number % 2 == 1:
        print(number, "is an Odd Number")
except ValueError:
    print("Sorry Invalid Input!!! Please Enter an Integer Number") 
				
			
Execution of Code (Dry Run)
Python Statement
Calculations
Variable(s)
Output Screen
try:   
number = int(input(“Enter an Integer Number: “)) number = 5Enter an Integer Number: 5
if number % 2 == 0:

Decision Making – Check Whether Condition is True or False 

number % 2 == 0

5 % 2 == 0

1 == 0

False (i.e., Statement(s) will be executed after if-Statement)

number = 5Enter an Integer Number: 5
print(number, “is an Even Number”)This Statement will not be executed because Condition is False in above if-Statement (see Output Screen)number = 5Enter an Integer Number: 5
if number % 2 == 1:

Decision Making – Check Whether Condition is True or False 

number % 2 == 0

5 % 2 == 1

1 == 1

True (i.e., Statement(s) will be executed after if-Statement)

number = 5

 

Enter an Integer Number: 5
print(number, “is an Odd Number”)This Statement will be executed because Condition is True in above if-Statement (see Output Screen)number = 5

Enter an Integer Number: 5

5 is an Odd Number

except: number = 5

Enter an Integer Number: 5

5 is an Odd Number

print(“Sorry Invalid Input!!! Please Enter an Integer Number”) number = 5

Enter an Integer Number: 5

5 is an Odd Number

Output of Code

Enter an Integer Number: 5
5 is an Odd Number

  • Example 2 – IF Statement
Completely and Correctly Understand a Task
Task Description
  • Ms. Samavi buys some grocery and wants to calculate the price paid by her to the salesman and the change return by him. Write a Python Program which takes two Numbers and an Arithmetic Operator as Input (from User) and performs Operation on Operands (Two Numbers) using Operator 
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
  • Input
    • Data
      • Two Numbers 
      • An Operator (+, -, *, /, **)
    • Instruction(s)
      • Take Two Numbers as Input from User
      • Take an Operator as Input from User
      • Make a Decision – whether Operator is +, -, *, / or **
      • Based on Value of Operator Add, Subtract, Multiply, Divide or (Take) Exponent of Two Numbers 
  • Output
    • Display the result obtained by applying Operator on Operands (Two Numbers) on the Output Screen 
  • Processing
    • Use Conditions for Decision Making – To check what Operator is Input by User
      • Condition for + Operator – if operator == ‘+’
      • Condition for – Operator – if operator == ‘-‘
      • Condition for * Operator – if operator == ‘*’
      • Condition for / Operator – if operator == ‘/’
      • Condition for ** Operator – if operator == ‘**’
      • Use print() Function – To display the result obtained by applying Operator on Operands (Two Numbers) on the Output Screen
Pseudo Code

Step 1: Start

Step 2: Input – number1

Step 3: Input – number2

Step 4: Input – operator (Arithmetic Operator)

Step 5: if operator is equal to ‘+’ 

                    Print – Sum of number1 and number2

              endif

             if operator is equal to ‘-‘

                    Print – Subtraction of number2 from number1

               endif

             if operator is equal to ‘*’ 

                    Print – Product (Multiplication) of number1 and number2

             endif

            if operator is equal to ‘/’ 

                    Print – Division of number1 by number2

             endif

             if operator is equal to ‘**’ 

                    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 (Integer / Float)  and an Operator as Input from User and perform Arithmetic Operation on Operands (Two Numbers) based on Value of Operator 
'''

try:
    # Take Two Numbers (Integer / 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)

    # Decision Making Check whether Value of Operator is - or not
    if operator == '-':
        print(number1,"-",number2,"=", number1 - number2)

    # Decision Making Check whether Value of Operator is * or not
    if operator == '*':
        print(number1,"*",number2,"=", number1 * number2)

    # Decision Making Check whether Value of Operator is / or not
    if operator == '/':
        print(number1,"/",number2,"=", number1 / number2)

    # Decision Making Check whether Value of Operator is ** or not
    if 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 (Dry Run)
Python Statement
Calculations
Variable(s)
Output Screen
try:   
number1 = float(input(“Enter First Number: “)) number1 = 10Enter First Number: 10
number2 = float(input(“Enter Second Number: “)) 

number1 = 10

number2 = 30

Enter First Number: 10

Enter Second Number: 30

operator = input(“Enter an Operator: “) 

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

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 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

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 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

if operator == ‘-‘:

Decision Making – Check Whether Condition is True or False 

/ == –

False (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

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 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

if operator == ‘*’:

Decision Making – Check Whether Condition is True or False 

/ == *

False (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

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 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

if operator == ‘/’:

Decision Making – Check Whether Condition is True or False 

/ == /

True (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

print(number1,”/”,number2,”=”, number1 / number2)

10 / 30 = 0.33

This Statement will be executed because Condition is True in above if-Statement (see Output Screen)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Integer Number: 10

Enter Second Integer Number: 30

Enter an Integer Number: /

Calculate Division of Numbers 10 30

10 / 30 = 0.33

if operator == ‘**’:

Decision Making – Check Whether Condition is True or False 

/ == **

False (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

print(number1,”**”,number2,”=”, number1 ** number2)This Statement will not be executed because Condition is False in above if-Statement (see Output Screen)

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

except ValueError: 

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

print(“Sorry Invalid Input. Please enter Integer / Float Number”) 

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

except ZeroDivisionError: 

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

print(“Sorry Invalid Division. Please enter Integer Number other than ZERO”) 

number1 = 10

number2 = 30

operators = ‘+’, ‘-‘, ‘*’, ‘/’

operator = /

10 / 30 = 0.33

Enter First Number: 10

Enter Second Number: 30

Enter an Operator: /

10 / 30 = 0.33

Output of Code 

Enter First Number: 10
Enter Second Number: 30
Enter an Operator: /
10 / 30 = 0.33

  • Example 3 – IF Statement
Completely and Correctly Understand a Task
Task Description
  • A Teacher is comparing marks of Students in two quizzes. Write a Python Program which takes two Numbers as Input (from User) and makes a decision: (1) First Number is greater than the Second Number, (2) Second Number is greater than First Number, or (3) Both Numbers are equal?
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 tan 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
  • Input
    • Data
      • Number 01
      • Number 02
    • Instruction(s)
      • Make a Decision 
        • First Number is Greater than the Second Number
        • Second Number is Greater than First Number
        • Both Numbers are Equal
  • Output
    • Display a Message (on the Output Screen) based on Outcome of Decision
      • number1 is Greater than number2
      • number2 is Greater than number1
      • number1 is Equal to number2
  • Processing
    • Use Conditions for Decision Making 
      • Condition 01 (To Check Whether First Number is Greater Than Second Number) – number1 > number2
      • Condition 02 (To Check Whether Second Number is Greater Than First Number) – number2 > number1
      • Condition 03 (To Check Whether First Number is Equal to Second Number) – number1 == number2
      • Use print() Function  – To display the Message on the Output Screen whether number1 is greater than number2, number2 is greater than number1 or both numbers are equal
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

              endif

              if number2 is greater than number2

                    Print – number2 is greater than number1              

             endif

              if 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 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")

    if number2 > number1:
        print("number2 is greater than number1")

    if 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 = float(input(“Enter First Number: “)) number1 = 10Enter First Number: 10
number2 = float(input(“Enter Second Number: “)) 

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

if number1 > number2:

Decision Making – Check Whether Condition is True or False 

10 > 5

True (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

print(“number1 is greater than number2”)This Statement will be executed because Condition is True in above if-Statement (see Output Screen)

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater than number2

if number2 > number1:

Decision Making – Check Whether Condition is True or False 

10 < 5

False (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater than number2

print(“number2 is greater than number1”)This Statement will not be executed because Condition is False in above if-Statement (see Output Screen)

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater than number2

if number1 == number2:

Decision Making – Check Whether Condition is True or False 

10 == 5

False (i.e., Statement(s) will be executed after if-Statement)

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater 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 Number: 10

Enter Second Number: 5

number1 is greater than number2

except ValueError: 

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater than number2

print(“Sorry Invalid Input !!! Please enter Integer / Float  Number”) 

number1 = 10

number2 = 5

Enter First Number: 10

Enter Second Number: 5

number1 is greater than number2

Output of Code 

Enter First Number: 10
Enter Second Number: 5
number1 is greater than number2

  • Example 4 – IF Statement

 

Completely and Correctly Understand a Task
Task Description
  • A Teacher wants to mark the status of Students as Pass or Fail. Write a Python Program which takes Marks of a Student in a Course as Input (from User) and makes a decision: (1) Student Passed the Course (2) Student Failed in the Course?
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
  • Input
    • Data
      • Marks (of a Student in a Course)
    • Instruction(s)
      • Make a Decision – Whether the Student Passed the Course or Not? 
  • Output
    • Display a Message (on the Output Screen) whether the Student passed the Course or not?
  • Processing
    • Use Conditions for Decision Making – To check whether the Student passed / failed the Course 
      • Condition 01 (To Check Whether Student Passed the Course) – marks >= 50.0
      • Condition 02 (To Check Whether Student Failed the Course) – marks < 50.0
      • Use print() Function  – To display the Message (on the Output Screen) whether the Student passed/ failed the Course 
Pseudo Code

Step 1: Start

Step 2: Input – number (Integer Value)

Step 3: if marks are greater than or equal to 50.0

                    Print – You Passed the Course

             endif

 

             if marks are less than 50.0

                    Print – You Failed the Course 

             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 Marks in Introduction to Python course as Input from User and check whether the Student Passed the Course or Not
'''

try:
    # Take a Marks (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")

    if marks < 50:
        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 = 49.99Enter Marks in Course: 49.99
if marks >= 50.0:

Decision Making – Check Whether Condition is True or False 

marks >= 50.0

49.99 >= 50.0

False (i.e., Statement(s) will be executed after if-Statement)

marks = 49.99Enter Marks in Course: 49.99
print(“You Passed the Course of Introduction to Python”)This Statement will not be executed because Condition is False in above if-Statement (see Output Screen)marks = 49.99Enter Marks in Course: 60
if marks < 50.0:

Decision Making – Check Whether Condition is True or False 

marks < 50.0

49.99 < 50.0

True (i.e., Statement(s) will be executed after if-Statement)

marks = 49.99Enter Marks in Course: 49.99
print(“You Failed the Course of Introduction to Python”)This Statement will be executed because Condition is True in above if-Statement (see Output Screen)marks = 49.99

Enter Marks in Course: 49.99

You Failed the Course of Introduction to Python

except ValueError: marks = 49.99

Enter Marks in Course: 49.99

You Failed the Course of Introduction to Python

print(“Sorry !!!. Invalid Input. Please enter Integer / Float Number”) marks = 49.99

Enter Marks in Course: 49.99

You Failed the Course of Introduction to Python

Output of Code 

Enter Marks in Course: 49.99
You Failed the Course of Introduction to Python

 

  • Example 5 – IF Statement
Completely and Correctly Understand a Task
Task Description
  • Calculate the Weather Forecast for today. Write a Python Program which takes a Number as Input (from User) and makes a decision: (1) Number is Positive (2) Number is Negative? 
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
  • Input
    • Data
      • A Number
    • Instruction(s)
      • Make a Decision – Whether the Number is Positive or Negative? 
  • Output
    • Display a Message (on the Output Screen) whether the Number is Positive or Negative?
  • Processing
    • Use if Selection Structure for Decision Making – To check whether the Number is Positive or Negative? 
      • Condition (To Check Whether the Number is Positive – number >= 0.0
      • Condition (To Check Whether the Number is Negative) – number < 0.0
      • Use print() Function  – To display the Message (on the Output Screen) whether the Number is Positive or Negative?
Pseudo Code

Step 1: Start

Step 2: Input – number 

Step 3: if number is greater than or equal to 0.0

                    Print – Positive Number

              endif

 

              if number is less than 0.0

                    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 a Number: "))

    # Decision Making - Check whether Condition is True or False 
    if number >= 0.0:
        print(number, "is a Positive Number")
    if number < 0.0:
        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
number = int(input(“Enter an Integer Number: “)) number = -100Enter an Integer Number: -100
if number >= 0:

Decision Making – Check Whether Condition is True or False 

number > 0

-100 > 0

False (i.e., Statement(s) will be executed after if-Statement)

number = -100Enter an Integer Number: -100
print(number, “is a Positive Number”)This Statement will not be executed because Condition is False in above if-Statement (see Output Screen)number = -100Enter an Integer Number: -100
if number < 0.0:

Decision Making – Check Whether Condition is True or False 

number < 0.0

-100 < 0.0

True (i.e., Statement(s) will be executed after if-Statement)

number = -100Enter an Integer Number: -100
print(number, ” is a Negative 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 Negative Number

Output of Code 

Enter an Integer Number: -100
-100 is a Negative Number

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following 
      • If Statement

 

Task 1
Completely and Correctly Understand the Task
Task Description
  • Write a Python Program which takes an Integer Number as Input from User check following conditions and display the message on the Output Screen
  • if number is a divisible by 3 then print “Number is divisible by 3”
  • if number is a divisible by 5 then print “Number is divisible by 5”
  • if number is a divisible by 7 then print “Number is divisible by 7”
  • if number is a divisible by 9 then print “Number is divisible by 9”
Sample Output

Sample 1

—————————-

Enter an Integer Number: 49

Number is divisible by 7

Sample 2

—————————-

Enter an Integer Number: 25

Number is divisible by 5

Sample 3

—————————-

Enter an Integer Number: 81

Number is divisible by 3

Number is divisible by 9

Task 2
Completely and Correctly Understand the Task
Task Description
  • Ms. Samavi wants to implement a System in which Bonus amount of each Employee will be decided based on their Salaries. For that purpose, write a Python Program in which Salary of an Employee will be taken as Input from the Employee and check following conditions and display message on Output Screen accordingly
  • If Salary >= 100000 and Salary =< 500000 then 
  • print “Bonus: 20% of Basic Salary”
  • If Salary >= 50000 and Salary =< 100000 then 
  • print “Bonus: 40% of Basic Salary”
  • If Salary >= 25000 and Salary =< 50000 then 
  • print “Bonus: 60% of Basic Salary”
  • If Salary < 25000 then 
  • print “Bonus: 80% of Basic Salary”
Sample Output

Enter Salary: 50000

Bonus: 40% of Basic Salary 

Your Turn Tasks

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 Statement
Chapter 15 - Selection Structures for Decision Making
  • Next
Chapter 17 - if-else Selection Structure
  • Next
Share this article!
Facebook
Twitter
LinkedIn

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Us

Ilm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.