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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
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 numberDiff line numberDiff 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