Python Objects and Classes

1) Define an Empty Class

Create an empty class Person. Then create an object of this class and print it.


2) Class With Constructor

Define a class Person with a constructor that takes name. Create an object and print the name.


3) Class With Two Attributes

Make a class Car with attributes make and year. Instantiate and print both attributes.


4) Method in Class

Define class Dog with method bark() that prints:

Woof!

Create an object and call the method.


5) Add a Method With Args

Define greet(self, name) in class Greeter that prints:

Hello, <name>!

Call it.


6) Class With Default Attribute

Create class Book with default attribute:

Print it from an object.


7) Modify Attribute

Create a class Student with attribute grade. Change grade on an object and print before/after.


8) Multiple Objects

Define class Point with x and y. Create three objects and print their coordinates.


9) Calculate Inside Method

Create class Rectangle with method area() that returns length * breadth.


10) Object List

Create 3 Person objects and store them in a list. Loop and print each name.


11) Method That Returns Value

Add get_age() to class Person that returns age. Create object and print return value.


12) Use __str__

Override __str__ in a class Car to return:

Print the object directly.


13) Increment Attribute in Method

Create class Counter with count = 0 and method increment() that increases it by 1.


14) Class With List Attribute

Define class Team with list members. Add a method to append a member and print the list.


15) Class With Multiple Methods

Class Calculator with methods: add(), sub(), mul(), div().


16) Constructor With Multiple Args

Make class Laptop with constructor taking brand, ram, price. Print all attributes.


17) Compare Two Objects

Create two Point objects and write a method to check if they are equal.


18) Class With Default Parameters

Define a class Person where age defaults to 18 if not provided.


19) Object Interaction

Create class Author and class Book. Each Book has an Author object inside. Print both.


20) Count Instances

Add a class variable in any class to count how many objects have been created.


πŸ“ Concepts Practiced

Topic
Brief

Class definition

class Name:

Constructor

__init__()

Instance attributes

self.x

Methods

Functions inside classes

Object creation

obj = ClassName()

Special methods

__str__

Class vs instance variables

Shared vs per object


πŸ“Œ Starter Template

Basic class with constructor and attribute:


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated