2 + 24
2 + 24
Python has the following types:
strboolintfloat <- a C doubledictlistsettuple2 ** 32 - 14294967295
2 ** 23458228863001835066529632658874512640596519760779404643181058684868103588231651983628740999193787882296163044366493632157270168550417673732837190022570379624359677896388978172052495892656722068451801215349472765139014318812471863009877701280158593080431221425432944328893737411043878272790470707895191304714844545104171879670960362918595278699535244673367090285651259233330614178023118290719313365134610411047047471669360811979405231925836161763944679737525318975335081478616357443060688819657564259801031831799666226609793670519258091624540623073202809652099366028831784182370424464956587657891971708754740727495828880356404026436672396009224339114167289394488157526146727812023740637240031427723432225144832
TrueTrue
FalseFalse
type('a')str
type("a")str
f = 23.2234
f = "lee"
f'lee'
d = {"a": 2, "edward": 1.1}
d{'a': 2, 'edward': 1.1}
d["a"] = 6
d{'a': 6, 'edward': 1.1}
d["b"] = "new variable"d{'a': 6, 'edward': 1.1, 'b': 'new variable'}
l = [1, 2, 3, d]
l[1, 2, 3, {'a': 6, 'edward': 1.1, 'b': 'new variable'}]
l[3] = "mutable"l[0]1
l[-1]'mutable'
l.append(2980)
l[1, 2, 3, 'mutable', 2980]
t = (1, 2, 3, l)
# t[0] = 20 # doesn't work
t = (2, 3, 4, 6)
t(2, 3, 4, 6)
# another look at dictionaries
d{'a': 6, 'edward': 1.1, 'b': 'new variable'}
d[1] = "new"
d[1.1] = "cool"
d[t] = "whoa"
# d[l] = "nope"
d{'a': 6,
'edward': 1.1,
'b': 'new variable',
1: 'new',
1.1: 'cool',
(2, 3, 4, 6): 'whoa'}
d[t] = "new thing"
d{'a': 6,
'edward': 1.1,
'b': 'new variable',
1: 'new',
1.1: 'cool',
(2, 3, 4, 6): 'new thing'}
del d[t]d{'a': 6, 'edward': 1.1, 'b': 'new variable', 1: 'new', 1.1: 'cool'}
def f(x):
return x + 3f(5)8
x3
def g(x, y):
return x * y + 3
g(3, 5)18
def h(x, z = 10):
return x * z
h(3, z = 3)9
def j(x, y = 10, z = 23):
return x + y * z
j(3, z = 2, y = 3)9
l[1, 2, 3, 'mutable', 2980]
def k(lst):
lst[0] = "new value"k(l)
l['new value', 2, 3, 'mutable', 2980]
for i in range(3):
print(i)0
1
2
l = ["something", "in", "your", "list"]
for li in l:
print(li)something
in
your
list
for i, li in enumerate(l):
print(i, li)0 something
1 in
2 your
3 list
t2 = ("math", "314")
a, b = t2
b'314'
def n(x):
return x, x * 2
a, b = n(3)b6
i = 0
while True:
i += 2
if i > 10:
break
print(i)2
4
6
8
10
if l:
print("cool")cool
if [[]]:
print("hwhat")hwhat
s = "math314"
s'math314'
x = 3
f = f"is a string with an interpolated variable like {x}"
f'is a string with an interpolated variable like 3'
l2 = [h(x) for x in range(3)]
l2[0, 10, 20]
d2 = {x: h(x) for x in range(10)}
d2{0: 0, 1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}
class Table():
def __init__(self, legs = 10):
self._legs = legs
def num_legs(self, x = 2):
return self._legstbl = Table()tbl.num_legs()10
tbl2 = Table(legs = 5)
tbl2.num_legs()5
x
type(x)int
dir(x)['__abs__',
'__add__',
'__and__',
'__bool__',
'__ceil__',
'__class__',
'__delattr__',
'__dir__',
'__divmod__',
'__doc__',
'__eq__',
'__float__',
'__floor__',
'__floordiv__',
'__format__',
'__ge__',
'__getattribute__',
'__getnewargs__',
'__getstate__',
'__gt__',
'__hash__',
'__index__',
'__init__',
'__init_subclass__',
'__int__',
'__invert__',
'__le__',
'__lshift__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__neg__',
'__new__',
'__or__',
'__pos__',
'__pow__',
'__radd__',
'__rand__',
'__rdivmod__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rlshift__',
'__rmod__',
'__rmul__',
'__ror__',
'__round__',
'__rpow__',
'__rrshift__',
'__rshift__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__trunc__',
'__xor__',
'as_integer_ratio',
'bit_count',
'bit_length',
'conjugate',
'denominator',
'from_bytes',
'imag',
'is_integer',
'numerator',
'real',
'to_bytes']
x.is_integer()True
y = 2.1
dir(y)['__abs__',
'__add__',
'__bool__',
'__ceil__',
'__class__',
'__delattr__',
'__dir__',
'__divmod__',
'__doc__',
'__eq__',
'__float__',
'__floor__',
'__floordiv__',
'__format__',
'__ge__',
'__getattribute__',
'__getformat__',
'__getnewargs__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__int__',
'__le__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__neg__',
'__new__',
'__pos__',
'__pow__',
'__radd__',
'__rdivmod__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rmod__',
'__rmul__',
'__round__',
'__rpow__',
'__rsub__',
'__rtruediv__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__trunc__',
'as_integer_ratio',
'conjugate',
'fromhex',
'hex',
'imag',
'is_integer',
'real']
y = 2.1
y.is_integer()False
class Table2():
def __init__(self):
self._legs = 4
def num_legs(self):
return 10tbl3 = Table2()# Definitely do not do this
# we did it to be silly and to highlight the dangers of Python
tbl3.__class__ = Table tbl3.num_legs()4
class Table3():
def __init__(self):
self._not_legs = 4tbl4 = Table()tbl4.__class__ = Table3tbl4._legs10