Python Lists and Arrays

A list is a collection of data objects in Python. Lists are a compact way to store and manipulate large data sets. They can be used in loops to analyze each of the entries in the list or create a database. Matrices and vectors are arrays of numbers that may be processed with standard Python commands or created and manipulated with the NumPy package.


Generative AI Learning

Write-it-yourself class: type the examples; the AI quizzes and plants traps. Use these prompts after working with lists and numpy arrays yourself.

"Quiz me with 5 questions, one at a time, on Python lists and numpy arrays: why indexing starts at 0 and what index the last element has, what the slice x[1:4] includes and excludes, what happens with b = a followed by b[0] = 99 when a is a list (aliasing), what + does for two lists versus two numpy arrays, and why numpy arrays are preferred for numerical data. Ask me to predict the printed output for at least two short snippets before you confirm. Grade my answers and list my misconceptions."
"Give me 5 one-line Python snippets using lists and numpy arrays where the output is commonly guessed wrong (slicing off the end, negative indices, aliasing, list*2 vs array*2, mixing int and float division). Show them one at a time; I will predict each output before you reveal it. Keep score and summarize which misconception each trap targets."

Discussion Questions

  1. The slice temps[0:10] of a 10-element list returns how many values, and why does temps[10] fail while temps[-1] works?
  2. After b = a and b.sort(), the 'original' list a is also sorted. Explain what a and b actually are, and give the one change that makes b an independent copy.
  3. A column of 8760 hourly sensor values needs a unit conversion. Compare doing it with a list and a loop versus one numpy expression - what changes in code length, speed, and the kinds of bugs possible?

Course Information

Excel and VBA

Python

MATLAB

MathCAD

Related Courses

Admin