Python 3 beginner course · Chapter 4 of 10
This chapter explains Data Structures: Strings, Lists, Dictionaries, Tuples, Sets and Bytes in plain language. Read the concept, run the example, check the result and change one part to learn by doing. Optional packages are identified in their sections.
Strings and slicing
A str is text. Indexing starts at 0, and slices use a start and an excluded end. Strings cannot be changed in place; create a new string when you need different text.
Python example
word = "Python"
print(word[0], word[1:4], word.lower())Expected result: P yth python
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Lists
A list keeps items in order and can be changed. append adds one item; indexing and slicing read items. Use a list when order matters or when the collection will grow.
Python example
scores = [80, 90]
scores.append(75)
print(scores[0], len(scores))Expected result: 80 3
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Dictionaries
A dictionary maps a key to a value. Use it for named fields or fast lookup by a unique key. Access a required key with brackets and an optional key with get().
Python example
student = {"name": "Asha", "score": 80}
print(student["name"], student.get("grade", "Unknown"))Expected result: Asha Unknown
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Tuples
A tuple is an ordered collection that cannot be changed in place. It is useful for a fixed group of values, such as a coordinate or a function returning two results. Unpacking gives each position a name.
Python example
point = (4, 7)
x, y = point
print(x + y)Expected result: 11
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Sets
A set stores unique values without promising a useful order. Use it to remove duplicates or compare groups with union and intersection. Do not rely on the printed order of set elements.
Python example
a = {1, 2, 3}
b = {3, 4}
print(sorted(a & b), sorted(a | b))Expected result: [3] [1, 2, 3, 4]
Practice: Predict the result before running the example. Change one value or line and explain the new result.
bytes and bytearray
bytes is an immutable sequence of byte values; bytearray is mutable. Encoding turns text into bytes and decoding turns bytes back into text. This matters for files and network data.
Python example
data = "Hi".encode("utf-8")
print(data, data.decode("utf-8"))Expected result: b'Hi' Hi
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Advanced structures and comprehensions
Choose the structure by its job: list for ordered changeable items, tuple for a fixed record, set for uniqueness and dict for key lookup. A comprehension creates a new collection from an iterable.
Python example
squares = [n * n for n in range(1, 5)]
print(squares)Expected result: [1, 4, 9, 16]
Practice: Predict the result before running the example. Change one value or line and explain the new result.
Quick review
Explain the main idea to a friend in one sentence. Then recreate one example without looking at the code. If it fails, read the error message and compare the indentation and names.
Continue through the complete Python learning path. These are original explanations and diagrams; the topic checklist was compared with the w3resource Python index and the official Python tutorial.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.