Q:

How to create a class in Python?

0

How to create a class in Python?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

In Python, we can create a class by using the keyword “class.” An object gets created from the constructor. This object represents the instance of the class. In Python, we generate classes and instances in the following way:

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
p1 = Person("Amlendra",28)
print(p1.name)
print(p1.age)

In the above example, we have created a class named Person and using the __init__() we are assigning name and age to the newly created object.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Interview Questions and Answers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the syntax for creating an instance of a c... >>
<< What Is Class In Python?...