Array

An array is a special variable, which can hold more than one value at a time.If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:


Initialization of Array

arr = [1, 2, 3, 4, 5]

# This list will store character type elements (strings in Python)

arr = ['a', 'b', 'c', 'd', 'e']

# This list will store float type elements

arr = [1.4, 2.0, 24.0, 5.0, 0.0]  # All float values


cars = ["Ford", "Volvo", "BMW"]

car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"

Accessing the elements of an Array

cars = ["Ford", "Volvo", "BMW"]
x = cars[0]
print(x)

Ford

Update the elements of an Array

cars = ["Ford", "Volvo", "BMW"]
print("Values before the updating the Array elements")
print(cars)
cars[0] = "Toyota"
print("Values after the updating the Array elements ")
print(cars)

Values before the updating the Array elements
['Ford', 'Volvo', 'BMW']
Values after the updating the Array elements
['Toyota', 'Volvo', 'BMW']


No comments:

Post a Comment