2
2 + 4
(2+5) * (3-5)
2.7 + 4.5
(2 + 3.5) * (2.7 -4)
0.1 + 0.11
2 + 3.5
What's different from OCaml?
Type coersion! Python does type conversion on its own.
OCaml would have simply rejected (2 + 3.5)
.
Its the same + for ints and floats and as we'll see, a lot of other things.
What about this?
2 + 3j
-5.2 + 6j
(2 + 3j) * (-5.2 + 6j)
"this "
"this is " + "cool"
"this is w" + "a"*10 + "y cool"
"this is so cool you'll fall off your seat"
Say I want the string: Tom says: "this is way cool"
'Tom says: "this is way cool"'
str(50)
str(92.7)
str("cool stuff, dude!")
int("22")
int("abc")
int(3.9)
float(22)
float("3.14")
x = 3
y = x + 4
y
s = "so "
t = "Python is " + s * y + "much fun" + "!" * x
t
s
len(s)
t
len(t)
s
s[0]
s[1]
t
t[7]
s[i:j]
is the (sub)sequence of characters from position i
through j-1
.t
t[0:3]
t[3:6]
t[3:]
t[:4]
t[100]
? What about t[-1]
t
t[100]
t[-1]
t
. t[:i] + t[i:]
is always equal to t
t
t[:2] + t[2:]
t[:6] + t[6:]
t[:-1] + t[-1:]
t1 = (3,5)
t2 = ("abc",2.9,99)
t3 = (t1,t2)
t3
What do you think this will do?
(1,2) + (3, 4)
t4 = ("abc",10) * 4
t4
Tuples are also sequences
t1
len(t1)
t3
len(t3)
t4
len(t4)
Can select:
t1
t1[0]
t2
t2[2]
t2[1:2]
t2[1:]
What other type do you think is a sequence?
l1 = [9,1,1]
l2 = [1, "a", 3.0, ["moo", "stew", "bamboo", "woohoo"]]
len(l1)
len(l2)
l1
l1[0]
l2
l2[1]
l2[3]
l2[3][0]
l2[1:3]
l1
l2
l1 + l2
l1 * 3
2 * l2
l2
l2[0] = "zzz"
l2
x = [1,2,3,4,5]
y = x
y[0] = "a"
y
x
l2
l1
l2[0:3] = l1 * 3
l2
x = -10
if x < 0:
x = abs(x)
print("Negative, flipped", x)
elif x == 0:
print("Zero!")
else:
print("Positive", x)
Overall structure is:
if cond:
if-body-statement
elif cond:
elif-body-statement
else:
else-body-statement
Here the "elif" stands for "else if" -- a convenient shorthand. This is how you can do switch/case statements in Python.
indentation matters!!!
x
pass
if x==15:
y = 0
print("is 15")
different from above!!!
pass
if x==15:
y = 0
print("is 15")
what do think pass does?
if x==15:
y = 0
print("is 15")
Conditions (for if, elif) are like the usual.
Anything thats "not zero" is true and zero is false.
if 1: print("true")
if 0: print("true")
if ["a"]: print("true")
What about this?
if []: print("true")
a,b = 0,1
while b < 20:
print(b)
a, b = b, a+b
Multiple assignment: All of rhs is evaluated and then "simultaneously" stuck into LHS (like pattern-matching in OCaml). Assigning to the tuple (a,b) the value (0,1).
Indentation: The body of the while is indented appropriately.
while cond:
while-body-statement
def fib(n):
a,b = 0,1
while (b < n):
print(b)
a,b = b,a+b
Easy enough. Note that the "body" of the function is indented appropriately. General form:
def fname(x1,x2,...):
<fbody-statement>
print(fib(5))
Can return something different using a return statement:
def fac(n):
if n == 0:
return 1
else:
return n * fac(n-1)
fac(10)
This returns an integer, but python is dynamically-typed so it can return different values.
def fac(n):
if n < 0:
return "Rubbish negative arg"
elif n == 0:
return 1
else:
return n * fac(n-1)
fac(-10)
fac(10)
"default" parameters.
def greet(i,name="Bob"):
print("H" + "e"*i + "y", name)
greet(3, "Sally")
greet(3)
fac
So this means that ...
You can return functions as return values
All of the functional goodness is still there!!! Woohoo, party time!
Check it out:
def compose(f,g):
def foo(x):
return f(g(x))
return foo
def times_3(x):
return 3*x
def first_2_elems(x):
return x[0:2]
baz = compose(times_3,first_2_elems)
baz([1, 2, "three", 4, "five"]) #works for sequences
baz("this is the end, my friend.") #works for sequences