Skip to main content

Chapter 9 - Dictionaries in Python

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

Popular posts from this blog

A Complete Guide to Developing Web Applications for Personal/Business Purposes (2023)

  In the early days of the internet, webpages were static, often featuring basic images and, occasionally, videos. However, the concept of web application development remained distant until around 2005 when the introduction of Ajax marked a turning point. Ajax brought forth the possibility of creating more sophisticated, faster, and interactive web applications. Fast forward to 2023, web applications have seamlessly integrated into our daily lives, often without us realizing it. From widely used applications like MS Word and PowerPoint to popular platforms like twitter, and even the ubiquitous Facebook, these applications offer personalized, immersive experiences comparable to native apps, accessible directly from web browsers. Modern web applications combine the user-centric nature of native mobile apps with the convenience of browser accessibility across various devices. This convergence makes web application development a coveted skill among developers, and more importantly,...

Free Self-taught Education in Programming/Computer Science!

    Contents Summary Community Curriculum Code of conduct Team Summary The programming skills for everyone is a  complete  computer science/programming  education path based solely on freely available online materials. It's for you if you want a proper,  well-rounded  basis in concepts fundamental to all programming and computing disciplines. You need to possess the discipline, will, and (most importantly!) good habits to obtain this skills largely on your own, but with support from a community of fellow learners. It is designed according to the degree requirements of undergraduate computer science majors, minus general education (non-CS) requirements, as it is assumed most of you are already educated/have knowledge outside the field of programming/CS. The courses themselves are among the very best in the world, often coming from Harvard, Princeton, MIT, etc. The coursework is also supplemented with relevant books when necessary. When there are courses...