Form 4 - Computer
2024-2025
Floor 4 - Computer Room
Mr. Peter
Types and IF Statements
Santa Rosa de Lima English Secondary School
聖羅撒英文中學
Colégio de Santa Rosa de Lima - Secção Inglesa
Outline
What is Variable Types in Python
1.
Review the concept of variable type in Python
What is IF Statement in Python
2.
Focusing on the syntax of IF Statement
Python IF Statement implementation
3.
Define IF condition for requirements
What is variable types?
What is variable type in Python
Variable is a box
What is variable type in Python
What can be inside the box?
What is variable type in Python
What can be inside the box?
1
Integer
What can be inside the box?
What is variable type in Python
What can be inside the box?
1
Integer
What can be inside the box?
2
Float
What is variable type in Python
What can be inside the box?
1
Integer
What can be inside the box?
2
Float
3
String
What is variable type in Python
What can be inside the box?
1
Integer
What can be inside the box?
2
Float
3
String
4
Boolean
What is variable type in Python
What can be inside the box?
1
Integer
What can be inside the box?
2
Float
3
String
4
Boolean
5
List
6
Set
7
Dictionary
8
None
IF Statement in Python
IF Statement in Python
if :
1.
2.
Command1
Tab
if Condition :
Command1
False
True
Condition is like a door that controls opening and closing.
IF Statement in Python
else:
Command2
if Condition :
1.
2.
3.
4.
Command1
Tab
Tab
False
If the door is closed
It will execute command2
IF Statement in Python
door = True
if door:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'A'
IF Statement in Python
door = False
if door == False:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'A'
IF Statement in Python
door = 18
if door > 18:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'B'
IF Statement in Python
door = 20
if door > 18 and door < 30:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'A'
IF Statement in Python
door = 20
if door > 18 or door < 30:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'A'
IF Statement in Python
door_A = 20
door_B = door_A != 20
if door_A == 20 and door_B == True:
print( 'A' )
else:
print( 'B' )
Please determine the output of the following IF statement:
'B'
Python IF Statement Exercises
Python IF Exercises - Ex01
Implement a function that uses Python's input() to check if the user's input is an empty string:
1.
Save your file as "ClassNumber_YourName_Ex01.py"
value = input( 'Please enter some text here: ' )
if the input value is empty, the function should print "Input value is empty".
1
2
if the input value is not empty, the function should print "Input value is not empty".
Ex01 Demo
Python IF Exercises - Ex01
Implement a function that uses Python's input() to check if the user's input is an empty string:
1.
Save your file as "ClassNumber_YourName_Ex01.py"
value = input( 'Please enter some text here: ' )
if the input value is empty, the function should print "Input value is empty".
1
2
if the input value is not empty, the function should print "Input value is not empty".
Ex01 Demo
Python IF Exercises - Ex02
Create a program that determines if a student is eligible to graduate based on their final grade and attendance rate. The conditions are:
1.
Save your file as "ClassNumber_YourName_Ex02.py"
The student must have a final grade of 70 or higher
1
2
AND the student must have an absence rate of 20% or less
3
OR if the student has a perfect grade of 100, they can graduate even with a higher absence rate
final_grade = int(input("Enter your final grade (0-100): "))
absence_rate = float(input("Enter your absence rate (0-100%): "))
Ex02 Demo
Python IF Exercises - Ex02
Create a program that determines if a student is eligible to graduate based on their final grade and attendance rate. The conditions are:
1.
Save your file as "ClassNumber_YourName_Ex02.py"
The student must have a final grade of 70 or higher
1
2
AND the student must have an absence rate of 20% or less
3
OR if the student has a perfect grade of 100, they can graduate even with a higher absence rate
final_grade = int(input("Enter your final grade (0-100): "))
absence_rate = float(input("Enter your absence rate (0-100%): "))
Ex02 Demo
F4 - Lesson03
By Mr Peter
F4 - Lesson03
- 16