0% found this document useful (0 votes)
7 views35 pages

Python Programming: Control Structures

Chapter 3 of the COMP1117B Computer Programming course covers Python control structures, including relational and boolean operators, if-else statements, and decision trees. It provides examples and exercises, such as creating a ticket vending system, checking for magic squares, and sorting integers. The chapter emphasizes the importance of understanding program logic and offers sample solutions for practical programming challenges.

Uploaded by

marcusngo2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

Python Programming: Control Structures

Chapter 3 of the COMP1117B Computer Programming course covers Python control structures, including relational and boolean operators, if-else statements, and decision trees. It provides examples and exercises, such as creating a ticket vending system, checking for magic squares, and sorting integers. The chapter emphasizes the importance of understanding program logic and offers sample solutions for practical programming challenges.

Uploaded by

marcusngo2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 3.

Python Control

2025-2026 COMP1117B Computer Programming


Dr. Hengshuang Zhao & Dr. H.F. Ting
School of Computing and Data Science
The University of Hong Kong
Relational operators
Relational operators are important for making decisions. They allow us compare
values to determine if one is greater than, less than, equal to, or not equal to
another.

2
Boolean operators
 and, or The tab character.

3
Boolean operators
 not

4
Boolean expression
Example:
(2 > 1) and Is_even or not big
Suppose Is_even = True, and big = True.
Then the value of the boolean expression
(2 > 1) and Is_even or not big
True True True
is True

True False

True

5
The eval “command”
For evaluating an expression in a string.

Equivalent to:
print(1 + 2*3 + ((4 > 5) and (5 % 2 == 0)))
from math import *
print(sin(0.1) + e * sqrt(7))

6
The if-else statement
Usage: when we want to perform different
computations (decision) depending on whether an
expression (condition) evaluates to true(1) or false(0).
Example Please write a program for the ticket
vending system. We charge $498 for
an adult ticket (aged 12 or above), and
$249 for a child ticket (aged 3 – 11).

7
Flowchart
Used in analyzing, designing, documenting or
managing a program.
Helps to visualize the logic of the program.

Before writing program, Yes


always make sure that you age >=12?
understand clearly about
No
the logic that you want your
program to implement. Ticket price is Ticket price is
$249 $498

8
Flowchart
The if-else statement
 Example Condition, which is an expression
with value either True or False

keywords
The if-else statement
 Example

must be a colon here


The if-else statement
 Example

When a compound statement


statements is executed, the statements in
with same indentation it will be
form one-single compound executed sequentially.
statement
The if-else statement
 Example

yes-statement

To execute an if-else statement,


the condition will be evaluated
first. If it is True, the yes-statement
is executed. Otherwise, the
no-statement
no-statement is executed.
The if-else statement Flow-chart for
showing the
 Example execution

yes-statement Evaluate
condition

yes no

yes-stmt no-stmt

no-statement
“pass” keyword
 Example

A special statement, which does nothing


“pass” keyword


Implement a menu

16
Implement a menu

17
Implement a menu

Yes-stmt

No-stmt

18
Implement a menu

Yes-stmt

No-stmt

19
Use of elif (else if) statement

Actions for the options


Options of the
menu

default
Exercise: Magic square revisited
 A magic square is a 3x3 table with nine distinct integers from 1
to 9 so that the sum of integers in each row, column, and
corner-to-corner diagonal is the same.
 An example
2 7 6
9 5 1
4 3 8

 Write a Python program that reads a 3x3 table and prints “yes”
if the table is a magic square, and “no” otherwise.

21
Recall: our magic square program for
printing the row sums, column sums and
diagonal sums

Modify the above program so that it prints “yes” if the


table is a magic square, and prints “no” otherwise.
22
Sample Solution 1

23
Sample Solution 2

