Python_lesson2
Python_lesson2
BRANCHING,
ITERATION
McS. Nguyen, Vuong Thuy Ngan
Previous
01 syntax and semantics
02 scalar objects
03 simple operations
03 indentation
concatenate strings
name = "ana"
greet = hi + name
greeting = hi + " " + name
Python_Python_Python_Python
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)
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
a b a and b a or b
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 ==
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
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)