CS L5
CS L5
• Strings
• characters are strings of length 1
• + - * / % (like C)
• += -= etc. (no ++ or --)
• Assignment using =
• but semantics are different!
a = 1
a = "foo" # OK
• Can also use + to concatenate strings
Comments
print(x)
print(y)
print(z)
print(x + k)
print(y + z)
Basic programming
a,b = 0, 1
# non-zero = true
while b < 10:
# formatted output, without \n
print b,
# multiple assignment
a,b = b, a+b
Control flow: if
x = int(raw_input("Please enter #:"))
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
▪ no case statement
Control flow: for
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
>>> fib(2000)