FP Ta 05
FP Ta 05
1.1 Lists
1.1.1 Basic List operations
[1]: a = [1, 3, 5]
print(a[0])
print(a[-1])
1
5
[2]: a = [1, 3, 5]
for i in a:
print(i)
1
3
5
[3]: a = [1, 3, 5]
for i in range(len(a)):
print(a[i])
1
3
5
for i in a:
i = -1
1
print(a)
[1, 3, 5]
[5]: # We can keep both index and value. This is called enumerating.
idx = 0
for i in a:
print(idx, i)
idx += 1
0 1
1 3
2 5
[1, 2, 4, 5]
[1, 2, 3, 4]
[4]: n = int(input())
a = []
for i in range(n):
a += [int(input())]
print(a)
3
1
2
3
[1, 2, 3]
[7]: a = []
t = int(input())
while t != -1:
a += [t]
2
t = int(input())
print(a)
1
2
3
4
-1
[1, 2, 3, 4]
[6]: a = ["Hello", 1, True, False, "Hooray!"] # Lists can hold multiple types of␣
↪data.
for i in a:
print(type(i))
<class 'str'>
<class 'int'>
<class 'bool'>
<class 'bool'>
<class 'str'>
print(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
for x in a:
b += [x]
print(b)
[1, 2, 3, 4]
3
values will not be affected by copying, so we must deep-copy the object. The definitions are not
important; The only important part here is that you must know what happens when you assign a
variable with type list to another.
[11]: a = list(range(1, 100, 4))
b = a
print(a)
print(b)
b += [10]
print(a)
print(b)
print("-----------------------PT.2-----------------------")
b[0] = 2
print(a)
print(b)
# The reason? As a bonus, I will explain the extra behind the curtain of python.
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97]
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97]
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97, 10]
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97, 10]
-----------------------PT.2-----------------------
[2, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97, 10]
[2, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77,
81, 85, 89, 93, 97, 10]
b += [x]
print(a)
print(b)
4
[1, 5, 9, 13, 17, 21, 25, 29]
[1, 5, 9, 13, 17, 21, 25, 29, 29]
# When no arguments passed, it splits the string from the white spaces.␣
↪Otherwise, it splits from the given character.
s1 = "1 2 \n 3 4 56"
print(s1.split())
s2 = "1,2,3,4,5"
print(s2.split(","))
words = words.split()
[1, 2, 3]
1
[15]: # PRACTICE: Now tell why this code does not work:
# Let's say, we are reading some grid of numbers from the input, and...
5
n = int(input())
grid = [[0] * n] * n
for i in range(n):
for j in range(n):
grid[i][j] = int(input())
print(grid)
2
1
2
3
4
[[3, 4], [3, 4]]
[17]: # CORRECTED: Multiplying does not work, even if we use `list(...)`. So, we MUST␣
↪use for for this one, or lsit comprehension which we will learn in future.
n = int(input())
grid = []
for i in range(n):
grid += [[0] * n]
for i in range(n):
for j in range(n):
grid[i][j] = int(input())
print(grid)
2
1
2
3
4
[[1, 2], [3, 4]]
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
print(a)
6
1 2 3
[1, 2, 3]
[25]: # Why this one doesn't work? It has to do with integer assignment. Python's␣
↪integers are immutable objects.
a = input().split()
for x in a:
x = int(x)
print(a)
1 2 3
['1', '2', '3']
[28]: # Practice: Read three numbers, n, m and p from input. Then, read an m*n and an␣
↪n*p matrix, and multiply them and print the result.
m, n, p = input().split()
m = int(m)
n = int(n)
p = int(p)
a = []
b = []
for i in range(m):
nums = input().split()
for i in range(len(nums)):
nums[i] = int(nums[i])
a += [nums]
for i in range(n):
nums = input().split()
for i in range(len(nums)):
nums[i] = int(nums[i])
b += [nums]
c = []
7
for i in range(m):
c += [[0] * p]
for i in range(m):
for j in range(p):
c[i][j] = 0
for k in range(n):
c[i][j] += a[i][k] * b[k][j]
for i in range(m):
for j in range(p):
print(c[i][j], end='\t')
print()
2 2 2
1 2
3 4
5 6
7 8
19 22
43 50
2 Sorts
In this part, we will learn about some of the sort algorithms in python. A sort algorithm is an
algorithm that sorts a
[29]: # Insertion sort
a = [10, 8, 3, 2, 12, 20, 1, 5, 5]
j += 1
a[j] = key # Place the card to insert.
print(a) # This algorithm works because the first i elements of the list are␣
↪always sorted. And we know how to add another number in between.
8
[30]: # Bubble Sort
a = [10, 8, 3, 2, 12, 20, 1, 5, 5]
print(a)
# Find minimum
for j in range(i, len(a)):
if a[j] < val:
idx = j
val = a[j]
tmp = a[i - 1]
a[i - 1] = val
a[idx] = tmp
print(a)
[32]: # Let's say we want to sort a list of numbers, but we know that 0 <= a[i] <=␣
↪1e5. What can we do?
for i in a:
cnt[i] += 1
a = []
for i in range(len(cnt)):
a += [i] * cnt[i]
9
print(a)
10