Overwriting previous file with inheritance added

master
mehul 2018-06-22 16:29:10 +10:00 committed by GitHub
parent 07e024000c
commit 2322b3951c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 469 additions and 8 deletions

View File

@ -136,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@ -154,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@ -163,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@ -172,7 +172,7 @@
"4"
]
},
"execution_count": 17,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@ -183,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@ -192,7 +192,7 @@
"50.24"
]
},
"execution_count": 18,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@ -203,7 +203,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -212,7 +212,7 @@
"25.12"
]
},
"execution_count": 20,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@ -221,6 +221,467 @@
"my_circle.get_circumference()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inheritance"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"class Area():\n",
" def __init__(self,radius):\n",
" self.radius = radius\n",
" \n",
" def diameter(self):\n",
" return 2*self.radius"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"class Circle(Area):\n",
" \n",
" pi = 3.14\n",
" \n",
" def __init__(self,radius):\n",
" Area.__init__(self,radius)\n",
" \n",
" def circumference(self):\n",
" return 2*Circle.pi*self.radius"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"myarea = Area(3)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myarea.diameter()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"mycircle = Circle(5)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31.400000000000002"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycircle.circumference()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mycircle.diameter()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### OOP Homework"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"class Line:\n",
" \n",
" def __init__(self,coor1,coor2):\n",
" self.coor1 = coor1\n",
" self.coor2 = coor2\n",
" \n",
" def distance(self):\n",
" return ((self.coor2[0]-self.coor1[0])**2 + (self.coor2[1]-self.coor1[1])**2)**0.5\n",
" \n",
" def slope(self):\n",
" return ((self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0]))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"coordinate1 = (3,2)\n",
"coordinate2 = (8,10)\n",
"\n",
"li = Line(coordinate1,coordinate2)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9.433981132056603"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"li.distance()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.6"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"li.slope()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"class Cylinder:\n",
" \n",
" pi = 3.14\n",
" \n",
" def __init__(self,height=1,radius=1):\n",
" self.height = height\n",
" self.radius = radius\n",
" \n",
" def volume(self):\n",
" return Cylinder.pi*self.radius**2*self.height\n",
" \n",
" def surface_area(self):\n",
" return (2*Cylinder.pi*self.radius)*(self.height+self.radius)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"c = Cylinder(2,3)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"56.52"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.volume()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"94.2"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.surface_area()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Challenge"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this challenge, create a bank account class that has two attributes:\n",
"\n",
"* owner\n",
"* balance\n",
"\n",
"and two methods:\n",
"\n",
"* deposit\n",
"* withdraw\n",
"\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": 51,
"metadata": {},
"outputs": [],
"source": [
"class Account:\n",
" \n",
" def __init__(self,owner,balance):\n",
" self.owner = owner\n",
" self.balance = balance\n",
" \n",
" def __str__(self):\n",
" return f'Account Owner : {self.owner}\\nAccount Balance : ${self.balance}'\n",
" \n",
" def deposit(self,amount):\n",
" self.balance += amount\n",
" print ('Deposit Accepted')\n",
" \n",
" def withdraw(self,amount):\n",
" if amount > self.balance:\n",
" print('Funds Unavilable!')\n",
" else:\n",
" self.balance -= amount\n",
" print('Withdrawal Accepted')"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"# 1. Instantiate the class\n",
"acct1 = Account('Jose',100)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Account Owner : Jose\n",
"Account Balance : $100\n"
]
}
],
"source": [
"# 2. Print the object\n",
"print(acct1)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Jose'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 3. Show the account owner attribute\n",
"acct1.owner"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 4. Show the account balance attribute\n",
"acct1.balance"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deposit Accepted\n"
]
}
],
"source": [
"# 5. Make a series of deposits and withdrawals\n",
"acct1.deposit(50)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Withdrawal Accepted\n"
]
}
],
"source": [
"acct1.withdraw(75)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Funds Unavilable!\n"
]
}
],
"source": [
"# 6. Make a withdrawal that exceeds the available balance\n",
"acct1.withdraw(500)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"75"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"acct1.balance"
]
},
{
"cell_type": "code",
"execution_count": null,