In-class examples of various Python syntax, etc.
1. + 2.
10.**2
10. / 2
3. / 2.
3 / 2
3. // 2.
15 % 2
100 - 1000 / 10
2 * 3
4 / 2
5 > 2
3 < 1
100 >= 100
10 <= 3
a = 2
a == 2
1 & 2
2 & 3
2 & 2
(a == 2) and (3. > 1.)
(a == 2) or (1+1==3)
a != 2
def foo():
pass
foo()
f = foo()
type(f)
a
def foo():
a = 395
foo()
a
def foo(x, y):
return x * y
foo(2, 3)
foo(2, 13)
def foo(g=9.8):
return 10*g
foo()
foo(3.6)
def foo(a, g=9.8):
return a * g
foo(10)
# not allowed (default param first, non default next)
# def foo(g=9.8, a):
# return a * g
foo(g=3.6, a=10)
def foo(a, g=9.8): # multiple returns
return a, g
foo(10)
def bar(x):
x = x * 2
print(x)
y = 2
bar(y)
print(y)
mylist = [3.14, 9.8]
def bar(somelist):
somelist[0] = 6.67e-11
somelist.append(6.022e23)
print(somelist)
bar(mylist)
mylist
aaronlist = mylist
aaronlist[2] = 365
mylist
mylist[1] = 3.7
aaronlist