CS105 Study Guide
Unit 4: Data Structures I – Lists and Strings
4a. Explain lists and indexing
- What is a list?
- How are elements within a list accessed?
- What are some useful applications of lists?
A list is a Python container that houses a mutable sequence of values. The term sequence means that the elements contained within the list are ordered and can be referred to using an index. In contrast, Python data structures such as sets and dictionaries are not sequences.
Lists can contain elements that are of any valid Python data type. List elements start with an index value of 0. Therefore, if a list contains 15 elements, element indices range from 0 to 14. It is extremely important that you master how to index elements within a list.
An empty list (containing zero elements) can be created with a command such as
alist = list()
alist = [ ]
using the standard left square bracket-right square bracket notation to build the list container.
Lists can be used to house records that contain different data types. For example, a list could contain information such as a name (of data type str), salary (of data type float), room number (of data type int), and so on. Because of the indexing property, lists can be applied in tandem with loops in order to generate a computation using the list elements.
4b. Write simple programs that apply list and string methods
- What is the syntax for invoking a list or a string method?
- What does the append method do?
- What does the join method do?
The true beauty of Python is that every variable referred to in a program is an object derived from a specific class. This means that every object is endowed with the methods defined within the object's class. The syntax for referring to a method uses the dot notation. For example:
will call the append method in order to append a value of 30 to the variable
Most Python editors within an IDE will enable you to view methods associated with a given object after typing the dot after the variable name. This feature can be used to help you remember the set of methods and can help you to complete typing a given command. For example, in Repl.it, typing
z='a string' z.
will generate a temporary window listing the set of possible built-in string methods. For example, the find method will search for a substring contained within the string variable. The join method will join together strings in order to form a new string. As you write more Python programs, you will naturally become familiar with the host of built-in Python methods associated with strings and lists.
4c. Explain and apply slicing
- What is slicing?
- What is the syntax for slicing?
- How can slicing be applied within a Python program?
Slicing is an efficient way of indexing elements within a Python sequenced container. For a given list variable alist, it is more efficient to write
alist[3:8] rather than individually referring to elements as
alist, alist, alist, alist, alist.
The general syntax for applying slicing to a list is:
listname[start index: stop index: step]
where the step refers to how many elements to skip over to reach the next element being referred to. In addition, Python index convention causes the slice to terminate at one less than the stop index. This is a feature that beginner programmers must pay close attention to.
Slicing allows for easy indexing of large numbers of elements. Assuming the initialization:
alist = [3,5,7,9,11,13,15,17,19]
these two snippets of code are equivalent:
zlist=[ ] for i in range(2,6): zlist.append(val)
zlist=[ ] zlist=alist[2:6]
Clearly, slicing has eliminated the need for the loop.
To review, see Slicing.
4d. Write programs that plot and visualize list data
- What is the import command?
- What is matplotlib?
- What is the syntax for plotting one list of data against another list of data?
Python is an amazing language because there are so many modules that are available for computations that go far beyond the basic language. In order to include these modules and access their methods within a given Python program, the
import command must be invoked.
matplotlib is an example of a module that can very readily enable the plotting of data. To use the
matplotlib module, you need to invoke the command
matplotlib module is equipped with a number of methods that allow for the rendering of data and annotating graphs. A basic use case of this module is to plot the data contained in numerical lists. A sequence of commands such as:
import matplotlib.pyplot as plt x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] z=[2,4,6,8,10,12,14,16,18,20] plt.plot(x,z) plt.show()
will plot the data contained in list
z against the data in list
To review, see The matplotlib Library.
Unit 4 Vocabulary
Be sure you understand these terms as you study for the final exam. Try to think of the reason why each term is included.