Python Classes/Objects

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Python tutorial, let us discuss classes and objects in Python with appropriate examples.

Introduction of Python

Python is a widely-used general-purpose, high-level programming language and allows programming in OOPs paradigms.


The core benefit of Python is a large library that can be applied in the several fields as follows,

  • Machine Learning
  • GUI Applications 
  • Web frameworks like Django 
  • Image processing 
  • Web scraping Test frameworks
  • Multimedia
  • Scientific computing
  • Text processing and many more

Creating a Class

To create a class, use the keyword “class”.

Example to create a class

class MyClass:
x = 5

The class named MyClass is created, with a property named x.

Creating an Object

We can use the class titled myClass to create an object.

Example to create an object

p1 = MyClass()

An object named p1 is created. It will print the value of x.

The init() Function

All classes have a init() function call that is executed when the class initiates. The init() function is used to assign values to object properties or other operations, that is necessary to do when the object created.

Example to initiate a function

class Person:
  def __init__(self, name, age): = name
   self.age = age
p1 = Person("John", 36)



Object Methods

Objects can also contain methods.

Example to print name greeting using object

class Person:
def __init__(self, name, age): = name
self.age = age
def myfunc(self):
print("Hello my name is " +
p1 = Person("John"36)


Hello my name is John

Leave a Comment

Your email address will not be published. Required fields are marked *