|
Python · Sets
Sets / Множества
Store unique values, check membership and change a collection one element at a time. Learning objectives
1. What is a set?A set is an unordered collection of distinct elements. Repeated equal values are stored only once.
Create an empty or non-empty set
Common mistake. To create an empty set, use
set(), not {}.Create a set from textWhen a string is passed to
The elements of a set may have different types, such as numbers and strings.
Use numeric sets for the calculations below. 2. Read, check and countCount elements and calculate statistics
Empty sets.
len(set()) and sum(set()) return 0. Calling min(set()) or max(set()) without a default value raises ValueError.
Check whether an element is present
Loop through a set
Keep the size unchanged during iteration. Do not add or delete elements in the same set while looping through it.
Display a setUse
3. Add and remove elementsAdd one element: add()
Choose the correct deletion method
discard(): absence is acceptable
remove(): check if needed
pop(): keep the returned element
Do not rely on a particular value being returned. clear(): start again
Do not replace a set with a method result.
add(), discard(), remove() and clear() return None after a successful call. Write members.add(110), not members = members.add(110).4. Set laboratoryApply an action and compare the set before and after it. This lab uses integers only. numbers — displayed in ascending order for readabilitylen(numbers) = 3
Try adding 5 twice, then compare discard(99) with remove(99).
The lab demonstrates these actions. It does not execute arbitrary Python code. Its choice for pop() is just one possible choice. 5. ExercisesThere are 8 exercises: the embedded warm-up and 7 checked tasks about sets. Check an answer, use a hint if needed, and read the explanation. Completed: 0 / 7 tasks
Answers and progress are saved in this browser on this device. They are not sent to a teacher. Clear all answers and progress for these 7 checked tasks? Interactive checks require JavaScript. If this notice remains visible, open the page in a browser with JavaScript enabled. Ex. 2 · Create a setChoose the correct construction or description in each case. Use set() for an empty set. Count distinct values, and remember that element order is not guaranteed.
Show explanation
Ex. 3 · Unique number statisticsPredict the four results for this set.
First remove the repeated values. All four functions work with the resulting set.
Show explanationThe set contains 2, 4 and 7. The results are 3, 13, 2, 7, in that order. Ex. 4 · Access checkPredict each Boolean result. The final line uses a string.
The number 101 and the string "101" are different values.
Show explanationTrue, True, False. 104 is present, 109 is absent, and the string "101" is not an element of the set. Ex. 5 · Add, clear and countWrite the size after each change to the set.
Adding an element that is already present does not increase the size. clear() removes every element.
Show explanation1, 1, 0, 1. The second 7 adds nothing new. clear() empties the set, and add(3) puts one element into it. Ex. 6 · Choose a deletion methodChoose the method that matches each requirement.
discard ignores a missing element. remove reports a missing element. clear empties the whole set.
Show explanationdiscard(), remove(), clear(); 2. The set ends with elements 2 and 6. Ex. 7 · Understand pop()The set is non-empty. Select everything that must be true after the code runs.
pop removes and returns one element. It does not promise the smallest or largest one.
Show explanationSelect the first three statements. Calling pop() on an empty set raises KeyError. Ex. 8 · Loop through unique valuesPredict the number and sum of the distinct even values.
Each element of the set is visited once. Repeated values in the literal do not create extra elements.
Show explanationThe even elements are 2, 6 and 8. The program prints 3 and then 16. | ||||||||||||||||
|
| ||||||||||||||||
| Просмотров: 12212 | | | ||||||||||||||||
| Всего комментариев: 0 | |