Week 1 Videos
Contents
Week 1 Videos#
list
vs tuple
vs set
vs dict
vs range
#
mylist = [3,1,4,1,5,9]
type(mylist)
list
mytuple = (3,1,4,1,5,9)
type(mytuple)
tuple
tup2 = tuple(mylist)
tup2
(3, 1, 4, 1, 5, 9)
mytuple == tup2
True
mylist[2]
4
mytuple[2]
4
mylist.append(20.4)
mylist
[3, 1, 4, 1, 5, 9, 20.4]
mytuple.append(20.4)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [12], line 1
----> 1 mytuple.append(20.4)
AttributeError: 'tuple' object has no attribute 'append'
mytuple.append(3)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [13], line 1
----> 1 mytuple.append(3)
AttributeError: 'tuple' object has no attribute 'append'
myset = {3,1,4,1,5,9}
myset[2]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [15], line 1
----> 1 myset[2]
TypeError: 'set' object is not subscriptable
myset
{1, 3, 4, 5, 9}
myset2 = {3,1,[2,1,5]}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [17], line 1
----> 1 myset2 = {3,1,[2,1,5]}
TypeError: unhashable type: 'list'
myset2 = {3,1,(2,1,5)}
myset2
{(2, 1, 5), 1, 3}
len(myset2)
3
d = {3:0, 1:5, 4:10, 1:2}
d
{3: 0, 1: 2, 4: 10}
d[2]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In [23], line 1
----> 1 d[2]
KeyError: 2
d[4]
10
d[(2,10)] = "Chris"
d
{3: 0, 1: 2, 4: 10, (2, 10): 'Chris'}
d[[1,10]] = "Davis"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [27], line 1
----> 1 d[[1,10]] = "Davis"
TypeError: unhashable type: 'list'
d["4"] = [1,10]
d
{3: 0, 1: 2, 4: 10, (2, 10): 'Chris', '4': [1, 10]}
r = range(10)
r
range(0, 10)
list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = range(3,20,5)
tuple(y)
(3, 8, 13, 18)
list(range(3,23,5))
[3, 8, 13, 18]
range(0,10,0.1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [36], line 1
----> 1 range(0,10,0.1)
TypeError: 'float' object cannot be interpreted as an integer
for loops#
Print the string
"Hello, world"
8 times.
for i in range(8):
print("Hello, world")
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Let mylist = [3,1,4,1,-5,-9,2]
.
Print the squares of each element in
mylist
.
mylist = [3,1,4,1,-5,-9,2]
for i in range(7):
print(mylist[i]**2)
9
1
16
1
25
81
4
for i in range(len(mylist)):
print(mylist[i]**2)
9
1
16
1
25
81
4
for x in mylist:
print(x**2)
9
1
16
1
25
81
4
Print the squares of the positive elements in
mylist
.
mylist
[3, 1, 4, 1, -5, -9, 2]
for x in mylist:
if x > 0:
print(x**2)
9
1
16
1
4
Make a new list containing
x-2
for each elementx
inmylist
.
newlist = []
for x in mylist:
newlist.append(x-2)
newlist
[1, -1, 2, -1, -7, -11, 0]
Functions in Python#
def f(x):
return x**2 - 5
f(7)
44
def g(x):
print(x**2 - 5)
g(7)
44
y = f(7)
z = g(7)
44
y
44
z
z+2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [9], line 1
----> 1 z+2
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
type(z)
NoneType
def check(x,y):
z = x + 2*y
if z > 10:
return z
elif z > 5:
return 2*z
else:
return "Choose bigger numbers"
check(2,1)
'Choose bigger numbers'
check(2,2)
12
x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In [14], line 1
----> 1 x
NameError: name 'x' is not defined
y
44
f2 = lambda w: w**2 - 5
f2(10)
95