Module II - Day 5

Python Made Easy: Science and Math Edition

Sep-Dec 2025 batch, Vikrant Patil

Date: 17 Jan 2026

Click here for All Notes

Live note are here https://vikrant.dev/python-made-easy-science-math/students-module2-day5.html

Please login to https://traininghub.vikrant.dev and create a notebook with name module2-day5.ipynb

© Vikrant Patil

Object Oriented Programming

x = 78
f = 45.8
text = "hello"
nums = [1, 2, 3, 4, 5]
person = {"name":"Vikrant"}
            Instance of class
            +---------------+
            |               |<---------methods to manipulate data
            |    data       |
            |               |<---------methods
            +---------------+
                    |
                    |
                    +------<------- methods to access data
x # it has an integer and has data with value as 78
78
text # it is an object of type str, and data in it is "hello"
'hello'
text.isdigit()
False
text.upper()
'HELLO'

Bank account

%%file bank1.py

balance = 0
#identification - on hold

def desposit(amount):
    global balance # when we declare a variable as global it does not create local variable
    balance = balance + amount

def withdraw(amount):
    global balance # when we declare a variable as global it does not create local variable
    balance -= amount
    

def get_balance():
    return balance
Writing bank1.py
import bank1
bank1.get_balance()
0
bank1.desposit(1000)
bank1.get_balance()
1000
bank1.withdraw(100)
bank1.get_balance()
900
%%file bank2.py


def create_account(amount, name):
    return {'balance': amount,
            'name': name}

def deposit(account, amount):
    account['balance'] += amount

def withdraw(account, amount):
    account['balance'] -= amount

def get_balance(account):
    return account['balance']
Overwriting bank2.py
import bank2
acc_vikrant = bank2.create_account(amount=2500, name="Vikrant")
bank2.deposit(acc_vikrant, 2000)
bank2.get_balance(acc_vikrant)
4500
acc_samiha = bank2.create_account(name="Samiha", amount=5000)
bank2.get_balance(acc_samiha)
5000
bank2.withdraw(acc_samiha, 250)
bank2.get_balance(acc_samiha)
4750
bank2.get_balance(acc_vikrant)
4500
nums
[1, 2, 3, 4, 5]
nums.append(6)
type(x)
int
type(nums)
list
type(person)
dict
type(text)
str
type(acc_vikrant)
dict
class BankAccount:

    # dunder init
    def __init__(self, name, amount): # this is a special function which is inside the class and
                                      # first argument is always self
        self.name = name
        self.balance = amount

    def deposit(self, amount):   
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount

    def get_balance(self):
        return self.balance
acc1 = BankAccount(name="Pallavi", amount=4000)
acc2 = BankAccount(name="Poorva", amount=4500)
type(acc1)
__main__.BankAccount
acc1.deposit(1000)
acc1.get_balance()
5000
acc2.get_balance()
4500
acc2.deposit(5000)
acc2.get_balance()
9500
def is_prime(n):
    return len([f for f in range(1, n+1) if n%f==0])==2

class Prime:
    
    def __init__(self, value):
        if is_prime(value):
            self.value = value
        else:
            raise ValueError("The value passed is not prime")
p5 = Prime(5)
type(p5)
__main__.Prime
Prime(4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[57], line 1
----> 1 Prime(4)

Cell In[53], line 10, in Prime.__init__(self, value)
      8     self.value = value
      9 else:
---> 10     raise ValueError("The value passed is not prime")

ValueError: The value passed is not prime
int("hello")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[58], line 1
----> 1 int("hello")

ValueError: invalid literal for int() with base 10: 'hello'
acc3 = BankAccount(name="Vikrant", amount=3000)
isinstance(x, int)
True
isinstance(acc1, BankAccount)
True
isinstance(x, Prime)
False
class Food:

    def __init__(self, name, quantity, unitprice):
        
class Book:

    def __init__(self, title, author, genre):
        self.title = title
        self.author = author
        self.genre = genre
        self.chapters = []

    def add_chapter(self, chapter):
        self.chapters.append(chapter)
0.04874730110168457
    def get_chapter(self, index):
        return self.chapters[index]

    def get_index(self):
        return "\n".join([f"{i} {c.title}" for i, c in enumerate(self.chapters, start=1)])


class Chapter:

    def __init__(self, title, contents):
        self.title = title
        self.contents = contents

    def append_contents(self, additonal_text):
        self.contentst = self.contents + additonal_text

    def get_contents(self):
        return self.contents
    
book1 = Book(title="My First Book", author="Vikrant", genre="Fiction")
book1.get_index()
''
chapter1 = Chapter("First Lesson", "Some testkjhsd askjhdlskajd")
book1.add_chapter(chapter1)
book1.get_index()
'1 First Lesson'
chapter2 = Chapter("Second Lesson", "jkdsgdjf jhf jkhdgsf ka\njhfsdghafjgh\n j,f jhgsdf askdhg \n jhgfasdgkf")
book1.add_chapter(chapter2)
print(book1.get_index())
1 First Lesson
2 Second Lesson
class Food:

    def __init__(self, name):
        self.name = name


class Raw(Food): # Raw is child of Food
    pass


class Cooked(Food): # Cooked is child of Food
    pass
f1 = Cooked("Chapati")
f2 = Cooked("Bhakri")
f3 = Cooked("Rice")

f4 = Raw("Cucumber")
f5 = Raw("Tomato")
f6 = Raw("Sprouts")
type(f1)
__main__.Cooked
isinstance(f1, Cooked)
True
isinstance(f1, Raw)
False
isinstance(f1, Food)
True
book1
<__main__.Book at 0x7feebb9c8d70>
nums
[1, 2, 3, 4, 5, 6]
nums.append(0)
nums
[1, 2, 3, 4, 5, 6, 0]
import time
time.time() # time in miliseconds from epoch.. 1970...
1768665236.7330015
time.time()
1768665264.4353375
def computation(n):
    s = 0
    for i in range(n):
        for j in range(n):
            s = s + float(i)*float(j)
    return s
computation(1000)
249500250000.0
computation(10000)
2499500025000000.0
class StopWatch:

    def __init__(self):
        self.starttime = 0
        self.endtime = 0

    def start(self):
        self.starttime = time.time()

    def stop(self):
        self.stoptime = time.time()

    def get_time_elapsed(self):
        return self.stoptime - self.starttime
timer = StopWatch()
timer.start()
computation(10000)
timer.stop()
print(timer.get_time_elapsed())
4.107861280441284
help(time.time)
Help on built-in function time in module time:

time()
    time() -> floating-point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.
timer.start()
computation(1000)
timer.stop()
print(timer.get_time_elapsed())
0.04874730110168457