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

Lec 2

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 views19 pages

Lec 2

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
You are on page 1/ 19

STRINGS

Letters, special characters, spaces, digits


Enclose in quotation marks or single quotes

hi = "hello there" hi = ‘hello there’

Concatenate strings
name = "Hugh"
greet = hi + name
greeting = hi + " " + name
Do some operations on a string as defined in Python
docs

silly = hi + (" " + name) * 3

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)

\n - a special character; forces output to go onto new line

print(“hello \nhello again”)


6.0001 LECTURE 2 5

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)

input gives you a string so must cast if working


with numbers
num = int(input("Type a number... "))
print(5*num)

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

‘a’ < ‘b’ ➔ True

‘zyzzyva’ >= ‘aardvark’ ➔ True

‘aardvark’ < ‘aardwolf’ ➔ True

‘ab’ > ‘abc’ ➔ False

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

<condition> has a value True or False


evaluate expressions in that block if <condition> is True

6.0001 LECTURE 2 11

7
INDENTATION

Matters in Python
◦ 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")
elif x > y:
print("y is smaller")
print("thanks!")

8
◼︎
= VS ==

x = float(input("Enter a number for x: "))


y = float(input("Enter a number for y: "))
if x == y: What if == is replaced with = ?
Syntax error
print("x and y are equal")
if y != 0:
print("therefore, x / y is", x/y)
elif x < y:
print("x is smaller")
elif x > y:
print("y is smaller")
print("thanks!")

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

# 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)

14
6.0001 LECTURE 2 18
CONTROL FLOW: 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.
6.0001 LECTURE 2 19
15
range(start, stop, step)
start and step are optional
Defaults: start = 0 and step = 1
Loop until loop variable's value is step - 1

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

You might also like