0% found this document useful (0 votes)
4 views

3 PythonControl

Uploaded by

Kwok Shi Ming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

3 PythonControl

Uploaded by

Kwok Shi Ming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Relational operators

1
Boolean operators
and, or The tab character.

2
Boolean operators
not

3
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

4
The eval “command”

5
The if-else statment
Example Condition, which is an expression
with value either True or False

keywords
The if-else statment
Example

must be a colon here


The if-else statment
Example

When a compound statement


statements is executed, the statements in
with same indentation it will be
executed sequentially.
form one-single compound
statement
The if-else statment
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 statment Flow-chart for
showing the
Example execution

Evaluate
yes-statement condition

yes no

yes-stmt no-stmt

no-statement
The if-else statment
Example

A special statement, which does nothing


Implement a menu

12
Implement a menu

13
Implement a menu

Yes-stmt

No-stmt

14
Implement a menu

Yes-stmt

No-stmt

15
Implement a menu
Use the elif statement

Actions for the options


Options of the
menu

default
In-class 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.

17
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.
18
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.

19
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.

20
loops/Iterations
To run a sequence of statements repeatedly using
(i) the while statement, or
(ii) the for statement
The while statement
In general
while (condition): condition

loop-body
true false

To execute the statement loop-body

1. determine if the condition is true/false.


2. If true, execute the loop-body once, and
then go back to Step 1. flow chart for
3. If false, stop the while statement, and while
executing the following statement.

22
The while statement
Another statement for implementing loops.
i < 10?

yes no
loop
body print “Hello”
i=i +1

23
The for statement
An equivalent program using the for statement example
First, 0 assigned to i and execute loop body
Then, 1 assigned to i and execute loop body
Then, 2 assigned to i and execute loop body
...
Finally, 9 assigned to i and exectue loop body

loop-body, the statement (or sequence of statements)


to be executed in each iteration (pass).
Note that the indentation is very important.
24
A simple way to generate
[0,1,2,3,4,5,6,7,8,9]: range(10)

Will also print 10 “Hello”

25
What does “for i in …” really do?

loop body

i is assigned each of the


elements in the list one by one,
and for each assignment,
execute the loop body once.
26
More on range()
range(a, b, t) gives
a, a+t, a+2t, ..., a+kt
where
k is the largest integer
with a+kt < b

27
"for i in …": … can be any group of values, even
set is allowed

Only the keys


will be assigned
to i

28
Program for checking magic square

29
Another program for checking magic
square

30
Decision Tree Sort 3

31

You might also like