pressures = [0.273, 0.275, 0.277, 0.275, 0.276]
print('pressures:', pressures)
print('length:', len(pressures))pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5
append, extend, and del.When you need to work with a collection of related values — pressures from a sensor, times in an experiment, filenames to process — storing them in separate variables like pressure_001, pressure_002 quickly becomes unmanageable. A list groups any number of values into a single object. Lists are written with square brackets, with values separated by commas:
pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5
Lists may contain values of any type, and different types can be mixed in the same list — though in practice a list of the same type is more common and easier to think about.
List elements are accessed by position using square brackets, exactly like string characters. Indexing starts at zero, and negative indices count from the end:
zeroth item of pressures: 0.273
fourth item of pressures: 0.276
Slices extract a portion of the list using [start:stop] notation, returning a new list. The start index is included; stop is excluded:
[0.275, 0.277, 0.275]
[0.273, 0.275, 0.277]
[0.277, 0.275, 0.276]
Going out of bounds in a slice never raises an error — Python just returns as many elements as are available. Going out of bounds with a direct index raises an IndexError:
Unlike strings, lists are mutable — you can change their contents after creation. Assign directly to an indexed position to replace a value:
pressures is now: [0.265, 0.275, 0.277, 0.275, 0.276]
del removes an element by index and shortens the list:
append is a list method — a function attached to the list object — that adds a single item to the end:
primes has become: [2, 3, 5, 7, 9]
extend merges another list into the end of the existing list, keeping the result flat. If you append a list instead, the result is a nested list — a list inside a list:
after extend: [2, 3, 5, 7, 9, 11, 13, 17, 19]
after append of a list: [2, 3, 5, 7, 9, 11, 13, 17, 19, [37, 41, 43, 47]]
The empty list [] is a useful starting point for collecting values inside a loop.
Use help(list) to see the full set of available methods.
Strings behave like lists in many ways — they support indexing and slicing, and len measures their length — but they are immutable: you cannot change individual characters after creation:
zeroth character: c
third character: b
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 element[0] = 'C' TypeError: 'str' object does not support item assignment
If you need a modified string, create a new one — for example, 'C' + element[1:] produces 'Carbon'.
Fill in the blanks so that the program produces the output shown.
first time: [1, 3, 5]
second time: [3, 5]
If low and high are both non-negative integers, how many elements does values[low:high] contain?
high - low elements. For example, values[1:4] contains the 3 elements at indices 1, 2, and 3.
string to list: ['t', 'i', 'n']
list to string: gold
list('some string') do?'-'.join(['x', 'y']) produce?'x-y' — join concatenates the list elements, inserting the separator string between each pair.What does the following program print?
values is a list, what does del values[-1] do?values?The program prints m.
-1 is the last element, -2 is second to last, and so on.-N, which refers to the first element.values[:-1] — slice from the start up to but not including the last element.What does the following program print?
[low:high:stride] do?furn
eniroulf
stride is the step size between selected elements.collection[::2] starts at index 0 and takes every second element (0, 2, 4, …).What do these two programs print? Explain the difference between sorted(letters) and letters.sort().
letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']
letters is ['d', 'g', 'l', 'o'] and result is None
Program A:
letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']
Program B:
letters is ['d', 'g', 'l', 'o'] and result is None
sorted(letters) returns a new sorted list and leaves the original unchanged. letters.sort() sorts the list in-place and returns None. Choose sorted when you need to keep the original; choose .sort() when you do not.
What do these two programs print? Explain the difference between new = old and new = old[:].
new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
Program A:
new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
Program B:
new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
new = old makes new an alias for the same list object, so modifying one modifies both. new = old[:] creates an independent copy, so changes to new do not affect old.
list[index] (zero-based) and extract sub-sequences with slices.append or extend to add, and del to remove.extend flattens a list into another; append adds it as a single (possibly nested) element.IndexError; out-of-bounds slicing returns an empty list.