Tutorial 1: OOPs in Python — Creating classes and objects

Gaurav Patil
3 min readDec 23, 2021
Photo by Hitesh Choudhary on Unsplash

When I started learning coding , I found that concepts are better understood when used in variety of examples. That’s why I am giving many examples for each topic enlisted below. Click on the topic to navigate directly .

OOPs introduction

Terminologies

Syntax

Example 1

Example 2 : Using attribute in a method

Example 3 : Defining the attribute in a method

What is OOP ?

Object Oriented Programming Language is the one in which we can create Objects . These objects can have features or characteristics and they can perform some actions using functions or methods.

Lot of jargon stuff !!! Just stay with me for a while and it will certainly make sense .

Why OOP ?

With the help of OOPs, we can write code reusable code and modifying the code is very easy. Of course, we can do that using functions as well but OOPs is much more powerful than using functions.

Terminologies :

  1. Classes : blueprint of something
  2. Objects : actual example or instance of that something
  3. Attributes : features, characteristics or properties of that something
  4. Methods : functions belonging to that class or actions performed by that something

Simple examples :

1 — Class : Animals

Object : Dog

Attributes : name, breed, color

Methods : barking, eating, running

2 — Class : Person

Object : Student

Attributes : name, age, qualification

Methods : learning, playing

Syntax :

init constructor or method :

initializes or assigns the attribute values to the attributes when object is created .

self keyword :

  • to connect the attribute with it’s value
  • to access the object-specific data (or attribute) inside class
  • ‘self’ argument is always present for every method(function) in the class

Now that you are very well acquainted with the terminologies used in OOPs and syntax too. Let’s learn how to create classes and objects using multitudes of examples as we always do in every tutorial.

Example 1 :

Note : In OOPs while creating Class, ‘self’ argument is always present for every method(function).

Example 2 : Using attribute in a method

Example 3 : Defining the attribute in a method

here TypeError says : __init__method takes 1 positional argument but 2 were given. So 1 positional argument means ‘self’ argument, as we already know ‘self’ argument is always present for every method(function) in the class.

Now you have good understanding of how class and object is created , how to access the attributes, how to call the methods defined inside a class.

Also , you must have noticed that we are learning something new in every example. So we will follow this suit for next tutorial as well to learn something new in every example.

So take a deep breathe and move to the next tutorial .

Happy learning !!

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.