BackPython Collections: Lists, Strings, Dictionaries, and Basic Statistics
Study Guide - Smart Notes
Tailored notes based on your materials, expanded with key definitions, examples, and context.
Python Collections
Introduction to Python Collections
Python provides several built-in data structures for storing and organizing data. Collections are fundamental for managing data in programs, enabling efficient access, modification, and analysis. The main types of collections in Python are sequential collections (such as lists and strings) and non-sequential collections (such as dictionaries).
Lists are used to store ordered, heterogeneous data.
Dictionaries store associative data as key-value pairs.
Strings are sequences of characters.
Sequential Collections: Lists and Strings
Lists
A list in Python is a mutable, ordered collection of items, which can be of different types. Lists are defined using square brackets [ ] and can be indexed and sliced.
Definition: A list is a sequence of Python objects, delimited inside square brackets.
Example: myList = [3, "cat", 6.5, 2]
Lists are mutable, meaning their contents can be changed after creation.
Figure 4.2: Sequential storage of the elements in a list with indices:
Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
Value | 3 | 'cat' | 6.5 | 2 |
Strings
A string is a sequence of characters. Unlike lists, strings are immutable, meaning their contents cannot be changed after creation.
Strings are defined using single or double quotes.
Individual characters can be accessed by indexing, but cannot be changed.
Operations on Python Sequences
Common Sequence Operations
Python provides several operations that can be performed on sequences such as lists and strings.
Operation Name | Operator/Function | Explanation |
|---|---|---|
Indexing | [ ] | Accesses an element of a sequence by its position. |
Concatenation | + | Combines sequences of the same type. |
Repetition | * | Concatenates a repeated number of times. |
Membership | in | Asks whether an item is in a sequence. |
Non-membership | not in | Asks whether an item is not in a sequence. |
Length | len | Asks the number of items in the sequence. |
Slicing | [ : ] | Extracts a part of a sequence. |
Additional info: Lists support the del statement to delete items, which is not available for strings.
Mutability of Lists and Strings
Lists are mutable, allowing modification of their elements, while strings are immutable and do not support item assignment.
List items can be changed by assignment.
String items cannot be changed; attempting to do so raises an error.
List Methods
Common List Methods
Python lists provide several methods for manipulating their contents.
Method or Function Name | Use | Explanation |
|---|---|---|
list(sequence) | Function | Creates a list from the elements in sequence. |
append(item) | Method | Adds an item to the end of a list. |
insert(i, item) | Method | Inserts item at position i. |
pop() | Method | Removes and returns the last item. |
pop(i) | Method | Removes and returns item at position i. |
sort() | Method | Arranges items in order. |
reverse() | Method | Reverses the order of items. |
index(item) | Method | Returns the index of the first occurrence of item. |
count(item) | Method | Returns the number of occurrences of item. |
remove(item) | Method | Removes the first occurrence of item. |
clear() | Method | Removes all items from the list. |
copy() | Method | Returns a copy of the list. |
Creating Lists
Using the list() Function
The list() function can be used to create lists from sequences such as ranges or strings.
Example: list(range(10)) produces [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Example: list("the quick fox") produces ['t', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'f', 'o', 'x']
Dictionaries
Introduction to Dictionaries
A dictionary is a non-sequential collection that stores data as key-value pairs. Dictionaries are defined using curly braces {} and allow fast lookup of values by their keys.
Definition: A dictionary is a collection of associated key-value pairs.
Example: myDict = {"name": "Alice", "age": 25}
Dictionaries are mutable and can be modified after creation.
Dictionary Operations and Methods
Operator/Method | Explanation |
|---|---|
[key] | Accesses the value associated with a key. |
in | Checks if a key exists in the dictionary. |
del | Deletes a key-value pair. |
keys() | Returns a list of all keys. |
values() | Returns a list of all values. |
items() | Returns a list of key-value pairs. |
get(key) | Returns the value for a key, or None if not found. |
update(dict2) | Updates the dictionary with another dictionary. |
Elementary Statistics with Python
Basic Statistical Functions
Python provides built-in functions and modules to compute elementary statistics on lists of data. These include minimum, maximum, sum, range, mean, median, and mode.
min(sequence): Returns the smallest item in the sequence.
max(sequence): Returns the largest item in the sequence.
sum(sequence): Returns the sum of the items in a numeric sequence.
Range:
Mean (Average):
Median: The middle value in a sorted list. If the list has an even number of items, the median is the average of the two middle values.
Mode: The value(s) that appear most frequently in the list.
Standard Deviation
Standard deviation measures the dispersion of data points from the mean. It is calculated as:
Data Visualization: Histograms
Producing a Histogram Using Matplotlib
Histograms are graphical representations of the distribution of numeric data. In Python, the matplotlib.pyplot module is commonly used to create histograms.
Frequency count data can be visualized using plt.hist() from matplotlib.pyplot.
Histograms help in understanding the spread and central tendency of data.
Summary Table: Python Collections
Type | Sequential | Non-sequential |
|---|---|---|
Strings | Yes | No |
Lists | Yes | No |
Dictionaries | No | Yes |
Additional info:
Lists and dictionaries are mutable, while strings are immutable.
Python's statistics module provides functions such as mean(), median(), mode(), and stdev() for statistical analysis.
Data must be collected and stored before analysis can be performed.