Home » Web Development » Python » OOP Python Tutorial

About Bhagvan Kommadi

Avatar photo
Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

OOP Python Tutorial

1. Introduction

This is an article about oop in python. Python can be used for developing software and executing the code. Python is a programming language that has dynamic types. Python started off in a research firm based at Netherlands. The goal of this language was to reduce the gap between the shell commands syntax and C. The motivation of the language comes from different languages such as Algol68, Pascal and ABC. 

2. OOP Python Tutorial

2.1 Prerequisites

Python 3.6.8 is required on windows or any operating system. Pycharm is needed for python programming.

2.2 Download

Python 3.6.8 can be downloaded from the website. Pycharm is available at this link.

2.3 Setup

2.3.1 Python Setup

To install python, the download package or executable needs to be executed.

2.4 IDE

2.4.1 Pycharm Setup

Pycharm package or installable needs to be executed to install Pycharm.

2.5 Launching IDE

2.5.1 Pycharm

Launch the Pycharm and start creating a pure python project named HelloWorld. The screen shows the project creation.

Python vs Java - Pycharm Create project
Pycharm Create project

The project settings are set in the next screen as shown below.

Python vs Java - Pycharm Project Settings
Pycharm Project Settings

Pycharm welcome screen comes up shows the progress as indicated below.

Pycharm Project View

You can create a Hello.py and execute the python file by selecting the Run menu.

Pycharm Hello World

The output message “Hello World” is printed when the python file is Run.

Pycharm Python Execution

2.6 What Is Object-Oriented Programming in Python?

Python support multiple programming paradigms. Object oriented programming is one of the paradigms supported by the language. Object have attributes and behavior. Now let us look at an example, Truck has attributes such as name, model, color, and speed. It has behaviors like speeding and driving. Reusability is one of the aspects of Python. Don’t repeat Yourself (DRY) is a very popular concept based on reusability.

2.7 Defining a Class

In python, a class represents the objects’ blueprint. Truck is a class of objects which have name, model, color and speed. Truck is an object. Class can be defined as below:

Class definition

class Truck:

An empty class Truck is defined using the class keyword. We can have instances created by using the class. You can perform operations on the class after instantiating it.

2.8 Instantiate an Object 

An instance of a class is an object. Class Truck is instantiated as below:

Class instantiation

truck = Truck()

In the above example, truck object is created using Truck class constructor.

2.9 Inherit From Other Classes

You can have in python inheritance modeled using the base class, parent class, and derived class. Vehicle, Machine, and Truck class examples are shown. The truck can inherit from Vehicle and Machine as shown below:

Class Inheritance

class Vehicle:
    def __init__(self):
        self.velocity = 50
    def getVelocity():
        return self.velocity

class Machine:
    def __init__(self):
        self._distanceTravelled = 100
    def getDistance():
        return self.distanceTravelled
class Truck(Vehicle, Machine):
    def __init__(self, velocity,time):
        self.velocity = velocity
        self.time = time
    def getVelocity():
        return self.distanceTravelled/self.time
    def getDistance():
        return self.velocity * time

2.10 What are Instance Methods and how do we use them?

You can have instance methods which are inside the body of a python class. Methods represent the behaviors of an object.

Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.

Class Instance Methods

class Truck:
    def __init__(self, name, color):
        self.name = name
        self.color = color
    def speed(self, speed):
        return "{} speeding at {}".format(self.name, speed)
truck = Truck("Wagon","Red")

3. Download the Source Code

You can download the full source code of this example here: OOP Python Tutorial: Object Oriented Programming

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments