Lec 2
Lec 2
Concatenate strings
name = "Hugh"
greet = hi + name
greeting = hi + " " + name
Do some operations on a string as defined in Python
docs
1
◼︎
◼︎
◼︎
◼︎
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)
2
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)
6.0001 LECTURE 2 6
3
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
4 2
6.0001 LECTURE 7
COMPARISON ON STRINGS
Lexicographical
◦ Dictionary order
5
◼︎
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 and b True if both are True
a or b True if either or both are True
A B A and B A or B
True True True True
True False False True
False True False True
False False False False
6.0001 LECTURE 2 8
6
CONTROL FLOW - BRANCHING
if <condition>: if <condition>:
<expression> Code block <expression>
<expression> <expression>
... ...
elif <condition>:
if <condition>: Indentation <expression>
<expression> <expression>
<expression> ...
... else:
else: <expression>
<expression> Indentation <expression>
<expression> ...
...
6.0001 LECTURE 2 11
7
INDENTATION
Matters in Python
◦ Denote blocks of code
8
◼︎
= VS ==
9
Legend
Legend of Zelda
of Zelda – –
Lost
Lost Woods
Woods
keep going right,
keep going right,
takes you back to this
takes
same you stuck
screen, backinto this
asame
loop screen, stuck in
mage Courtesy Nintendo, All Rights Reserved. This content is excluded from our Creative
ommons license. For more information, see http://ocw.mit.edu/help/faq-fair-use/.
a loop
if <exit right>:
if
ntendo, <set <exit_right>:
background
All Rights to woods_background>
Reserved. This content is excluded from our Creative
For moreif <setright>:
<exit
information, seebackground to woods_background>
http://ocw.mit.edu/help/faq-fair-use/.
t right>: <set background to woods_background>
if <exit_right>:
if <exit right>:
t background <set
woods_background>
tobackground
<set background to woods_background>
to woods_background>
<exit right>: andifso<exit_right>:
on and on and on...
<set background
else: to woods_background>
<set background to woods_background>
if <exit right>: <set background to exit_background>
else: and so on and on and on…
<set <setbackground
else:
background toto woods_background>
exit_background>
else:and so on and <setonbackground
and on... to exit_background>
else: <set background to exit_background>
else:
<set background to exit_background>
<set background to 2 exit_background>
6.0001 LECTURE 14
e: else:
<set background to exit_background>
<set background to exit_background>
10
t background to exit_background>
Lost Woods
Legend
Legend of Zelda
of Zelda – –
Lost Woods
Lost Woods keep going right,
takes
keep going right, you back to th
keep going right,
takes you back to this screen, stuck i
same
takes
same you stuck
screen, back in
a loopto this
asame
loop screen, stuck in
ord Cloud
ommons license. copyright un nown,
For more information, All Right Reserved. This content
a loop
mage Courtesy Nintendo, All Rights Reserved. This content is excluded from our Creative
is excluded from our Creative
see http://ocw.mit.edu/help/faq-fair-use/.
Commons license.
if <exit For more information, see http://ocw.mit.edu/help/faq-fair-use/.
right>:
ntendo, <set background
while <exit right>:
All Rights to woods_background>
Reserved. This content is excluded from our Creative
For moreif <exit right>:
information, see http://ocw.mit.edu/help/faq-fair-use/.
<set
t right>: background
<set background to woods_background>
to woods_background>
<set if
background
<exit right>:to exit_background>
t background to woods_background>
<set background to woods_background>
<exit right>:and so on and on and on...
<set background
else: to woods_background>
<set background to exit_background>
if <exit right>:
else:
<set
<setbackground
background toto woods_background>
exit_background>
else:and so on and on and on...
<set background to exit_background>
else:
<set background to exit_background>
6.0001 LECTURE 2 14
e:
<set background to exit_background>
11
t 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
12
while LOOP EXAMPLE
You are in the Lost Forest
****************
****************
😀
****************
****************
Go left or right?
PROGRAM:
n = input("You are in the Lost Forest\n" + ('****************\n')*2 + ' 😀 \n' +
('****************\n')*2 + "Go left or right? ")
while n == "right" or n == "Right":
n = input("You are in the Lost Forest\n" + ('****************\n')*2 + ' 😭 \n' +
('****************\n')*2 + "Go left or right? ")
print("\nYou got out of the Lost Forest!\n🙌 ")
13
CONTROL FLOW:
while and for LOOPS
iterate through numbers in a sequence
14
6.0001 LECTURE 2 18
CONTROL FLOW: for LOOPS
for <variable> in range(<some_num>):
<expression>
<expression>
...
mysum = 0 mysum = 0
for i in range(5, 11, 2):
for i in range(7, 10):
mysum += i
mysum += i if mysum == 5:
print(mysum) break
mysum += 1
print(mysum)
16
◼︎
◼︎
◼︎
break STATEMENT
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>
17LECTURE 2
6.0001 21
EXAMPLE
mysum = 0
for i in range(3, 11, 2):
mysum += i
if mysum == 8:
break
mysum += 1
print(mysum)
18
for VS while LOOPS
for loops while loops
know number of unbounded number of
iterations iterations
can end early via can end early via break
break can use a counter but
must initialize before loop
uses a counter and increment it inside loop
can rewrite a for loop may not be able to
using a while loop rewrite a while loop using
a for loop
6.0001 LECTURE 2 23
19