2. Add "orange" to the set.
3. Check if "apple" is in the set and print the result.
Right Answer:
# Step 1
fruits = {"apple", "banana", "cherry"}
# Step 2
fruits.add("orange")
# Step 3
print("apple" in fruits) # Output: True
Right Answer:
# Step 1
fruits = {"apple", "banana", "cherry"}
# Step 2
fruits.add("orange")
# Step 3
print("apple" in fruits) # Output: True
Right Answer:
# Step 1
car = {
"brand": "Toyota",
"model": "Corolla",
"color": "Blue",
"year": 2020
}
# Step 2
print(car.keys()) # Output: dict_keys(['brand', 'model', 'color', 'year'])
# Step 3
print(car.values()) # Output: dict_values(['Toyota', 'Corolla', 'Blue', 2020])
Right Answer:
# Step 1
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# Step 2
if 5 in A:
B.add(5)
else:
B.discard(5)
# Step 3
if 2 not in B:
A.discard(2)
# Step 4
print("A:", A)
print("B:", B)
# Step 5
if A & B:
print("Common elements:", A & B)
else:
print("No common elements.")
Right Answer:
# Step 1
colors = {"red", "green", "blue"}
# Step 2
colors_alternate = set(["red", "green", "blue"])
# Step 3
colors.discard("green")
# Step 4
colors.discard("yellow") # No error raised if "yellow" doesn't exist
# Step 5
colors.update(["Purple", "Pink"])
# Step 6
print(len(colors))
# Step 7
colors.clear()
# Step 8
del colors
Right Answer:
# Step 1
shapes = [('square', 'blue'), ('triangle', 'red'), ('circle', 'yellow')]
# Step 2
shapes_dict = dict(shapes)
# Step 3
shapes_dict['rectangle'] = 'green'
# Step 4
shapes_dict.pop('circle')
# Step 5
del shapes_dict['triangle']
# Step 6
shapes_list = list(shapes_dict.items())
print(shapes_list)
# Output: [('square', 'blue'), ('rectangle', 'green')]
Right Answer:
# Step 1
employees = {
"Akash": {"job_title": "Engineer", "salary": 50000},
"Dev": {"job_title": "Manager", "salary": 60000}
}
# Step 2 & 3
print(employees["Dev"]["salary"]) # Output: 50000
Right Answer:
# Step 1
inventory = {
"apples": 10,
"bananas": 12,
"cherries": 34
}
# Step 2
print("bananas" in inventory) # Output: True
# Step 3
print(5 in inventory.values()) # Output: False
# Step 4
fruits = {"apples", "bananas", "cherries"}
# Step 5 & 6
if len(inventory) > len(fruits):
print("Inventory has more items.")
else:
print("Fruits set has equal or more items.")
Right Answer:
# Step 1
prices = {
"apple": 0.5,
"banana": 0.25,
"orange": 0.75
}
# Step 2
prices["banana"] = 0.3
# Step 3
prices["cherry"] = 0.1
# Step 4
prices.update({"pear": 0.7})
# Step 5
print(prices)
# Output: {'apple': 0.5, 'banana': 0.3, 'orange': 0.75, 'cherry': 0.1, 'pear': 0.7}
Right Answer:
# Step 1
num_set = {5, 1, 3, 7, 8, 2, 3}
# Step 2
print(num_set) # Order is not preserved in sets
# Step 3
print("Max:", max(num_set)) # Max: 8
print("Min:", min(num_set)) # Min: 1
# Step 4
num_set.update([False, 3.14, "Rose"])
# Step 5
print(num_set) # Order is arbitrary in sets
# Step 6
num_set.add(1) # Adding a duplicate doesn't change the set
Right Answer:
# Step 1
person = {
"name": "John Doe",
"age": 30,
"job": "Engineer"
}
# Step 3
print(person["name"]) # Output: John Doe