0% found this document useful (0 votes)
3 views3 pages

Python Data Structures: Arrays & Classes

The document provides an overview of data structures in Python, focusing on arrays and their functionalities such as accessing, modifying, adding, and removing elements. It also introduces Python classes and the __init__() function for initializing object attributes. Examples are included to illustrate each concept clearly.

Uploaded by

Ishwari Kumbhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Data Structures: Arrays & Classes

The document provides an overview of data structures in Python, focusing on arrays and their functionalities such as accessing, modifying, adding, and removing elements. It also introduces Python classes and the __init__() function for initializing object attributes. Examples are included to illustrate each concept clearly.

Uploaded by

Ishwari Kumbhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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])

You might also like