(use the Space key to navigate through all slides)
Prof. Andrea Gallegati |
Prof. Dario Abbondanza |
list
A list is an ordered collection of items (objects)
numbers = [1, 2, 3, 7, 300]
they are mutable, you can change, add or remove elements without creating a new list
numbers[3] = 0
numbers = numbers + ['hi']
numbers.remove(300)
numbers = [1, 2, 4, 17, -8, 300]
# Access elements by index
numbers[0]
numbers[3]
# Negative indexing
numbers[-1]
# Slicing the list
# to get sub-lists
start = 2
end = 4
numbers[start:end]
To access a subset of its elements
append(x): Add x to the end of the list.
insert(index, x): Insert x at a specific index.
remove(x): Remove the first occurrence of x.
pop(index): Remove and return the item at index (default is the last item).
sort(): Sort the list in place.
count(x): Count how many times x appears in the list.
Functions that are proper of the list class
Functions that are proper of the list class
Is commonly used with lists
numbers = [1, 2, 4, 17, -8, 300]
for n in numbers:
print(n)
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations