week1 Wed

week1 Wed#

# what is the output 
# (<-) true
# (->) false

0.8 - 0.7 == 0.1
False
# create a list of all number in [1,100] that are divisible by 7
# how many of them
# (<-) 15
# (->) 14
# ( ) 13

x = []
for i in range(1,101):
    if i % 7 == 0:
        x.append(i)

print(x)
len(x)
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
14
# what is x in the end
# (<-) [1, 'hello']
# (->) [[], 1, 'hello']
# ( ) error
x = [True, 1, 'hello']
x[0] = []
x
[[], 1, 'hello']
# How to remove the first element
x = [True, 1, 'hello']

x.remove(x[0])
print(x)
[1, 'hello']
x = [True, 1, 'hello']
x = x[1:]
print(x)
[1, 'hello']
x = [True, 1, 'hello']
y = x.pop(0)
print(x)
print(y)
[1, 'hello']
True
x = [True, 1, 'hello']
del x[0]
print(x)
[1, 'hello']
# what is printed
# (<-) 1, 2
# (->) 1, 1
# ( ) 2, 2
a = 1
b = a
b = 2
print(a,b)
1 2
# what is "a"
# (<-) [1,2,3]
# (->) [0,2,3]
# ( ) [2,2,3]
a = [1,2,3]
b = a
b[0] = 0
print(a)
[0, 2, 3]
a[-1]=100
b
[0, 2, 100]
# This create two different lists
a = [1,2,3]
b = []
for x in a:
    b.append(x)
print(b)

# more convenient way, list comprehension, deepcopy
[1, 2, 3]
b[0] = 0
print(a)
[1, 2, 3]
# what is "a"
# (<-) [1,2,3]
# (->) [1,2,3,4]
# ( ) [2,3,4]

a = [1,2,3]
b = a + [4]
print(a)
print(b)
[1, 2, 3]
[1, 2, 3, 4]
# what is printed
# (<-) 5
# (->) 6
# ( ) 3

x = [[1,2,3], [4,5,6]]
print(x[1][2])
6
x[1]
[4, 5, 6]
x[1][2]
6
# what is the output
# (<-) 4
# (->) 3
# ( ) 2
x = "abcd"
len(x)
4
# example of f-string (formatted string)
x = 5/3
y = f"x is {x}"
print(y)
x is 1.6666666666666667
x = True
y = f"x is {x}"
print(y)
x is True
# create a list [0, 1, ... 9] in one line

[x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = []
for i in range(10):
    x.append(i)
print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = list(range(10))
print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# range is a object
y = range(10)
type(y)
range
# list() convert the range object to a list
x = list(y)
print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# create a list of all number in [1,100] that are divisible by 7
y = [x for x in range(1,101) if x%7==0]
print(y)
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
# how define a new list, that contains all the positive elements in x
x = [3,     1, -2, 10, -5]
y = [i for i in x if i>0]
print(y)
[3, 1, 10]