Classes and Objects
A class in Python is a user-defined template for creating objects. It is a collection data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.
Syntax:
class CLASS_NAME:
variables
def FUNCTION_NAME:
return()
OBJECT_NAME = CLASS_NAME()
#Get class variables
print(OBJECT_NAME.variables)
#use functions of a class
OBJECT_NAME.FUNCTION_NAME()
Example
class test:
a = 10
b = "hello"
c = [10,20,30]
def add(self,x,y):
return(x+y)
t1 = test()
print(t1.a,t1.b,t1.c)
z = t1.add(10,30)
print(z)
Special init() function in class
In Python, class has init() function. It automatically executes when an object is created. This function does not have a return() functionality
class CLASS_NAME:
def __init__(self):
print("executes automatically")
OBJECT_NAME = CLASS_NAME()
init() example
class Dog:
species = "Dogs" # Class attribute
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age # Instance attribute
# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)
dog2 = Dog("Tom", 5)
print(dog1.name,dog1.age)
print(dog1.species)
print(dog2.name,dog2.age)
print(dog2.species)
Practise Programs
- Create a class that has 3 variables of int, string, float and create a object and print all the variables in the class object
- Create a class that has functions to has add two numbers,subtract two numbers,multiply three number and a function for simple interest. then create a class object and call all the functions with right arguments and print them
Class usage in real world senarios
The real power of class comes from OOPs. OOPs empowers Python with modularity, reusability and easy to maintain code. Sample code of Both the below files have to be in the same folder for the code to work
- dataGather.py has the class assigned in it
- telangana_data.py python file: has the program that imports the class from dataGather.py
- after you copy the files execute telangana_data.py
dataGather.py
class data:
# Class attribute
nationality = "Indian"
def __init__(self, name, state, gender,date_of_birth):
# Instance attribute
self.name = name
self.state = state
self.gender = gender
self.date_of_birth = date_of_birth #list example [16,01,2000] 16-01-2000
def age(self,year):
a = year - self.date_of_birth[2]
return(a)
telangana_data.py
import dataGather
# present year
current_year = 2025
h1 = dataGather.data("Ram","Telangana","Male",[10,5,1999])
h2 = dataGather.data("Sita","Telangana","Female",[10,6,2000])
print(h1.name,"age is",h1.age(current_year))
print(h2.name,"age is",h2.age(current_year))
Advance practise programs
Classes and programs have to be in different files
- Create a class that has 3 variables int.string.float and create a object and print all the variables in the class object
- Create a class that functions to has add two numbers,subtract two numbers,multiply three number and a function for simple interest. then create a class object and call all the functions with right arguments and print them