SlideShare a Scribd company logo
1 of 21
Download to read offline
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

1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
allenbailey
ย 
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
ย 
Working with comparison operators
Working with comparison operatorsWorking with comparison operators
Working with comparison operators
Sara Corpuz
ย 
Testcase design techniques final
Testcase design techniques finalTestcase design techniques final
Testcase design techniques final
shraavank
ย 
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
ย 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
AqeelAbbas94
ย 

Similar to Conditional-Statement.pdf (20)

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
ย 
Working with comparison operators
Working with comparison operatorsWorking with comparison operators
Working with comparison operators
ย 
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() day2
Init() day2Init() day2
Init() day2
ย 

Recently uploaded

young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Serviceyoung call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
amitlee9823
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
ย 
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
amitlee9823
ย 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
University of Wisconsin-Milwaukee
ย 
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
amitlee9823
ย 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
TusharBahuguna2
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
anilsa9823
ย 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
SUHANI PANDEY
ย 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
home
ย 

Recently uploaded (20)

young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Serviceyoung call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
young call girls in Pandav nagar ๐Ÿ” 9953056974 ๐Ÿ” Delhi escort Service
ย 
call girls in Kaushambi (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”...
call girls in Kaushambi (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”...call girls in Kaushambi (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”...
call girls in Kaushambi (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”...
ย 
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
Escorts Service Basapura โ˜Ž 7737669865โ˜Ž Book Your One night Stand (Bangalore)
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
ย 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
ย 
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Dakshinpuri  (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Dakshinpuri (DELHI) ๐Ÿ” >เผ’9953056974 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
Brookefield Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore...
ย 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
ย 
Tapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the FunnelTapestry Clothing Brands: Collapsing the Funnel
Tapestry Clothing Brands: Collapsing the Funnel
ย 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
ย 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
ย 
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service ...
ย 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Aminabad Lucknow best Night Fun service
ย 
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance VVIP ๐ŸŽ SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance  VVIP ๐ŸŽ SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance  VVIP ๐ŸŽ SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi ๐Ÿซฆ No Advance VVIP ๐ŸŽ SER...
ย 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
ย 
Top Rated Pune Call Girls Saswad โŸŸ 6297143586 โŸŸ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad โŸŸ 6297143586 โŸŸ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad โŸŸ 6297143586 โŸŸ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad โŸŸ 6297143586 โŸŸ Call Me For Genuine Sex Serv...
ย 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
ย 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
ย 
Pastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. XxxPastel Portfolio _ by Slidesgo.pptx. Xxx
Pastel Portfolio _ by Slidesgo.pptx. Xxx
ย 

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.