Professional Documents
Culture Documents
ENGR 101
Introduction to Programming
Week 9
Lists
• A list of mumbo-jumbo
['spam', 2.0, 5, [10, 20]]
• Other examples
cheeses = ['Cheddar', 'Edam', 'Gouda']
numbers = [17, 123]
Cheddar
[17, 5]
s1 = "Alice"
s2 = s1
s1 = "Bob"
print(s2)
list = [5, 6]
list2 = list
list[0] = 10
print(list2)
True
print('Brie' in cheeses)
False
[1, 2, 3, 4, 5, 6]
• List methods are all void; they modify the list and return
None. If you accidentally write t = t.sort(), you will be
disappointed with the result.
• As usual, the slice selects all the elements up to, but not
including, the second index.
def delete_head(t):
del t[0]
letters = ['a', 'b', 'c']
delete_head(letters)
print(letters)
['b', 'c']
[1, 2, 3]
print(t2)
None
[1, 2, 3]
print(t1 is t3)
False
result[i] = 2*numbers[i]
• He then called for his servant 2 and ordered him to switch the locks on cells
2,4,6,…
• Similarly, the king continued to call for servants until the nth servant (servant
i switched cells i, 2i, 3i,...).
• Prisoners whose cells are unlocked at the very end are set free.
• Write a function that inputs n and returns the list of cell ids for which
the corresponding prisoner is free?
ENGR 101 – Introduction to Programming by Dr. Çakmak and Dr. Nergiz