BCS-DS-427B: PYTHON
Manav Rachna International Institute of Research and Studies, Faridabad
PYTHON
Decision Control Statements
ADITI Gaur
Associate Professor, CSE
MRIIRS
BCS-DS-427B: PYTHON
Decision Control/Conditional
Statements
Logical
Allows us to execute a set of code based on some
condition
Types
● If statement
● If..else statement
● Nested-if-else
● If-elif
If (Ghost is there)
do nothing
If(Ghost is there)
say “AHHHH!!”
Source - https://img.devrant.com/devrant/rant/r_1647754_X1Twj.jpg
Conditional Statements
Nested-if-else
If-else
If condition is true
code1 is exceuted ,
else code2 is
exceuted
When one if –else is
present within another if-
else and used to check
multiple conditions
If-elif
Allows you to check multiple
conditions and execute a
block of code whose
condition evaluates to TRUE
if(condition):
code1
else:
code2
if(condition1):
code1
elif(condition2):
code2
else:
code3
if(condition1):
code1
else:
if(condition2):
code2
else:
code3
If Decision Control Statement
• The if decision control flow statement starts with if keyword and ends
with a colon.
• The if statement decides whether to run some particular statement or
not depending upon the value of the Boolean expression.
• If the Boolean expression evaluates to True then statements in the if
block will be executed; otherwise the result is False then none of the
statements are executed.
• In Python, the if block statements are determined through indentation
and the first unindented statement marks the end.
● Program Reads a Number and Prints a Message If It Is Positive
number = int(input("Enter a number"))
if number >= 0:
print("The number entered by the user is a positive number")
Output
Enter a number 6
The number entered by the user is a positive number
● Program to Read Luggage Weight and Charge the Tax Accordingly
weight = int(input("How many pounds does your suitcase weigh?"))
if weight > 50:
print("There is a $25 surcharge for heavy luggage")
print("Thank you")
if…else Decision control statement
● if…else statement allows for a two-way decision.
● Program to Find If a Given Number Is Odd or Even
number = int(input("Enter a number"))
if number % 2 == 0:
print(f"{number} is Even number")
else:
print(f"{number} is Odd number“
Output
Enter a number: 45
45 is Odd number
Program to Check If a Given Year Is a Leap Year
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
Nested-if-else statement
Program to Check If a Number is +ve,-ve or
zero using nested if else
num=int(input("Enter a number"))
if(num>0):
print(“Number is positive”)
else:
if(num<0):
print(“Number is negative”)
else:
print(“Number is zero”)
● The if…elif…else is also called as multi-way decision control
statement.
if…elif…else Statement
Program to Check If a Number is +ve,-ve or
zero using if-elif-else
num=int(input("Enter a number"))
if(num>0):
print(“Number is positive”)
elif(num<0):
print(“Number is negative”)
else:
print(“Number is zero”)
Write a Program to Prompt for a Score between 0.0 and 1.0. If the Score Is
Out of Range, Print an Error. If the Score Is between 0.0 and 1.0, Print a
Grade Using the Following Table
Solution
score = float(input("Enter your score"))
if score < 0 or score > 1:
print('Wrong Input')
elif score >= 0.9:
print('Your Grade is "A" ')
elif score >= 0.8:
print('Your Grade is "B" ')
elif score >= 0.7:
print('Your Grade is "C" ')
elif score >= 0.6:
print('Your Grade is "D" ')
else:
print('Your Grade is "F" ')
Output
Enter your score0.92
Your Grade is "A"
Write program to :
a) find person is eligible to vote or not using if-
else
b) Determine triangle is equilateral or isoceles
c) Find roots of quadratic equation are real or
imaginary or zero

1.4 decision control statements IN PYTHON.pptx

  • 1.
    BCS-DS-427B: PYTHON Manav RachnaInternational Institute of Research and Studies, Faridabad
  • 2.
    PYTHON Decision Control Statements ADITIGaur Associate Professor, CSE MRIIRS BCS-DS-427B: PYTHON
  • 3.
    Decision Control/Conditional Statements Logical Allows usto execute a set of code based on some condition Types ● If statement ● If..else statement ● Nested-if-else ● If-elif If (Ghost is there) do nothing If(Ghost is there) say “AHHHH!!” Source - https://img.devrant.com/devrant/rant/r_1647754_X1Twj.jpg
  • 4.
    Conditional Statements Nested-if-else If-else If conditionis true code1 is exceuted , else code2 is exceuted When one if –else is present within another if- else and used to check multiple conditions If-elif Allows you to check multiple conditions and execute a block of code whose condition evaluates to TRUE if(condition): code1 else: code2 if(condition1): code1 elif(condition2): code2 else: code3 if(condition1): code1 else: if(condition2): code2 else: code3
  • 5.
    If Decision ControlStatement • The if decision control flow statement starts with if keyword and ends with a colon. • The if statement decides whether to run some particular statement or not depending upon the value of the Boolean expression. • If the Boolean expression evaluates to True then statements in the if block will be executed; otherwise the result is False then none of the statements are executed. • In Python, the if block statements are determined through indentation and the first unindented statement marks the end.
  • 6.
    ● Program Readsa Number and Prints a Message If It Is Positive number = int(input("Enter a number")) if number >= 0: print("The number entered by the user is a positive number") Output Enter a number 6 The number entered by the user is a positive number ● Program to Read Luggage Weight and Charge the Tax Accordingly weight = int(input("How many pounds does your suitcase weigh?")) if weight > 50: print("There is a $25 surcharge for heavy luggage") print("Thank you")
  • 7.
    if…else Decision controlstatement ● if…else statement allows for a two-way decision.
  • 8.
    ● Program toFind If a Given Number Is Odd or Even number = int(input("Enter a number")) if number % 2 == 0: print(f"{number} is Even number") else: print(f"{number} is Odd number“ Output Enter a number: 45 45 is Odd number
  • 9.
    Program to CheckIf a Given Year Is a Leap Year year=int(input("Enter year to be checked:")) if(year%4==0 and year%100!=0 or year%400==0): print("The year is a leap year!") else: print("The year isn't a leap year!")
  • 10.
  • 11.
    Program to CheckIf a Number is +ve,-ve or zero using nested if else num=int(input("Enter a number")) if(num>0): print(“Number is positive”) else: if(num<0): print(“Number is negative”) else: print(“Number is zero”)
  • 12.
    ● The if…elif…elseis also called as multi-way decision control statement. if…elif…else Statement
  • 13.
    Program to CheckIf a Number is +ve,-ve or zero using if-elif-else num=int(input("Enter a number")) if(num>0): print(“Number is positive”) elif(num<0): print(“Number is negative”) else: print(“Number is zero”)
  • 14.
    Write a Programto Prompt for a Score between 0.0 and 1.0. If the Score Is Out of Range, Print an Error. If the Score Is between 0.0 and 1.0, Print a Grade Using the Following Table
  • 15.
    Solution score = float(input("Enteryour score")) if score < 0 or score > 1: print('Wrong Input') elif score >= 0.9: print('Your Grade is "A" ') elif score >= 0.8: print('Your Grade is "B" ') elif score >= 0.7: print('Your Grade is "C" ') elif score >= 0.6: print('Your Grade is "D" ') else: print('Your Grade is "F" ') Output Enter your score0.92 Your Grade is "A"
  • 16.
    Write program to: a) find person is eligible to vote or not using if- else b) Determine triangle is equilateral or isoceles c) Find roots of quadratic equation are real or imaginary or zero