-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures.py
More file actions
56 lines (47 loc) · 1.81 KB
/
Copy pathdata_structures.py
File metadata and controls
56 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Python Core Data Structures
# ------------------------------
# 1. LISTS (Ordered & Mutable)
# Think of this as a "To-Do" list or a shopping list.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Adds to the end
fruits[0] = "blueberry" # Changes "apple" to "blueberry"
movies = ["the matrix", "transformers", "james bond"]
movies.append("the mask of zorro")
movies[0] = "juice"
print(f"Fruits List: {fruits}")
print(f"First fruit: {fruits[0]}") # Accessing by index
print(f"movies List: {movies}")
print(f"First movie: {movies[0]}")
# 2. DICTIONARIES (Key-Value Pairs)
# Think of this as a real-world dictionary or a user profile.
user_profile = {
"name": "Royce",
"role": "Developer",
"level": 1
}
user_profile = {
"name": "Supdawg",
"role": "Rapper",
"level": 2
}
user_profile["level"] = 1 & 2 # Updating a value
user_profile["level"] = 1
user_profile["status"] = "Active" # Adding a new key-value pair
print(f"\nUser Profile: {user_profile}")
print(f"User Name: {user_profile['name']}")
# 3. TUPLES (Ordered & Immutable)
# Use this for things that SHOULD NOT change, like GPS coordinates.
coordinates = (40.7128, -74.0060)
coordinates = (47.7728, -77.0460)
# coordinates[0] = 50.0 # <--- This would cause an ERROR!
print(f"\nCoordinates Tuple: {coordinates}")
print(f"\nCoordinates Tuple: {coordinates}")
# 4. SETS (Unordered & Unique)
# Use this when you want to ensure no duplicates.
id_numbers = {101, 102, 103, 101, 102} # Note the duplicates
print(f"\nSet of IDs (Notice no duplicates): {id_numbers}")
# ------------------------------
# EXERCISE TODO:
# 1. Add your favorite movie to a new list called 'movies'.
# 2. Create a dictionary called 'car' with keys: 'brand', 'model', and 'year'.
# 3. Try to add a duplicate number to the 'id_numbers' set and see what happens when you print it.