Introduction to Python
Floor 4 - Computer Room
Outline
Outline
If statement review
1
if elif statement
2
Exercises
3
If statements
if :
1.
2.
Command1
Tab
if Condition :
Command1
False
True
Condition is like a door that controls opening and closing.
If else statements
else:
Command2
if Condition :
1.
2.
3.
4.
Command1
Tab
Tab
False
If the door is closed
It will execute command2
If else statements
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
print(diff)
Page 28 - Example 1
If else statements
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
print(diff)
How many variables are declared?
Page 28 - Example 1
1
What is the variable of "diff" used for?
2
What is going to do in this program?
3
What is the output of this program after execution?
4
If else statements
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
print(diff)
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
else:
diff = a - b
print("a is larger.")
print(diff)
Page 28 - Example 1
Page 29 - Example 2
If else statements
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
print(diff)
a = 15
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
else:
diff = a - b
print("a is larger.")
print(diff)
Page 28 - Example 1
Page 29 - Example 2
What are the difference between two program?
If else statements
Are the execution results of the two programs in Examples 1 and 2 the same?
Same
If the first lines of the two programs are both changed to a = 25, will the execution results of the two programs to be the same?
Differrent
If you change the first line of Example 2 to a = 20, try to explain the execution result of the program.
1.
2.
3.
Questions in P.29
The program will output 'a is larger' and 0, because the conditional statement only checks whether b is greater than a, and does not check whether the two numbers are equal, resulting the output of 'a is larger'.
If else statements
Are the execution results of the two programs in Examples 1 and 2 the same?
Same
If the first lines of the two programs are both changed to a = 25, will the execution results of the two programs to be the same?
Differrent
Questions in P.29
If you change the first line of Example 2 to a = 20, try to explain the execution result of the program.
The program will output 'a is larger' and 0, because the conditional statement only checks whether b is greater than a, and does not check whether the two numbers are equal, resulting the output of 'a is larger'.
1.
2.
3.
Logical Error
if elif statement
Same
Differrent
1.
2.
1.
2.
Tab
if Condition :
Command1
if elif statement
Same
Differrent
1.
2.
1.
2.
Tab
if Condition :
3.
Command1
4.
5.
Tab
Command2
else:
elif Condition :
6.
Tab
Command3
if elif statement
Same
Differrent
1.
2.
a = 20
b = 20
diff = a - b
if b > a:
diff = b - a
print("b is larger.")
else:
diff = a - b
print("a is larger.")
print(diff)
Rewrite the Example 2 code to solve the Logical Error problem
If a is equal to b, then print "a and b are equal".
1.
2.
Tab
if Condition :
3.
Command1
4.
5.
Tab
Command2
else:
elif Condition :
6.
Tab
Command3
if elif statement
Same
Differrent
1.
2.
Use a if elif statement to determine the range of grades for a given score.
Declare a variable called score
"C" if the score is less than 40
Grade range
"B" if the score is between 40 and 60
"A" if the score is grater than or equal to 60
1.
2.
1.
2.
3.
Requirement
60
score
"C"
score < 40
?
else
score >= 60
else
"A"
"B"