• Javascript
  • Python
  • Go

Creating a List of Objects in Python

Python is a popular programming language known for its simple syntax and powerful capabilities. One of its most useful features is the abili...

Python is a popular programming language known for its simple syntax and powerful capabilities. One of its most useful features is the ability to create lists of objects, which allow for efficient and organized data storage. In this article, we will explore how to create a list of objects in Python and how to use them in your code.

To begin, let's first define what an object is in the context of Python. An object is a collection of data and functions that work together to perform a specific task. It can be thought of as a real-world object, such as a car or a person, that has its own set of characteristics and behaviors. In Python, objects are created using a class, which serves as a blueprint for creating multiple instances of that object.

Now, let's dive into creating a list of objects. To do this, we first need to create a class that will serve as the basis for our objects. Let's use the example of a student, with attributes such as name, age, and grade.

```

class Student:

def __init__(self, name, age, grade):

self.name = name

self.age = age

self.grade = grade

```

In the code above, we have defined a class called "Student" and initialized it with the attributes of name, age, and grade. The "self" parameter refers to the instance of the object and allows us to access its attributes and methods.

Next, we can create instances of the Student class and add them to a list. This can be done using the append() function, which adds an element to the end of a list.

```

# Create instances of Student class

student1 = Student("John", 18, 12)

student2 = Student("Emily", 17, 11)

student3 = Student("Mark", 16, 10)

# Create a list of students

student_list = []

student_list.append(student1)

student_list.append(student2)

student_list.append(student3)

```

Now, we have a list called "student_list" that contains three instances of the Student class. This list can be accessed and manipulated just like any other list in Python. For example, we can use a for loop to print out the names of all the students in the list.

```

# Print out names of students in the list

for student in student_list:

print(student.name)

```

Furthermore, we can also add methods to our class to perform specific tasks on our objects. Let's add a method that calculates the average grade of a student.

```

class Student:

def __init__(self, name, age, grade):

self.name = name

self.age = age

self.grade = grade

# Calculate average grade

def calculate_average_grade(self):

return sum(self.grade) / len(self.grade)

# Create instances of Student class

student1 = Student("John", 18, [90, 85, 95])

student2 = Student("Emily", 17, [80, 75, 90])

student3 = Student("Mark", 16, [95, 90, 85])

# Create a list of students

student_list = []

student_list.append(student1)

student_list.append(student2)

student_list.append(student3)

# Print out average grade of each student

for student in student_list:

print(student.name, "has

Related Articles

Listing Even Numbers

<strong>Listing Even Numbers</strong> In the world of mathematics, numbers are classified into two categories: even and odd. Eve...

Creating a 2D Matrix in Python

Creating a 2D Matrix in Python Matrices are an essential data structure in mathematics and computer science. They are used to represent and ...