Chapter 9 of the Python textbook, Python for Everybody: Exploring Data Using Python 3 by Dr Charles R. Severance, covers dictionaries, which are fundamental data structures in Python. Here are the significant points of each section along with explanations and examples:
Section 9.1: Dictionaries
- Dictionaries
are similar to lists but more general; they map keys to values.
- Keys
can be almost any data type.
- You
can create an empty dictionary using {}.
- To
add items to a dictionary, use square brackets.
Example:
eng2sp = dict()
eng2sp['one'] = 'uno'
Section 9.2: Dictionary as a Set of Counters
- Dictionaries
can be used to count the occurrence of items.
- You
can create a dictionary to count characters in a string or words in a
text.
Example:
word = 'brontosaurus'
counts = dict()
for c in word:
if c not in counts:
counts[c] = 1
else:
counts[c] += 1
Section 9.3: Looping and Dictionaries
- for
loop can be used to iterate over keys in a dictionary.
- You
can iterate over keys, values, or both in a dictionary using dictionary
methods.
- The get()
method simplifies dictionary value retrieval and is a common idiom.
Example:
counts = { 'chuck': 1, 'annie':
42, 'jan': 100}
for key in counts:
print(key, counts[key])
Section 9.4: Advanced Text Parsing
- Dictionaries
can be used to parse text and count occurrences of words.
- You
can use string methods to preprocess text, like removing punctuation and
converting to lowercase.
- Python's
string.punctuation provides a set of punctuation characters.
Example:
import string
line = "But, soft! what
light through yonder window breaks?"
line =
line.translate(line.maketrans('', '', string.punctuation))
line = line.lower()
words = line.split()
Section 9.5: Debugging
- Debugging
becomes essential as data sets get larger.
- Strategies
for debugging include scaling down input, checking summaries and types,
writing self-checks, and pretty-printing output.
This chapter introduces dictionaries as a powerful data
structure for various tasks, including counting and mapping items.
Understanding dictionaries and their usage is crucial for many real-world
programming scenarios.
Key Terms:
- Dictionary
- Hashtable
- Hash
Function
- Histogram
- Implementation
- Item
- Key-Value
Pair
- Lookup
- Nested
Loops
- Value
Conclusion
This chapter introduces dictionaries as a powerful data
structure for various tasks, including counting and mapping items.
Understanding dictionaries and their usage is crucial for
many real-world programming scenarios.
Complete exercise 9 before proceeding to read chapter 10
Chapter 9 of the
Book: Python for Everybody: Exploring
Data Using Python 3 by Dr. Charles R. Severance (Available in pdf:
http://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf).
Comments
Post a Comment