Engineering Python · Data & Control
Lists and Indexing
In Engineering Python because measurements and feature rows arrive as ordered sequences — indexing, slicing and mutation are how you touch them.
A list is an ordered, mutable sequence. Indexing picks one element; slicing picks a range; assignment through an index changes the list in place.
- Engineering Python
- Easy level
- 4 concepts
1Building a list
Square brackets build a list: `[10, 20, 30]`. Elements keep order and may be mixed types, though labs usually keep one type.
Figure. Square brackets build a list. temps = [20, 21, 19] keeps that order.
List
temps = [20, 21, 19]
print(temps)What does `[1, 2, 3]` create?
- A list of three ints
- A string
- A dict
Square brackets with commas make a list.
2Indexing and slicing
Index `0` is the first element. Negative indices count from the end (`-1` is last).
Slice `a[1:3]` takes from index 1 up to but not including 3.
Figure. Index 0 is the first element. -1 is last. Slice a[1:3] takes from 1 up to but not including 3.
Takeaway
- IdeaIndex `0` is the first element. Negative indices count from the end (`-1` is last).
Index and slice
a = [10, 20, 30, 40]
print(a[0], a[-1])
print(a[1:3])For the list items = ["a", "b", "c", "d"], what does items[1:3] return?
- ["a", "b", "c"]
- ["b", "c"], slicing from index 1 up to but excluding index 3
- ["b", "c", "d"]
- ["c", "d"]
Python slice [start:stop] includes the start index and excludes the stop index, taking indices 1 and 2.
3Mutating a list
`a[i] = value` changes one slot. `append` grows the list. The name still refers to the same list object.
Figure. a[i] = value changes one slot. append grows the list. The name still refers to the same list object.
Takeaway
- Idea`a[i] = value` changes one slot. `append` grows the list. The name still refers to the same list object.
Mutate
a = [1, 2, 3]
a[1] = 9
a.append(4)
print(a)What does items.append("z") do to an existing list object items = ["a", "b"]?
- It creates and returns a new list without modifying items
- It mutates items in-place, adding "z" to the end and returning None
- It replaces the first element of items with "z"
- It reverses the order of existing list elements
list.append() is an in-place mutation method that appends the item to the end of the existing list and returns None.
4Lab: mutate a small list
Change one element and append another, then print the list.
Figure. Set index 0 to 100, append 4, and print the list. Stdout contains 100.
Takeaway
- IdeaChange one element and append another, then print the list.
Coding lab. Mutate a list runs in the app, with checks on your output.
Notes
- In Engineering Python because measurements and feature rows arrive as ordered sequences — indexing, slicing and mutation are how you touch them.
- Square brackets build a list: `[10, 20, 30]`. Elements keep order and may be mixed types, though labs usually keep one type.
- Index `0` is the first element. Negative indices count from the end (`-1` is last).
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Lists are ordered and mutable; index from 0; slices exclude the end.
- Building a list
- Square brackets build a list: `[10, 20, 30]`. Elements keep order and may be mixed types, though labs usually keep one type.
- Indexing and slicing
- Index `0` is the first element. Negative indices count from the end (`-1` is last).
- Mutating a list
- `a[i] = value` changes one slot. `append` grows the list. The name still refers to the same list object.
- Lab: mutate a small list
- Change one element and append another, then print the list.
Practise Lists and Indexing
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 3 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device