24
Sample Solution 3 Output:
========= RESTART:
C:/Users/twchim/Magic
[Link]
R1 = [int(input()),int(input()),int(input())] =========
R2 = [int(input()),int(input()),int(input())] 2
R3 = [int(input()),int(input()),int(input())] 7
6
T = [R1, R2, R3] 9
Sum_R1 = T[0][0]+T[0][1]+T[0][2] 5
Sum_R2 = T[1][0]+T[1][1]+T[1][2] 1
4
Sum_R3 = T[2][0]+T[2][1]+T[2][2] 3
Sum_C1 = T[0][0]+T[1][0]+T[2][0] 8
Sum_C2 = T[0][1]+T[1][1]+T[2][1] yes
Sum_C3 = T[0][2]+T[1][2]+T[2][2] ========= RESTART:
Sum_D1 = T[0][0]+T[1][1]+T[2][2] C:/Users/twchim/Magic
Sum_D2 = T[0][2]+T[1][1]+T[2][0] [Link]
=========
2
if ((Sum_R1 == Sum_R2) and (Sum_R1 == Sum_R3) 7
and (Sum_R1 == Sum_C1) and (Sum_R1 == Sum_C2) 5
9
and (Sum_R1 == Sum_C3) and (Sum_R1 == Sum_D1) 5
and (Sum_R1 == Sum_D2)): 1
print("yes") 4
3
else: 7
print("no") no
25
Decision Tree: A useful method to help
making complicated decisions
Example: Apply decision tree to determine whether a calendar
year is a leap year.
A leap year is a calendar year that meets the following criteria:
 The year must be evenly divisible by 4.
 But if the year can also be evenly divided by 100, it is not a leap year.
 Unless the year is also evenly divisible by 400. Then it is also a leap year.

Problem: Write a program that inputs a calendar year, and then


prints “Yes” if the year is a leap year, and prints “No” otherwise.

26
A decision tree for determining whether a year is
a leap year.

y % 4 == 0
True False

y % 100 == 0 print “No”


True False

y % 400 == 0 print “Yes”

True False

print “Yes” print “No”

27
Sample solution

y % 4 == 0
True False

y % 100 == 0 print “No”


True False

y % 400 == 0 print “Yes”

True False

print “Yes” print “No”

28
Exercise: Sort three integers
 Write a program that reads three integers, and then prints
them in ascending order (i.e., from smallest to largest).
 Example:
 Input: 34, 1, 15
 Output: 1, 15, 34
 You may assume that the three integers are distinct.

29
Decision tree for sorting three integers
a>b
Y N

a>c
Y N b>c
Y
N
b>c b
N a a>c a
Y
c Y N b
c
c b
b c c a
a a a c
b b

Challenge: Write a Python program that uses only the if-else statement to
sort four integers.

30
Sample solution

Challenge: Can you write a Python program that


uses only the if-else statement to sort four
integers?

31
Sorting four integers - sample solution 1
# Take input from the user for four integers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))

# Sort the numbers using only if-else statements


if num1 > num2:
num1, num2 = num2, num1
if num3 > num4:
num3, num4 = num4, num3
if num1 > num3:
num1, num3 = num3, num1 Enter the first number: 7
if num2 > num4: Enter the second number: 9
num2, num4 = num4, num2 Enter the third number: 5
if num2 > num3: Enter the fourth number: 3
num2, num3 = num3, num2 Sorted numbers are: 3 5 7 9

# Print the sorted numbers


print("Sorted numbers are:", num1, num2, num3, num4)

32
Sorting four integers - sample solution 2
# Take input from the user for four integers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))

# Create a list of the numbers and sort them


numbers = [num1, num2, num3, num4]
sorted_numbers = sorted(numbers)

# Print the sorted numbers


print("Sorted numbers are:", sorted_numbers)

Enter the first number: 7


Enter the second number: 9
Enter the third number: 5
Enter the fourth number: 3
Sorted numbers are: 3 5 7 9
33
We are happy to help you!

“If you face any problems in understanding the materials in


the lectures or the tutorials, please feel free to contact us or
our STAs.
We are very happy to help you!
We wish you enjoy learning programming in this class ☺.”
Chapter 3.

END

2025-2026 COMP1117B Computer Programming


Dr. Hengshuang Zhao & Dr. H.F. Ting
School of Computing and Data Science
The University of Hong Kong

You might also like