Gaddis Python 6e Chapter 07
Chapter 7: Lists and Tuples
Overview
Covered topics include Lists, Tuples, and the matplotlib Package for plotting data.
Sequences
Definition: An object that contains multiple items of data.
Storage: Items stored in sequence, one after another.
Types in Python: Lists (mutable) and Tuples (immutable).
Introduction to Lists
List: An object containing multiple data items.
Elements: Individual items in a list.
Format:
list = [item1, item2, ...]Types of Items: Can hold different data types.
Display: Use the
printfunction to display a list.Conversion: The
list()function converts certain objects to lists.
Repetition Operator and Iterating over a List
Repetition Operator:
*makes multiple copies of a list.General Format:
list * nfor n copies.Iteration: Use a for loop to iterate over a list.
Format:
for x in list:
Indexing
Index: Number specifying the position of an element in a list.
Access: First element index is 0, second is 1, and n’th is n-1.
Negative Indexes: Identify positions from the end of the list.
Example:
-1for the last element.
The len Function
Purpose:
len()returns the length of a sequence.Example:
size = len(my_list)gives the number of elements.Prevents
IndexErrorduring list iteration.
Lists Are Mutable
Mutable Sequence: Can change items in the sequence.
Updating Elements: Use
list[index] = new_value.Valid index necessary to avoid
IndexError.
Concatenating Lists
Concatenate: Use the
+operator to join two lists.Cannot concatenate with different data types.
Augmented Assignment: Use
+=for concatenation.
List Slicing
Slice: A span of items taken from a list.
Format:
list[start:end]produces a new list.Defaults: If start is not specified, defaults to 0; if end is not specified, defaults to len(list).
Can include step value and negative indexes.
Finding Items in Lists with the in Operator
Usage:
item in listchecks if an item is in a list.Returns:
Trueif found,Falseif not.Not in Operator: Used to check if an item is not in the list.
List Methods and Useful Built-in Functions
append(item): Adds an item to the end of the list.
count(item): Returns occurrences of an item.
index(item): Returns the first index of an item.
insert(index, item): Inserts an item at a specified index.
sort(): Sorts list elements in ascending order.
remove(item): Removes the first occurrence of an item.
reverse(): Reverses the list order.
del statement: Removes an element from a specific index.
Sum, min, max: Built-in functions for totals and extremes.
Copying Lists
Methods: To copy a list, use a for loop or concatenate.
Creating a new empty list ensures complete copying.
Processing Lists
List elements can be utilized in calculations.
Total Calculation: Use a loop and an accumulator.
Average: Total divided by
len(list).Lists can be passed to functions and saved to files.
List Comprehensions
Definition: Concise expression for creating new lists.
Example:
list2 = [item for item in list1]creates a copy of a list.Can include conditional statements:
list2 = [item for item in list1 if item < 10].
Two-Dimensional Lists
Definition: A list that contains other lists; often referred to as nested lists.
Usage: Useful for handling multiple data sets with two indexing levels.
Tuples
Definition: An immutable sequence; elements cannot be added, removed, or changed.
Creation: Use parentheses
my_tuple = (item1, item2).Support various list operations but lack certain methods like append, remove, and sort.
Can store mutable objects such as lists within tuples.
Plotting Data with Matplotlib
Library: Not part of standard library; requires separate installation.
Installation: Example commands provided for Windows, Mac, and Linux.
Importing: Use
import matplotlib.pyplot as pltfor plotting functions.
Plotting Types
Line Graphs: Created using
plt.plot(x_coords, y_coords).Bar Charts: Created using
plt.bar(left_edges, heights); modify bar width and colors can be specified.Pie Charts: Created using
plt.pie(values, labels), which visualizes proportions of datasets.
Summary
This chapter covered Lists, Tuples, processing techniques, and the matplotlib Package for data visualization.