0% found this document useful (0 votes)
39 views10 pages

FP Ta 05

The document outlines a lecture on lists and their operations in Python, covering basic operations, appending, copying, and list functions. It includes examples of list manipulation, sorting algorithms, and the importance of understanding how Python handles list references. Additionally, it provides practice exercises related to list operations and sorting methods.

Uploaded by

yalda.hajilito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

FP Ta 05

The document outlines a lecture on lists and their operations in Python, covering basic operations, appending, copying, and list functions. It includes examples of list manipulation, sorting algorithms, and the importance of understanding how Python handles list references. Additionally, it provides practice exercises related to list operations and sorting methods.

Uploaded by

yalda.hajilito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

S05

December 14, 2024

1 FOP Session 05 TA class lectures


In this session, we will cover lists and its operations. Along with algorithms related to them.

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

[4]: # What happens?


a = [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.1.2 Appending to list


We can use += operator. It concatenates two lists and saves it to the left-hand-side list.
[3]: print([1, 2] + [4, 5])
a = [1, 2]
b = [3, 4]
a = a + b
print(a)

[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'>

1.1.3 Multiplying a list, etc.

[8]: # Let's say we have a list a:


a = [1, 2, 3]
a = a * 3 # It's wonderful that such an action is acceptable!

print(a)

[1, 2, 3, 1, 2, 3, 1, 2, 3]

[10]: # PRACTICE: Reverse a list


a = [1, 2, 3, 4]
b = []

for x in a:
b += [x]

print(b)

[1, 2, 3, 4]

1.1.4 Copying lists.


In this part, we’ll learn about something called pass by reference. Well, the actual definition is a
little different, but the idea is the same. If we copy a pyhton’s non-atomic object (such as list), the

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]

[12]: # This way we solve the problem


a = []

for x in range(1, 30, 4):


a += [x]

b = list(a) # For now, just use this method instead.

b += [x]

print(a)
print(b)

4
[1, 5, 9, 13, 17, 21, 25, 29]
[1, 5, 9, 13, 17, 21, 25, 29, 29]

1.1.5 List functions


[19]: # Given a string, we can split it and convert it into list using the split␣
↪method.

# 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(","))

['1', '2', '3', '4', '56']


['1', '2', '3', '4', '5']

[22]: words = input()

words = words.split()

for word in words:


print(word)

Hello everybody. Let's test what happens.


Hello
everybody.
Let's
test
what
happens.

[21]: # Well, lists can hold lists in themselves...


a = [[1, 2, 3], [4, 5, 6]] # We can think of them as matrices.

# We can index a 2D list with two indices:


print(a[0])
print(a[0][0])

[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]]

[18]: # Let's say we want to read a line of numbers:

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]

for i in range(1, len(a)):


key = a[i] # We keep the card we want to insert.
j = i - 1 # We iterate over each card, and if it has a greater value, push␣
↪it forward.

while j >= 0 and a[j] >= key:


a[j + 1] = a[j]
j -= 1

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.

[1, 2, 3, 5, 5, 8, 10, 12, 20]

8
[30]: # Bubble Sort
a = [10, 8, 3, 2, 12, 20, 1, 5, 5]

for i in range(len(a), 0, -1):


for j in range(0, i - 1):
if a[j] > a[j + 1]:
tmp = a[j]
a[j] = a[j + 1]
a[j + 1] = tmp

print(a)

[1, 2, 3, 5, 5, 8, 10, 12, 20]

[31]: # Selection sort


a = [10, 8, 3, 2, 12, 20, 1, 5, 5]

for i in range(1, len(a)):


idx = i - 1
val = a[i - 1]

# 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)

[1, 2, 3, 5, 5, 8, 10, 12, 20]

[32]: # Let's say we want to sort a list of numbers, but we know that 0 <= a[i] <=␣
↪1e5. What can we do?

cnt = [0] * int(1e5 + 1)


a = [10, 8, 3, 2, 12, 20, 1, 5, 5]

for i in a:
cnt[i] += 1

a = []

for i in range(len(cnt)):
a += [i] * cnt[i]

9
print(a)

[1, 2, 3, 5, 5, 8, 10, 12, 20]

[34]: # This one's a mystery... If you know, you know.


print(chr(sum(range(ord(min(str(not())))))))

10

You might also like