Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
388 changes: 388 additions & 0 deletions OOP.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,388 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"labra\n",
"5\n",
"7\n",
"saksham\n",
"saksham is 20 years old and is Student\n"
]
}
],
"source": [
"#Classes \n",
"#syntax \n",
"# class ClassName()\n",
"# def __init__(self,argument1,......)\n",
"# self.argument1 = argument1,\n",
"# .....\n",
"# def myfunc():\n",
"# .......\n",
"\n",
"#Classes are used to use common statements for different arguments...\n",
"\n",
"#Note : ClassName must be capitalize.......\n",
"\n",
"#Example 1:\n",
"\n",
"#Lets make class for dog \n",
"\n",
"class Dog():\n",
" #Attributes the class takes in\n",
" #is assigned to class using self method\n",
" #Not : It is very similar to javascript method 'this' with the differnce that in javascript, constructor func. is used and 'this' is not an argument.\n",
" def __init__(self,breed,age):\n",
" self.breed = breed\n",
" self.age = age\n",
" \n",
"my_dog = Dog('labra',5)\n",
"print(my_dog.breed)\n",
"print(my_dog.age)\n",
"\n",
"my_dog2 = Dog('huksy',7)\n",
"print(my_dog2.age)\n",
"\n",
"\n",
"\n",
"#Methods in Classes\n",
"#methods are the functions defined in classes.\n",
"\n",
"#Example 1:\n",
"\n",
"class Person():\n",
" def __init__(self,name,age,profession):\n",
" self.name = name\n",
" self.age = age\n",
" self.profession = profession\n",
" \n",
" def bio(self):\n",
" print('{} is {} years old and is {}'.format(self.name,self.age,self.profession))\n",
" \n",
"me = Person('saksham',20,'Student')\n",
"print(me.name)\n",
"me.bio()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Circle has the perimeter of 12.56\n",
"314.0\n",
"Hello World\n"
]
}
],
"source": [
"#Example 2:\n",
"\n",
"class Circle():\n",
" \n",
" #Class Argument\n",
" pi = 3.14\n",
" \n",
" #instance attribute\n",
" def __init__(self,radius):\n",
" self.radius = radius\n",
" self.area = radius*radius*self.pi\n",
" \n",
" def perimeter(self):\n",
" print('Circle has the perimeter of {}'.format(self.radius*2*self.pi))\n",
"\n",
"# instantiate the Circle class \n",
"circle1 = Circle(2) \n",
"# access the class attributes\n",
"circle.perimeter()\n",
"\n",
"circle2 = Circle(10)\n",
"print(circle2.area)\n",
"\n",
"\n",
"\n",
"# important: when we initiate class,then __init__ function runs automatically..\n",
"#For Ex:\n",
"class Example():\n",
" def __init__(self):\n",
" print('Hello World')\n",
" \n",
"exam1 = Example() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inheritance"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello from my side\n",
"I am an animal\n",
"Hello from my side\n",
"Hola\n",
"I love eating\n",
"I love barking\n"
]
}
],
"source": [
"#Inheritance is using one class inside another class and inherating one's class property to another class........\n",
"\n",
"\n",
"#Example 1:\n",
"\n",
"class Animal():\n",
" def __init__(self):\n",
" print('Hello from my side')\n",
" \n",
" def who_me(self):\n",
" print('I am an animal')\n",
" \n",
" def i_love(self):\n",
" print('I love eating')\n",
" \n",
"animal1 = Animal()\n",
"animal1.who_me()\n",
" \n",
"#Now creating another class and inherating Animal class init...\n",
"\n",
"class Dog(Animal):\n",
" def __init__(self):\n",
" Animal.__init__(self)#It inheritis Animal into Dog....\n",
" print('Hola')\n",
" \n",
" def i_love(self):\n",
" print('I love barking')#Overwriting the function...\n",
"\n",
"dog1 = Dog()\n",
"animal1.i_love()\n",
"dog1.i_love()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Polymorphism\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parrot's can fly\n",
"Penguin's can't fly\n"
]
}
],
"source": [
"#Polymorphism is an ability (in OOP) to use common interface for multiple form (data types).\n",
"\n",
"#Suppose, we need to color a shape, there are multiple shape option (rectangle, square, circle). \n",
"#However we could use same method to color any shape. This concept is called Polymorphism.\n",
"\n",
"class Parrot():\n",
" def fly(self):\n",
" print('Parrot\\'s can fly')\n",
" \n",
" def swim(self):\n",
" print('Parrot\\'s can\\'t fly')\n",
" \n",
"class Penguin():\n",
" def fly(self):\n",
" print('Penguin\\'s can\\'t fly')\n",
" \n",
" def swim(self):\n",
" print('Penguin can swim')\n",
" \n",
" \n",
"myParrot = Parrot()\n",
"myPenguin = Penguin()\n",
"\n",
"def flying_test(animal):\n",
" animal.fly()\n",
" \n",
" \n",
"flying_test(myParrot)\n",
"flying_test(myPenguin)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gone Girl by Gillian Flynn\n",
"278\n",
"This class is deleted\n"
]
}
],
"source": [
"#Some built-in methods :\n",
"\n",
"\n",
"class Book():\n",
" def __init__(self,title,author,pages):\n",
" self.title = title\n",
" self.author = author\n",
" self.pages = pages\n",
" \n",
" def __str__(self):\n",
" return f'{self.title} by {self.author}'\n",
" \n",
" def __len__(self):\n",
" return self.pages\n",
" \n",
" def __del__(self):\n",
" print('This class is deleted')\n",
" \n",
"b = Book('Gone Girl','Gillian Flynn',278)\n",
"print(str(b))\n",
"\n",
"print(len(b))\n",
"\n",
"del b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OOP Challenge:\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"For this challenge, create a bank account class that has two attributes:\n",
"\n",
"owner\n",
"balance\n",
"and two methods:\n",
"\n",
"deposit\n",
"withdraw\n",
"As an added requirement, withdrawals may not exceed the available balance.\n",
"\n",
"Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Amount 20 is added onto the account. Available balance is 40\n",
"Amount 100 is added onto the account. Available balance is 140\n",
"Amount 25 is withdrawn from the account. Available balance is 115\n",
"Amount 100 is withdrawn from the account. Available balance is 15\n"
]
},
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Bank():\n",
" \n",
" total=0\n",
" \n",
" def __init__(self,owner,balance):\n",
" self.owner = owner\n",
" self.balance = balance \n",
" self.total+=balance\n",
" \n",
" def deposit(self,amount):\n",
" self.total+=amount\n",
" print(f'Amount {amount} is added onto the account. Available balance is {self.total}')\n",
" \n",
" def withdraw(self,amount):\n",
" if self.total-amount<0:\n",
" print('Available balance is not sufficient')\n",
" else:\n",
" self.total-=amount\n",
" print(f'Amount {amount} is withdrawn from the account. Available balance is {self.total}')\n",
" \n",
" \n",
"myAccount = Bank('saksham',20) \n",
"myAccount.balance\n",
"myAccount.deposit(20)\n",
"myAccount.deposit(100)\n",
"myAccount.withdraw(25)\n",
"myAccount.withdraw(100)\n",
"myAccount.total"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading