All the IPython Notebooks in this lecture series are available at


Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial.

A class is declared as follows

class class_name:

In [1]:
class FirstClass:

pass in python means do nothing.

Above, a class object named "FirstClass" is declared now consider a "egclass" which has all the characteristics of "FirstClass". So all you have to do is, equate the "egclass" to "FirstClass". In python jargon this is called as creating an instance. "egclass" is the instance of "FirstClass"

In [2]:
egclass = FirstClass()
In [3]:
In [4]:

Now let us add some "functionality" to the class. So that our "FirstClass" is defined in a better way. A function inside a class is called as a "Method" of that class

Most of the classes will have a function named "__init__". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute.

These helps simplify the process of initializing a instance. For example,

Without the use of magic method or __init__ which is otherwise called as constructors. One had to define a init( ) method and call the init( ) function.

In [ ]:
eg0 = FirstClass()

But when the constructor is defined the __init__ is called thus intializing the instance created.

We will make our "FirstClass" to accept two variables name and symbol.

I will be explaining about the "self" in a while.

In [6]:
class FirstClass:
    def __init__(self,name,symbol): = name
        self.symbol = symbol

Now that we have defined a function and added the __init__ method. We can create a instance of FirstClass which now accepts two arguments.

In [7]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [8]:
print, eg1.symbol
print, eg2.symbol
one 1
two 2

dir( ) function comes very handy in looking into what the class contains and what all method it offers

In [9]:
['__doc__', '__init__', '__module__']

dir( ) of an instance also shows it's defined attributes.

In [10]:
['__doc__', '__init__', '__module__', 'name', 'symbol']

Changing the FirstClass function a bit,

In [11]:
class FirstClass:
    def __init__(self,name,symbol):
        self.n = name
        self.s = symbol

Changing and self.symbol to self.n and self.s respectively will yield,

In [12]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [13]:
print, eg1.symbol
print, eg2.symbol
AttributeError                            Traceback (most recent call last)
<ipython-input-13-3717d682d1cf> in <module>()
----> 1 print, eg1.symbol
      2 print, eg2.symbol

AttributeError: FirstClass instance has no attribute 'name'

AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance.

In [14]:
['__doc__', '__init__', '__module__', 'n', 's']
In [15]:
print eg1.n, eg1.s
print eg2.n, eg2.s
one 1
two 2

So now we have solved the error. Now let us compare the two examples that we saw.

When I declared and self.symbol, there was no attribute error for and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s

From the above we can conclude that self is nothing but the instance itself.

Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self.

In [16]:
class FirstClass:
    def __init__(asdf1234,name,symbol):
        asdf1234.n = name
        asdf1234.s = symbol
In [17]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [18]:
print eg1.n, eg1.s
print eg2.n, eg2.s
one 1
two 2

Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass.

In [19]:
eg1.cube = 1
eg2.cube = 8
In [20]:
['__doc__', '__init__', '__module__', 'cube', 'n', 's']

Just like global and local variables as we saw earlier, even classes have it's own types of variables.

Class Attribute : attributes defined outside the method and is applicable to all the instances.

Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance.

In [21]:
class FirstClass:
    test = 'test'
    def __init__(self,name,symbol): = name
        self.symbol = symbol

Here test is a class attribute and name is a instance attribute.

In [22]:
eg3 = FirstClass('Three',3)
In [23]:
print eg3.test,
test Three

Let us add some more methods to FirstClass.

In [24]:
class FirstClass:
    def __init__(self,name,symbol): = name
        self.symbol = symbol
    def square(self):
        return self.symbol * self.symbol
    def cube(self):
        return self.symbol * self.symbol * self.symbol
    def multiply(self, x):
        return self.symbol * x
In [25]:
eg4 = FirstClass('Five',5)
In [26]:
print eg4.square()
print eg4.cube()
In [27]:

The above can also be written as,

In [28]:


There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can "inherit" the previous class and add it's own methods to it. This is called as inheritance.

Consider class SoftwareEngineer which has a method salary.

In [29]:
class SoftwareEngineer:
    def __init__(self,name,age): = name
        self.age = age
    def salary(self, value): = value
In [30]:
a = SoftwareEngineer('Kartik',26)
In [31]:
Kartik earns 40000
In [32]:
['__doc__', '__init__', '__module__', 'salary']

Now consider another class Artist which tells us about the amount of money an artist earns and his artform.

In [33]:
class Artist:
    def __init__(self,name,age): = name
        self.age = age
    def money(self,value): = value
    def artform(self, job):
        self.job = job
        print,"is a", self.job
In [34]:
b = Artist('Nitin',20)
In [35]:
Nitin earns 50000
Nitin is a Musician
In [36]:
['__doc__', '__init__', '__module__', 'artform', 'money']

money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes,

In [37]:
class Artist(SoftwareEngineer):
    def artform(self, job):
        self.job = job
        print,"is a", self.job
In [38]:
c = Artist('Nishanth',21)
In [39]:
['__doc__', '__init__', '__module__', 'artform', 'salary']
In [40]:
Nishanth earns 60000
Nishanth is a Dancer

Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class.

In [41]:
class Artist(SoftwareEngineer):
    def artform(self, job):
        self.job = job
        print,"is a", self.job
    def salary(self, value): = value
        print "I am overriding the SoftwareEngineer class's salary method"
In [42]:
c = Artist('Nishanth',21)
In [43]:
Nishanth earns 60000
I am overriding the SoftwareEngineer class's salary method
Nishanth is a Dancer

If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result.

In [44]:
class emptylist:
    def __init__(self): = []
    def one(self,x):
    def two(self, x ):**2)
    def three(self, x):**3)
In [45]:
xc = emptylist()
In [46]:

Since is a list direct list operations can also be performed.

In [47]:
[1, 8]
In [48]:
[1, 8, 9]

If the number of input arguments varies from instance to instance asterisk can be used as shown.

In [49]:
class NotSure:
    def __init__(self, *args): = ''.join(list(args)) 
In [50]:
yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')
In [51]:

Where to go from here?

Practice alone can help you get the hang of python. Give your self problem statements and solve them. You can also sign up to any competitive coding platform for problem statements. The more you code the more you discover and the more you start appreciating the language.

Now that you have been introduced to python, You can try out the different python libraries in the field of your interest. I highly recommend you to check out this curated list of Python frameworks, libraries and software

The official python documentation :

You can also check out Python practice programs written by my friend, Kartik Kannapur. Github Repo :

Enjoy solving problem statements because life is short, you need python!


Rajath Kumar M.P ( rajathkumar dot exe at gmail dot com)