Data Structures Python
Arrays
Arrays are used to store multiple values in one single variable
cars = ["Ford", "Volvo", "BMW"]
print(cars)
Output : ["Ford", "Volvo", "BMW"]
Access the Elements of an Array
cars = ["Ford", "Volvo", "BMW"]
x = cars[2]
print(x)
Output : BMW
Modify Value of array
cars = ["Ford", "Volvo", "BMW"]
cars[0] = “Toyota”
print(cars)
Output : ["Toyota", "Volvo", "BMW"]
Length on an array
cars = ["Ford", "Volvo", "BMW"]
x = len(cars)
print(x)
Output : 3
Looping Array Elements
You can use the for in loop to loop through all the elements of an array.
cars = ["Ford", "Volvo", "BMW"]
x = len(cars)
print(x)
Output : Ford
Volvo
BMW
Adding Array Elements
cars = ["Ford", "Volvo", "BMW"]
[Link]("Honda")
print(cars)
Output: ['Ford', 'Volvo', 'BMW', 'Honda']
Removing Array Elements
You can use the pop() method to remove an element from the array.
cars = ["Ford", "Volvo", "BMW"]
[Link](1)
print(cars)
Output: ['Ford', 'BMW']
You can use the pop() method to remove an element from the array.
cars = ["Ford", "Volvo", "BMW"]
[Link](“Volvo”)
print(cars)
Output: ['Ford', 'BMW']
Python Classes and Objects
The __init__() Function
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
p1 = Person("John", 36)
p2 = Person("Mary", 12)
print([Link])
print([Link])
print([Link])
print([Link])