
Python objects are an important part of the Python programming language as being an object oriented programming language, Python relies on objects and classes to handle its data items. Python complete itself around the concept of OOPs which help manage codes in an efficient manner. Python objects and classes are the main concepts of Python OOPs.
In this tutorial, we will learn more about Python objects in detail and learn how we are going to use it in our projects. We will also take an example to understand the Python OOPs and its object.
| class Car: def __init__(self, brand, model): self.brand = brand self.model = model def drive(self): print(f"Driving a {self.brand} {self.model}.") my_car = Car("Toyota", "Corolla") my_car.drive() |
![]() |
| class car: toyota = 5 maruti = 5 honda = 4 volkswagen = 3 #Now we will create a simple object and access the attributes inside the class. obj 1 = car() print (obj 1. maruti) print (obj 1. honda) |
![]() |
| class PWSkills:: def greet(self, name): print(f"Hello {name}, welcome to PW Skills!") # Creating an object of the class student = PWSkills() student.greet("Ankit") |
![]() |
| class PWSkills: def __init__(self, student_name): self.name = student_name def update_name(self, new_name): self.name = new_name def display_name(self): print(f"Student's name is {self.name}") # Create an object student = PWSkills("Ankit") # Display the initial name student.display_name() # Change the name using method student.update_name("Riya") # Display the updated name student.display_name() |
![]() |
| class Student: def __init__(self, name, course): self.name = name self.course = course def introduce(self): print(f"My name is {self.name} and I am studying {self.course}.") # Create multiple objects student1 = Student("Ankit", "Data Science") student2 = Student("Riya", "Web Development") student3 = Student("Rahul", "AI and ML") # Calling methods for each object student1.introduce() student2.introduce() student3.introduce() |
![]() |
| class PWSkills: def __init__(self, student_name): self.name = student_name self.course = "Data Science" # Create an object student = PWSkills("Ankit") # Display properties print(student.name) # Output: Ankit print(student.course) # Output: Data Science # Deleting a property del student.course # Trying to access deleted property will give an error # print(student.course) # Uncommenting this will cause AttributeError # Deleting the entire object del student # Trying to access deleted object will also cause an error # print(student) # Uncommenting this will cause NameError |
![]() |
![]() |