What is python class?
Define Python
Class. What is attributes and methods
in class. 3+3+9
A class is considered as a blueprint of objects. We can think of the class as a
sketch (prototype) of a house. It contains all the details about the floors,
doors, windows, etc. Based on these descriptions we build the house.
House is the object.
Since many houses can be made from the same description, we can create
many objects from a class.
We use the class keyword to create a class in Python. For example,
class ClassName:
Here, we have created a class named ClassName.
Let's see an example,
class Bike:
name = ""
gear = 0
Here,
Bike - the name of the class
name/gear - variables inside the class with default values "" and 0
respectively.
A class by itself is of no use unless there is some functionality
associated with it. Functionalities are defined by setting attributes,
which act as containers for data and functions related to those
attributes. Those functions are called methods.
Attributes:
You can define the following class with the name Snake. This class will have
an attribute name.
>>> class Snake:
... name = "python"
You can assign the class to a variable. This is called object instantiation. You
will then be able to access the attributes that are present inside the class
using the dot . operator. For example, in the Snake example, you can access
the attribute name of the class Snake.
>>> snake = Snake()
>>> print([Link])
python
Methods
Once there are attributes that “belong” to the class, you can define
functions that will access the class attribute. These functions are called
methods. When you define methods, you will need to always provide the
first argument to the method with a self keyword.
For example, you can define a class Snake, which has one attribute name
and one method change_name. The method change name will take in an
argument new_name along with the keyword self.
>>> class Snake:
... name = "python"
... def change_name(self, new_name): # note that the first argument is
self
... [Link] = new_name # access the class attribute with the self
keyword
Now, you can instantiate this class Snake with a variable snake and then
change the name with the method change_name.
>>> # instantiate the class
>>> snake = Snake()
>>> # print the current object name
>>> print([Link])
python
>>> # change the name using the change_name method
>>> snake.change_name("anaconda")
>>> print([Link])
Anaconda
2. Write a Python class named Circle constructed from a radius and two
methods that will compute the area and the perimeter of a circle. Write a
Python program to find the area and perimeter of a rectangle using class. 7+8
ANS: -
class Circle():
def __init__(self, r):
[Link] = r
def area(self):
return [Link]**2*3.14
def perimeter(self):
return 2*[Link]*3.14
NewCircle = Circle(8)
print([Link]())
print([Link]())
#=> 200.96
50.24
class Rectangle:
def __init__(self, length, breadth):
[Link] = length
[Link] = breadth
def display(self):
print ("Length of Rectangle is: ", [Link])
print ("Breadth of Rectangle is: ", [Link])
def area(self):
return ([Link]*[Link])
def perimeter(self):
return (2*[Link] + 2*[Link])
l = int (input("Enter the length of the Rectangle: "))
b = int (input("Enter the breadth of rectangle: "))
r1 = Rectangle(l , b)
print ("Rectangle object details are:")
[Link]()
print("")
print ("Area of Rectangle is: ", [Link]())
print("")
print ("The Perimeter of the Rectangle is: ", [Link]())
#=> Enter the length of the Rectangle: 10
Enter the breadth of rectangle: 5
Rectangle object details are:
Length of Rectangle is: 10
The breadth of Rectangle is: 5
Area of Rectangle is: 50
The perimeter of the Rectangle is: 30