Python Data Structures
Python provides built-in data structures that are essential for storing and manipulating data. The three fundamental data structures in Python are lists, dictionaries, and tuples.
Lists
A list is a collection of items that are ordered and changeable. It allows duplicate items and is represented by square brackets ([]). You can access and modify elements in a list using indexing and slicing.
PYTHON
1import numpy as np
2
3# Create a list
4numbers = [1, 2, 3, 4, 5]
5
6# Access elements in the list
7print(numbers[0]) # Output: 1
8
9# Modify elements in the list
10numbers[2] = 9
11print(numbers) # Output: [1, 2, 9, 4, 5]
12
13# Iterate over the list
14for number in numbers:
15 print(number)
xxxxxxxxxx
38
coordinates[0] = 7
import numpy as np
# Create a list
numbers = [1, 2, 3, 4, 5]
# Access elements in the list
print(numbers[0]) # Output: 1
# Modify elements in the list
numbers[2] = 9
print(numbers) # Output: [1, 2, 9, 4, 5]
# Iterate over the list
for number in numbers:
print(number)
# Create a dictionary
person = {
"name": "John",
"age": 30,
"occupation": "Engineer"
}
# Access values in the dictionary
print(person["name"]) # Output: John
# Modify values in the dictionary
person["age"] = 35
print(person) # Output: {'name': 'John', 'age': 35, 'occupation': 'Engineer'}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment