Let's explore the amazing world of Sets and Dictionaries!
Points
Badges
Exercises
Hey there! Think of a Set like a magical bag that only keeps unique items! No duplicates allowed! 🎒✨
set() function# Creating a set
my_fruits = {"apple", "banana", "cherry"}
print(my_fruits) # Output: {'apple', 'banana', 'cherry'}
# Sets remove duplicates automatically!
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # Output: {1, 2, 3} - duplicates are gone!
Fun Fact: Sets in Python work just like sets in math class! They're perfect for finding unique items in a collection.
# Direct creation
colors = {"red", "green", "blue"}
# From a list
my_list = [1, 2, 3, 3]
my_set = set(my_list) # {1, 2, 3}
# Must use set() for empty set
empty_set = set()
# NOT {} - that creates an empty dictionary!
# This creates a DICTIONARY, not a set!
wrong = {} # Empty dictionary
right = set() # Empty set
fruits = {"apple", "banana"}
fruits.add("cherry")
# {"apple", "banana", "cherry"}
fruits.remove("banana")
# {"apple", "cherry"}
# Or use discard() - no error if not found
fruits.discard("mango")
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2 # or set1.union(set2)
# {1, 2, 3, 4, 5}
set1 = {1, 2, 3}
set2 = {3, 4, 5}
common = set1 & set2 # or set1.intersection(set2)
# {3}
set1 = {1, 2, 3}
set2 = {3, 4, 5}
diff = set1 - set2 # or set1.difference(set2)
# {1, 2}
| Method | Description | Example |
|---|---|---|
add() |
Adds an element | s.add(4) |
remove() |
Removes element (error if not found) | s.remove(2) |
discard() |
Removes element (no error if not found) | s.discard(2) |
pop() |
Removes and returns random element | item = s.pop() |
clear() |
Removes all elements | s.clear() |
len() |
Returns number of elements | len(s) |
in |
Check if element exists | 3 in s |
Create a set called my_set containing the numbers 1, 2, 3, 4, 5. What do you write?
What will be the output of this code?
numbers = {1, 2, 2, 3, 3, 3, 4}
print(len(numbers))
Given set1 = {1, 2, 3} and set2 = {2, 3, 4}, what is the intersection?
Match the set operation with its result for sets A = {1, 2, 3} and B = {3, 4, 5}
Complete the code to find all unique characters in "hello world":
text = "hello world"
unique_chars =
print(unique_chars)
Test your knowledge! Complete this quiz to earn the Sets Master Badge! 🏆
Think of a Dictionary like a real dictionary! 📚 You look up a word (key) to find its meaning (value)!
# Creating a dictionary
student = {
"name": "Alice",
"age": 12,
"grade": "7th"
}
# Accessing values
print(student["name"]) # Output: Alice
print(student["age"]) # Output: 12
Did you know? Dictionaries in Python are incredibly fast at looking up values! They use something called "hash tables" behind the scenes.
# Direct creation
person = {"name": "Bob", "age": 25}
# Using dict()
person = dict(name="Bob", age=25)
# From two lists
keys = ["a", "b", "c"]
values = [1, 2, 3]
my_dict = dict(zip(keys, values))
# Empty dictionary
empty_dict = {}
# OR
empty_dict = dict()
student = {"name": "Alice", "age": 12}
# Method 1: Square brackets
print(student["name"]) # Alice
# Method 2: get() - safer!
print(student.get("name")) # Alice
print(student.get("grade", "N/A")) # N/A (default)
student = {"name": "Alice"}
# Add new key-value pair
student["age"] = 12
# Update existing value
student["name"] = "Bob"
print(student) # {'name': 'Bob', 'age': 12}
student = {"name": "Alice", "age": 12, "grade": "A"}
# Using del
del student["grade"]
# Using pop() - returns the value
age = student.pop("age") # age = 12
print(student) # {'name': 'Alice'}
my_dict = {}
| Method | Description | Example |
|---|---|---|
keys() |
Returns all keys | d.keys() |
values() |
Returns all values | d.values() |
items() |
Returns key-value pairs as tuples | d.items() |
get(key) |
Returns value (None if not found) | d.get("name") |
pop(key) |
Removes and returns value | d.pop("age") |
update() |
Updates with another dict | d.update({"x": 1}) |
clear() |
Removes all items | d.clear() |
student = {"name": "Alice", "age": 12, "grade": "A"}
# Loop through keys
for key in student.keys():
print(key)
# Loop through values
for value in student.values():
print(value)
# Loop through both
for key, value in student.items():
print(f"{key}: {value}")
Dictionaries can contain other dictionaries! It's like having folders inside folders! 📁📁
# A classroom with students
classroom = {
"student1": {
"name": "Alice",
"age": 12,
"grades": {"math": 95, "science": 88}
},
"student2": {
"name": "Bob",
"age": 13,
"grades": {"math": 82, "science": 90}
}
}
# Accessing nested values
print(classroom["student1"]["name"]) # Alice
print(classroom["student1"]["grades"]["math"]) # 95
Create a dictionary called pet with keys "name" and "species", with values "Buddy" and "dog":
What will this code print?
info = {"city": "Paris", "country": "France"}
print(info["city"])
How do you safely get a value that might not exist?
data = {"x": 10}
# Get value for key "y" with default 0 if not found
Arrange the code in correct order to create a dictionary and add a new key:
Given this nested dictionary, what code accesses Bob's science grade?
school = {
"class1": {
"Alice": {"math": 95, "science": 88},
"Bob": {"math": 82, "science": 90}
}
}
Complete this quiz to earn the Dictionary Master Badge! 🏆
Complete all Sets theory sections
Complete all Sets exercises
Pass the Sets quiz with 80%+
Complete all Dictionary theory sections
Complete all Dictionary exercises
Pass the Dictionary quiz with 80%+
Earn all other badges!
Score 100 points in under 10 minutes
Total Points
Badges Earned
Exercises Done
Time Learning