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)xxxxxxxxxx38
coordinates[0] = 7import numpy as np# Create a listnumbers = [1, 2, 3, 4, 5]# Access elements in the listprint(numbers[0]) # Output: 1# Modify elements in the listnumbers[2] = 9print(numbers) # Output: [1, 2, 9, 4, 5]# Iterate over the listfor number in numbers: print(number)# Create a dictionaryperson = { "name": "John", "age": 30, "occupation": "Engineer"}# Access values in the dictionaryprint(person["name"]) # Output: John# Modify values in the dictionaryperson["age"] = 35print(person) # Output: {'name': 'John', 'age': 35, 'occupation': 'Engineer'}OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment


