# Example of creating dictionaries
empty_dict = {} # Empty dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print(person)
# Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Example of accessing and modifying key-value pairs
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing values
print(person["name"]) # Output: Alice
print(person["age"]) # Output: 25
# Modifying values
person["age"] = 26
print(person)
# Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}
# Adding new key-value pair
person["job"] = "Engineer"
print(person)
# Output: {'name': 'Alice', 'age': 26, 'city': 'New York', 'job': 'Engineer'}
len()
, max()
, min()
, and sorted()
.# Example of built-in functions used on dictionaries
my_dict = {"a": 10, "b": 20, "c": 30}
print(len(my_dict)) # Output: 3 (number of key-value pairs)
print(max(my_dict)) # Output: 'c' (maximum key based on alphabetical order)
print(min(my_dict)) # Output: 'a' (minimum key based on alphabetical order)
print(sorted(my_dict)) # Output: ['a', 'b', 'c'] (sorted keys)
get()
, keys()
, values()
, items()
, pop()
, update()
, and more.# Example of dictionary methods
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# get() method
print(person.get("name")) # Output: Alice
print(person.get("job", "Not available")) # Output: Not available
# keys(), values(), items() methods
print(person.keys()) # Output: dict_keys(['name', 'age', 'city'])
print(person.values()) # Output: dict_values(['Alice', 25, 'New York'])
print(person.items()) # Output: dict_items([('name', 'Alice'), ('age', 25), ('city', 'New York')])
# pop() method
age = person.pop("age")
print(age) # Output: 25
print(person) # Output: {'name': 'Alice', 'city': 'New York'}
# update() method
person.update({"age": 26, "job": "Engineer"})
print(person) # Output: {'name': 'Alice', 'city': 'New York', 'age': 26, 'job': 'Engineer'}
del
Statementdel
statement is used to delete a key-value pair from a dictionary or to delete the entire dictionary.# Example of using the del statement
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Delete a specific key-value pair
del person["city"]
print(person) # Output: {'name': 'Alice', 'age': 25}
# Delete the entire dictionary
del person
()
and can store different types of data.# Example of creating tuples
empty_tuple = ()
single_element_tuple = (42,)
mixed_tuple = (1, "apple", 3.14)
print(mixed_tuple) # Output: (1, 'apple', 3.14)
+
), repetition (*
), and membership checking (in
).# Example of basic tuple operations
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
# Concatenation
new_tuple = tuple1 + tuple2
print(new_tuple) # Output: (1, 2, 3, 4, 5)
# Repetition
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
# Membership
print(2 in tuple1) # Output: True
print(6 in tuple1) # Output: False
# Example of indexing and slicing
my_tuple = (10, 20, 30, 40, 50)
# Indexing
print(my_tuple[0]) # Output: 10
print(my_tuple[-1]) # Output: 50
# Slicing
print(my_tuple[1:4]) # Output: (20, 30, 40)
print(my_tuple[:3]) # Output: (10, 20, 30)
len()
, max()
, min()
, and sum()
work with tuples.# Example of built-in functions
numbers = (10, 20, 30, 40)
print(len(numbers)) # Output: 4
print(max(numbers)) # Output: 40
print(min(numbers)) # Output: 10
print(sum(numbers)) # Output: 100
# Example of converting between tuples and lists
my_tuple = (1, 2, 3)
my_list = list(my_tuple) # Convert tuple to list
my_list.append(4)
new_tuple = tuple(my_list) # Convert list back to tuple
print(new_tuple) # Output: (1, 2, 3, 4)
# Example of using a tuple as a key in a dictionary
location_coordinates = {
(40.7128, -74.0060): "New York",
(34.0522, -118.2437): "Los Angeles"
}
print(location_coordinates[(40.7128, -74.0060)])
# Output: New York
count()
and index()
.# Example of tuple methods
my_tuple = (10, 20, 20, 30)
# count() method
print(my_tuple.count(20)) # Output: 2
# index() method
print(my_tuple.index(30)) # Output: 3
zip()
Functionzip()
function can be used to combine multiple iterables (e.g., lists or tuples) into tuples.# Example of using zip() function
names = ("Alice", "Bob", "Charlie")
scores = (85, 90, 95)
zipped = zip(names, scores)
print(list(zipped)) # Output: [('Alice', 85), ('Bob', 90), ('Charlie', 95)]
{}
or the set()
constructor.# Example of creating sets
empty_set = set() # Empty set
numbers_set = {1, 2, 3, 4, 5}
mixed_set = {1, "apple", 3.14}
print(mixed_set) # Output: {1, 3.14, 'apple'}
add()
, remove()
, union()
, intersection()
, and difference()
.# Example of set methods
fruits = {"apple", "banana", "cherry"}
# add() method
fruits.add("orange")
print(fruits) # Output: {'apple', 'orange', 'cherry', 'banana'}
# remove() method
fruits.remove("banana")
print(fruits) # Output: {'apple', 'orange', 'cherry'}
# union() method
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
# intersection() method
print(set1.intersection(set2)) # Output: {3}
# difference() method
print(set1.difference(set2)) # Output: {1, 2}
for
loop, but remember that sets are unordered, so the elements will not follow any particular sequence.# Example of traversing a set
my_set = {"apple", "banana", "cherry"}
for fruit in my_set:
print(fruit)
# Output (order may vary):
# apple
# banana
# cherry
Made By SOU Student for SOU Students