SlideShare a Scribd company logo
Conditional
Statement
Duration: 4 period (50 minute)
1. if - else statement
2. Nested if statement
3. elif statement
4. Multiple condition in if and elif statement
5. pass statement
Duration: 4 period (50 minute)
The conditional statements in programming
languages decide the direction of the flow of
program execution. It is used for decision-
making.
The decision is made based on the condition
provided to the if and elif
statement(conditional statements). However,
if none of the conditions gets fulfilled, else
part will be executed.
Introduction
if-else statement
An if statement is used for conditional execution. It is
used to decide whether a certain statement or block of
statements will be executed or not i.e. if a certain
condition is true then a block of statement is executed
otherwise else statement is executed if present.
Syntax:
if condition:
Statement
else:
Statement
***else statement will not take
any condition. Else statement will
be executed only if the if
statement condition fails.
You should know comparison
operators to write condition
Click here for more detail on comparison
operators.
if - else Example
age = int(input("Enter your age: "))
if age <= 12:
print("You are a kid!")
else:
print("You are an adult!")
Output:
# Output sample 1
Enter your age: 10
You are a kid!
=================
# Output sample 2
Enter your age: 15
You are an adult!
Write a python program to check whether you
are eligible to vote or not?
Your program should get the age of the voter
from the user and if their age is 18 and above
let them vote otherwise deny them from
voting.
elif statement
The keyword ‘elif’ is short for ‘else if’, and is useful
to avoid excessive indentation and used when
there are multiple conditions to be checked. If the
condition is not true, it will check for elif condition.
Otherwise, it will directly execute the else
statement.
Syntax:
if condition 1:
Statement
elif condition 2:
Statement
elif condition 3:
Statement
elif condition can continue………
Example
age = int(input("Enter your age:"))
if age <= 12:
print("You are a kid!")
elif age == 18:
print("you are 18 and in adulthood.")
elif age >= 19:
print("You are above 18.")
Output:
# Possible output
Enter your age:10
You are a kid!
=================
Enter your age:18
you are 18 and in
adulthood.
=================
Enter your age:20
You are above 18.
Write a python program that will check for the
following conditions:
● If the light is green – Car is allowed to go
● If the light is yellow – Car has to wait
● If the light is red – Car has to stop
● Other signal - unrecognized signal. Example black, blue,
etc...
1. Traffic light:
Write a program to check students' grades.
Your program should fulfill the following
conditions:
1. Grade A - Outstanding
2. Grade B - Excellent
3. Grade C - Very Good
4. Grade D - Good
5. Grade E – Satisfactory
6. Others - Unrecognized
A program should also ask to enter the
student's name, class, and section. The
expected output is attached below.
2. Students Grade
Expected output:
Multiple condition in if and elif statement
The if and elif statement can execute
multiple conditions at the same time.
Multiple conditions can be used using
logical operators.
You should know logical operators to
write multiple condition
Click here for more detail logical
operators
Example
age = int(input("Enter your age:"))
if age < 12 and age > 0:
print("You are a kid!")
elif age > 12 and age < 25:
print("you are a youth!")
elif age > 25 and age < 54:
print("you are a man!")
else:
print("You are an old man!")
Output:
# Output
Enter your age:8
You are a kid!
=================
Enter your age:23
you are a youth!
=================
Enter your age:45
you are a man!
=================
Enter your age:70
You are an old man!
Modify the earlier program students’ grades in such a way that they
should take in five subject marks. Find the total mark and their
percentage. Your program should check for the following conditions:
• If the percentage falls below 45, they are considered fail.
• If the percentage is between 45 and 60, grade them as pass.
• If the percentage is between 60 and 75, grade them as good.
• If the percentage is between 75 and 85, grade them as very good.
• If the percentage is between 85 and 100, grade them excellent.
• If the percentage is below zero or above 100, it’s an error.
The expected output is attached below.
Student result with grade Expected output:
Nested if statement
Nested if statements mean an if statement inside
another if statement.
Syntax:
if condition 1:
Statements
if condition 1.1:
Statement
else:
Statement
else:
Statement
Example
age = int(input("Enter your age:"))
if age <= 12:
print("You are a kid!")
if age < 5: #nested if condition
print("and also below 5!")
else:
print("but not below 5.")
else:
print("you are above 12!")
Output:
# Three possible output
Enter your age:3
You are a kid!
and also below 5!
=================
Enter your age:7
You are a kid!
but not below 5.
=================
Enter your age:20
you are above 12!
Write a program to trace your subject mark. Your program should fulfill the following
conditions:
• If the subject mark is below 0 and above 100, print “error: mark should be between 0
and 100 only”
• Students will fail in the subject if their mark is below 50.
• Students will pass in the subject if they score 50 and above.
• If subject mark is between 50 and 60, grade student as good.
• If subject mark is between 60 and 80, grade student as very good.
• If subject mark is between 80 and 100, grade student as outstanding.
Make sure to print their mark in every statement to prove that the condition is fulfilled.
Moreover, name, class, and section should be also displayed along with the marks and their
grade.
Pass statement
The pass statement does nothing. It can be
used when a statement is required
syntactically correct but the program requires
no action.
The pass can be also used as a placeholder
for a function or conditional body when you
are working on a new code, allowing you to
keep thinking at a more abstract level. The
pass is silently ignored.
Example
age = 13
if age <= 12:
pass
Output:
In the above example, nothing will be printed and it
won’t generate any error.
Conditional-Statement.pdf

