E ExamMaster

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?
  1. A list of three ints
  2. A string
  3. 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

  1. 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?
  1. ["a", "b", "c"]
  2. ["b", "c"], slicing from index 1 up to but excluding index 3
  3. ["b", "c", "d"]
  4. ["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

  1. 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"]?
  1. It creates and returns a new list without modifying items
  2. It mutates items in-place, adding "z" to the end and returning None
  3. It replaces the first element of items with "z"
  4. 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

  1. 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
Continue with Google — freeNo card, no trial. Works offline once installed.