搜尋此網誌

2026年6月10日星期三

Chapter Quiz

In Python, a defaultdict is a special kind of dictionary from the collections module. Its main advantage is that it automatically provides a default value for any key that doesn’t exist yet, instead of raising a KeyError.

You create it by passing in a default factory function (like int, list, or set).

When you access a missing key, defaultdict calls that factory to create a default value and stores it under that key.

In Python, the get() function is a method of dictionaries. It’s used to safely retrieve the value for a given key without risking a KeyError if the key doesn’t exist.


In Python, a set is a built‑in data type that represents an unordered collection of unique elements. Think of it like a mathematical set: no duplicates, and order doesn’t matter.

Unique elements only → duplicates are automatically removed.

Unordered → items don’t have a fixed position or index.

Mutable → you can add or remove elements.

Fast membership tests → checking if something is in a set is very efficient.

# Using curly braces fruits = {"apple", "banana", "cherry"} # Using the set() constructor numbers = set([1, 2, 2, 3]) print(numbers) # {1, 2, 3} (duplicates removed)


In Python, item[1:3] is an example of list slicing (or sequence slicing).

item is assumed to be a list (or another sequence like a string or tuple).

[1:3] means:

    Start at index 1 (the second element, since Python uses zero‑based indexing).

    Stop before index 3 (so it includes elements at positions 1 and 2, but not 3).

In Python, item[1:] is another example of slicing.

1: means start at index 1 (the second element, since indexing starts at 0).

The colon without an end index means go all the way to the end.

So item[1:] returns everything from index 1 onward.


A nested list comprehension in Python is simply a list comprehension inside another one. It’s often used to flatten lists, generate combinations, or apply transformations across multiple levels of data.

[expression for outer in iterable1 for inner in iterable2]

The outer loop runs first, then the inner loop.

You can nest more than two loops if needed.

To apply a function someFunc to all comments in a list of blog objects using a nested list comprehension

[[someFunc(comment) for comment in blog.comments] for blog in blogs]

Outer loopfor blog in blogs → iterate through each blog object.

Inner loopfor comment in blog.comments → iterate through each comment in that blog.

ExpressionsomeFunc(comment) → apply your function to each comment.

Result → a list of lists, where each inner list contains the processed comments for one blog.

def someFunc(text): return text.upper() class Blog: def __init__(self, comments): self.comments = comments blogs = [ Blog(["Nice post!", "Great read"]), Blog(["Interesting idea", "Could be improved"]), Blog(["Loved it", "Thanks for sharing"]) ] processed = [[someFunc(comment) for comment in blog.comments] for blog in blogs] print(processed)

Result:
[['NICE POST!', 'GREAT READ'], ['INTERESTING IDEA', 'COULD BE IMPROVED'], ['LOVED IT', 'THANKS FOR SHARING']]

  • __init__ → the special method that runs automatically when you create a new object from the class.

  • self → refers to the new object being created.

  • comments → a parameter you pass in when creating the object.

  • self.comments = comments → stores the passed‑in value as an attribute of the object, so you can access it later.

class Blog: def __init__(self, comments): self.comments = comments # Create a Blog object with a list of comments blog1 = Blog(["Nice post!", "Great read"]) print(blog1.comments) # ['Nice post!', 'Great read']

  • When you call Blog(["Nice post!", "Great read"]), Python automatically calls __init__.

  • The comments list is passed in and stored as self.comments.

  • Now the object blog1 has an attribute comments you can use anywhere.


list(range(101))[::5]

range(101) → generates numbers from 0 up to 100 (inclusive).

So list(range(101)) = [0, 1, 2, 3, ..., 100].

[::5] → slice syntax with:

start = empty → defaults to beginning (0).

stop = empty → defaults to end (100).

step = 5 → take every 5th element.

Result → all multiples of 5 between 0 and 100.


list(range(101))[::-5]

Step 1: Build the list

list(range(101)) → [0, 1, 2, 3, ..., 100]

Step 2: Understand the slice

The slice is [::-5], which means:

start → empty → defaults to the end of the list.

stop → empty → defaults to the beginning.

step = -5 → move backwards, taking every 5th element.


Microsoft Copilot

沒有留言:

發佈留言