More Related Content

Similar to Conditional-Statement.pdf

4. decision making and some basic problem
4. decision making and some basic problem4. decision making and some basic problem
4. decision making and some basic problem
Alamgir Hossain
 
ICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - TutorialICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - Tutorial
Michael Lew
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
NewsMogul
 
1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
allenbailey
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
Widad Jamaluddin
 
5 Things You Are Probably Doing Wrong With Data
5 Things You Are Probably Doing Wrong With Data5 Things You Are Probably Doing Wrong With Data
5 Things You Are Probably Doing Wrong With Data
Sahil Manekia
 
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docxScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
potmanandrea
 
Manual Testing.
Manual Testing.Manual Testing.
Manual Testing.
Dhanasekaran Nagarajan
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
Gina Bullock
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
Gina Bullock
 
Dymystify Statistics Day 1.pdf
Dymystify Statistics Day 1.pdfDymystify Statistics Day 1.pdf
Dymystify Statistics Day 1.pdf
KristineIbaez2
 
Testcase design techniques final
Testcase design techniques finalTestcase design techniques final
Testcase design techniques final
shraavank
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
Stephen JE Ventura
 
Casestudy 01 algorithms
Casestudy 01 algorithmsCasestudy 01 algorithms
Casestudy 01 algorithms
rawaha134
 
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docxMath 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
andreecapon
 
1 mceeea june 17, 2011
1 mceeea june 17, 20111 mceeea june 17, 2011
1 mceeea june 17, 2011
Cathy McCafferty-Smith
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
AqeelAbbas94
 
MIKE'S IELTS - WRITING Step by Step - DOC THU
MIKE'S IELTS - WRITING Step by Step - DOC THUMIKE'S IELTS - WRITING Step by Step - DOC THU
MIKE'S IELTS - WRITING Step by Step - DOC THU
Lê Thảo
 
PRESENTATION.pptx
PRESENTATION.pptxPRESENTATION.pptx
PRESENTATION.pptx
MahrAlyanYousafwasli
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2

Similar to Conditional-Statement.pdf (20)

4. decision making and some basic problem
4. decision making and some basic problem4. decision making and some basic problem
4. decision making and some basic problem
 
ICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - TutorialICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - Tutorial
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
5 Things You Are Probably Doing Wrong With Data
5 Things You Are Probably Doing Wrong With Data5 Things You Are Probably Doing Wrong With Data
5 Things You Are Probably Doing Wrong With Data
 
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docxScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
ScoreWeek 4Confidence Intervals and Chi Square  (Chs .docx
 
Manual Testing.
Manual Testing.Manual Testing.
Manual Testing.
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
 
Eo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5eEo gaddis java_chapter_04_5e
Eo gaddis java_chapter_04_5e
 
Dymystify Statistics Day 1.pdf
Dymystify Statistics Day 1.pdfDymystify Statistics Day 1.pdf
Dymystify Statistics Day 1.pdf
 
Testcase design techniques final
Testcase design techniques finalTestcase design techniques final
Testcase design techniques final
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Casestudy 01 algorithms
Casestudy 01 algorithmsCasestudy 01 algorithms
Casestudy 01 algorithms
 
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docxMath 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
Math 009 Final Examination Spring, 2015 1 Answer Sheet M.docx
 
1 mceeea june 17, 2011
1 mceeea june 17, 20111 mceeea june 17, 2011
1 mceeea june 17, 2011
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
 
MIKE'S IELTS - WRITING Step by Step - DOC THU
MIKE'S IELTS - WRITING Step by Step - DOC THUMIKE'S IELTS - WRITING Step by Step - DOC THU
MIKE'S IELTS - WRITING Step by Step - DOC THU
 
PRESENTATION.pptx
PRESENTATION.pptxPRESENTATION.pptx
PRESENTATION.pptx
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 

Recently uploaded

Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Designforuminternational
 
International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4
Kyungeun Sung
 
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdfAHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
talaatahm
 
Timeless Principles of Good Design
Timeless Principles of Good DesignTimeless Principles of Good Design
Timeless Principles of Good Design
Carolina de Bartolo
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
21uul8se
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
3vgr39kx
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
k7nm6tk
 
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdfSECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
eloprejohn333
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
ellemjani
 
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
9lq7ultg
 
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
pmgdscunsri
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
Febless Hernane
 
一比一原版肯特大学毕业证UKC成绩单一模一样
一比一原版肯特大学毕业证UKC成绩单一模一样一比一原版肯特大学毕业证UKC成绩单一模一样
一比一原版肯特大学毕业证UKC成绩单一模一样
tobbk6s8
 
ARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdfARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdf
Knight Moves
 
Virtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburghVirtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburgh
millarj46
 
CocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdfCocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdf
PabloMartelLpez
 
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
qo1as76n
 
UXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdfUXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdf
anthonylin333
 
Practical eLearning Makeovers for Everyone
Practical eLearning Makeovers for EveryonePractical eLearning Makeovers for Everyone
Practical eLearning Makeovers for Everyone
Bianca Woods
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NishantRathi18
 

Recently uploaded (20)

Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
Discovering the Best Indian Architects A Spotlight on Design Forum Internatio...
 
International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4International Upcycling Research Network advisory board meeting 4
International Upcycling Research Network advisory board meeting 4
 
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdfAHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
AHMED TALAAT ARCHITECTURE PORTFOLIO .pdf
 
Timeless Principles of Good Design
Timeless Principles of Good DesignTimeless Principles of Good Design
Timeless Principles of Good Design
 
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
一比一原版亚利桑那大学毕业证(UA毕业证书)如何办理
 
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
一比一原版南安普顿索伦特大学毕业证Southampton成绩单一模一样
 
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
一比一原版(LSBU毕业证书)伦敦南岸大学毕业证如何办理
 
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdfSECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
SECURING BUILDING PERMIT CITY OF CALOOCAN.pdf
 
Introduction to User experience design for beginner
Introduction to User experience design for beginnerIntroduction to User experience design for beginner
Introduction to User experience design for beginner
 
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
一比一原版马里兰大学毕业证(UMD毕业证书)如何办理
 
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
Maximize Your Content with Beautiful Assets : Content & Asset for Landing Page
 
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANEEASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
EASY TUTORIAL OF HOW TO USE CAPCUT BY: FEBLESS HERNANE
 
一比一原版肯特大学毕业证UKC成绩单一模一样
一比一原版肯特大学毕业证UKC成绩单一模一样一比一原版肯特大学毕业证UKC成绩单一模一样
一比一原版肯特大学毕业证UKC成绩单一模一样
 
ARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdfARENA - Young adults in the workplace (Knight Moves).pdf
ARENA - Young adults in the workplace (Knight Moves).pdf
 
Virtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburghVirtual Tour Application Powerpoint for museum of edinburgh
Virtual Tour Application Powerpoint for museum of edinburgh
 
CocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdfCocaCola_Brand_equity_package_2012__.pdf
CocaCola_Brand_equity_package_2012__.pdf
 
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
哪里办理美国中央华盛顿大学毕业证双学位证书原版一模一样
 
UXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdfUXpert_Report (UALR Mapping Renewal 2022).pdf
UXpert_Report (UALR Mapping Renewal 2022).pdf
 
Practical eLearning Makeovers for Everyone
Practical eLearning Makeovers for EveryonePractical eLearning Makeovers for Everyone
Practical eLearning Makeovers for Everyone
 
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHINHR Engineers Portfolio 2023 2024 NISHANT RATHI
NHR Engineers Portfolio 2023 2024 NISHANT RATHI
 

Conditional-Statement.pdf

  • 2. 1. if - else statement 2. Nested if statement 3. elif statement 4. Multiple condition in if and elif statement 5. pass statement Duration: 4 period (50 minute)
  • 3. The conditional statements in programming languages decide the direction of the flow of program execution. It is used for decision- making. The decision is made based on the condition provided to the if and elif statement(conditional statements). However, if none of the conditions gets fulfilled, else part will be executed. Introduction
  • 4. if-else statement An if statement is used for conditional execution. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statement is executed otherwise else statement is executed if present. Syntax: if condition: Statement else: Statement ***else statement will not take any condition. Else statement will be executed only if the if statement condition fails.
  • 5. You should know comparison operators to write condition Click here for more detail on comparison operators.
  • 6. if - else Example age = int(input("Enter your age: ")) if age <= 12: print("You are a kid!") else: print("You are an adult!") Output: # Output sample 1 Enter your age: 10 You are a kid! ================= # Output sample 2 Enter your age: 15 You are an adult!
  • 7. Write a python program to check whether you are eligible to vote or not? Your program should get the age of the voter from the user and if their age is 18 and above let them vote otherwise deny them from voting.
  • 8. elif statement The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation and used when there are multiple conditions to be checked. If the condition is not true, it will check for elif condition. Otherwise, it will directly execute the else statement. Syntax: if condition 1: Statement elif condition 2: Statement elif condition 3: Statement elif condition can continue………
  • 9. Example age = int(input("Enter your age:")) if age <= 12: print("You are a kid!") elif age == 18: print("you are 18 and in adulthood.") elif age >= 19: print("You are above 18.") Output: # Possible output Enter your age:10 You are a kid! ================= Enter your age:18 you are 18 and in adulthood. ================= Enter your age:20 You are above 18.
  • 10. Write a python program that will check for the following conditions: ● If the light is green – Car is allowed to go ● If the light is yellow – Car has to wait ● If the light is red – Car has to stop ● Other signal - unrecognized signal. Example black, blue, etc... 1. Traffic light:
  • 11. Write a program to check students' grades. Your program should fulfill the following conditions: 1. Grade A - Outstanding 2. Grade B - Excellent 3. Grade C - Very Good 4. Grade D - Good 5. Grade E – Satisfactory 6. Others - Unrecognized A program should also ask to enter the student's name, class, and section. The expected output is attached below. 2. Students Grade Expected output:
  • 12. Multiple condition in if and elif statement The if and elif statement can execute multiple conditions at the same time. Multiple conditions can be used using logical operators.
  • 13. You should know logical operators to write multiple condition Click here for more detail logical operators
  • 14. Example age = int(input("Enter your age:")) if age < 12 and age > 0: print("You are a kid!") elif age > 12 and age < 25: print("you are a youth!") elif age > 25 and age < 54: print("you are a man!") else: print("You are an old man!") Output: # Output Enter your age:8 You are a kid! ================= Enter your age:23 you are a youth! ================= Enter your age:45 you are a man! ================= Enter your age:70 You are an old man!
  • 15. Modify the earlier program students’ grades in such a way that they should take in five subject marks. Find the total mark and their percentage. Your program should check for the following conditions: • If the percentage falls below 45, they are considered fail. • If the percentage is between 45 and 60, grade them as pass. • If the percentage is between 60 and 75, grade them as good. • If the percentage is between 75 and 85, grade them as very good. • If the percentage is between 85 and 100, grade them excellent. • If the percentage is below zero or above 100, it’s an error. The expected output is attached below. Student result with grade Expected output:
  • 16. Nested if statement Nested if statements mean an if statement inside another if statement. Syntax: if condition 1: Statements if condition 1.1: Statement else: Statement else: Statement
  • 17. Example age = int(input("Enter your age:")) if age <= 12: print("You are a kid!") if age < 5: #nested if condition print("and also below 5!") else: print("but not below 5.") else: print("you are above 12!") Output: # Three possible output Enter your age:3 You are a kid! and also below 5! ================= Enter your age:7 You are a kid! but not below 5. ================= Enter your age:20 you are above 12!
  • 18. Write a program to trace your subject mark. Your program should fulfill the following conditions: • If the subject mark is below 0 and above 100, print “error: mark should be between 0 and 100 only” • Students will fail in the subject if their mark is below 50. • Students will pass in the subject if they score 50 and above. • If subject mark is between 50 and 60, grade student as good. • If subject mark is between 60 and 80, grade student as very good. • If subject mark is between 80 and 100, grade student as outstanding. Make sure to print their mark in every statement to prove that the condition is fulfilled. Moreover, name, class, and section should be also displayed along with the marks and their grade.
  • 19. Pass statement The pass statement does nothing. It can be used when a statement is required syntactically correct but the program requires no action. The pass can be also used as a placeholder for a function or conditional body when you are working on a new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored.
  • 20. Example age = 13 if age <= 12: pass Output: In the above example, nothing will be printed and it won’t generate any error.