Introduction
Objectives:
- Review of how to define a simple object
Files Created: stock.py
Objectives:
Files Created: stock.py
Create a file stock.py
and define the following class:
class Stock:
def __init__(self, name, shares, price):
self.name = name
self.shares = shares
self.price = price
def cost(self):
return self.shares * self.price
Once you have done this, run your program and experiment with your new Stock
object:
Note: To do this, you might have to run python using the -i
option. For example:
python3 -i stock.py
>>> s = Stock('GOOG',100,490.10)
>>> s.name
'GOOG'
>>> s.shares
100
>>> s.price
490.1
>>> s.cost()
49010.0
>>> print('%10s %10d %10.2f' % (s.name, s.shares, s.price))
GOOG 100 490.10
>>> t = Stock('IBM', 50, 91.5)
>>> t.cost()
4575.0
>>>
Congratulations! You have completed the Define a Simple Object lab. You can practice more labs in LabEx to improve your skills.