PHY10016
BRANCHING,
ITERATION
McS. Nguyen, Vuong Thuy Ngan
Previous
01 syntax and semantics
02 scalar objects
03 simple operations
04 expressions, variables and values
TODAY
01 string object type
02 branching and conditionals
03 indentation
04 iteration and loops
Email problem
STRINGS
letters, special characters, spaces, digits
enclose in quotation marks or single quotes
hi = "hello there"
concatenate strings
name = "ana"
greet = hi + name
greeting = hi + " " + name
do some operations on a string as defined in Python docs
silly = hi + " " + name * 3
STRINGS
How to get the output below?
Python_Python_Python_Python
Your student number but with the rule:
odd position display a time * with a is the value at that position
even position display b time _ with b is the value at that position
example: 124234 will be
*__****__***____
5 mins
INPUT/OUTPUT: print
used to output stuff to console
keyword is print
x = 1 print(x)
x_str = str(x)
print("my fav num is", x, ".", "x =", x)
print("my fav num is " + x_str + ". " + "x = " + x_str)
INPUT/OUTPUT: input(" ")
prints whatever is in the quotes
user types in something and hits enter
binds that value to a variable
text = input("Type anything... ")
print(5*text)
input gives you a string so must cast if working with numbers
num = int(input("Type a number... "))
print(5*num)
INPUT/OUTPUT
How to get the output below?
Python_Python_Python_Python=> Repeat Python with _ n time but
the last one won’t have _
Your student number but with the rule:
odd position display a time * with a is the value at that position
even position display b time _ with b is the value at that position
example: 124234 will be
5 mins
*__****__***____
Input your student ID
COMPARISON OPERATORS ON
int,float, string
i and j are variable names
comparisons below evaluate to a Boolean
i>j
i >= j
i<j
i <= j
i == j -> equality test, True if i is the same as j
i != j -> inequality test, True if i not the same as j
LOGIC OPERATORS ON bools
a and b are variable names (with Boolean values)
not a -> True if a is False
False if a is True
a b a and b a or b
a and b -> True if both are True
a or b -> True if either or both are True True True True True
False True False True
True False False True
False False False False
LOGIC OPERATORS ON bools
If you can decribe and and or with Math, what would it be?
a b a and b a or b
True True True True
False True False True
True False False True
False False False False
COMPARISON EXAMPLE
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time)
derive = True
drink = False
both = drink and derive print(both)
If right clear, go right
If right blocked, go forward
If right and front blocked, go left
If right , front, left blocked, go back
CONTROL FLOW -BRANCHING
<condition> has a value True or False
if <condition>: if <condition>:
evaluate expressions in that block if
<expression> <expression>
<condition> is True
<expression> <expression>
... ...
elif <condition>:
if <condition>: <expression>
<expression> <expression>
<expression> ...
... else:
else: <expression>
<expression> <expression>
<expression> ...
...
CONTROL FLOW -BRANCHING
print:
F if your score smaller than 5
D if your score equal or greater than 5 and smaller than 7
C if your score equal or greater than 7 and smaller than 8
B if your score equal or greater than 8 and smaller than 9
A if your score greater than 9
5 mins
INDENTATION
matters in Python
how you denote blocks of code
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y:
print("x and y are equal")
if y != 0:
print("therefore, x / y is", x/y)
elif x < y:
print("x is smaller")
else:
print("y is smaller")
print("thanks!")
= vs ==
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y: What if we change to x=y?
print("x and y are equal")
if y != 0:
print("therefore, x / y is", x/y)
elif x < y:
print("x is smaller")
else:
print("y is smaller")
print("thanks!")
keep going right, takes you back to this same
screen, stuck in a loop
if <exit right>:
<set background to movie_background>
if <exit right>:
<set background to movie_background>
Lost bird
if <exit right>:
<set background to movie_background>
and so on and on and on...
else:
<set background to exit_background>
else:
<set background to exit_background>
else:
<set background to exit_background>
keep going right, takes you back to this same
screen, stuck in a loop
Lost bird
while <exit right>:
<set background to movie_background>
<set background to exit_background>
CONTROL FLOW
while LOOPS
while <condition>:
<expression>
<expression>
...
<condition> evaluates to a Boolean
if <condition> is True, do all the steps inside the
while code block
check <condition> again
repeat until <condition> is False
whileLOOP EXAMPLE
You are the Lost Bird.
************
************
🐦
************
************
Go left or right?
PROGRAM:
n = input("You're the Lost Bird. Go left or right? ")
while n == "right":
n = input("You're in the Lost Forest. Go left or right? ")
print("You got out of the Lost Forest!")
CONTROL FLOW
while and for LOOPS
iterate through numbers/list in a sequence
# more complicated with while loop
n=0
while n < 5:
print(n) n = n+1
# shortcut with for loop
for n in range(5):
print(n)
CONTROL FLOW
while and for LOOPS
for <variable> in range(<some_num>):
<expression>
<expression>
...
each time through the loop, <variable> takes a
value
first time, <variable> starts at the smallest value
next time, <variable> gets the prev value + 1
etc.
range(start,stop,step)
default values are start = 0 and step = 1 and
optional
loop until value is stop - 1
mysum = 0
for i in range(7, 10):
mysum += i
print(mysum)
mysum = 0
for i in range(5, 11, 2):
mysum += i
print(mysum)
breakSTATEMENT
immediately exits whatever loop it is in
skips remaining expressions in code block
exits only innermost loop!
while <condition_1>:
while <condition_2>:
<expression_a>
break
<expression_b>
<expression_c>
breakSTATEMENT
mysum = 0
for i in range(5, 11, 2):
mysum += i
if mysum == 16:
break
mysum += 1
print(mysum)
what happens in this program?
for VS while
draw your bingo 3x3 fill with your
keywords about for vs while
End....
contact using MSTeam/Email
[email protected]