Define a Simple Class

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Define a simple class

Files Modified: stock.py

In Exercise 1.5, you defined a simple class Stock for representing a holding of stock. In this exercise, we're simply going to add a few features to that class as well as write some utility functions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132490{{"`Define a Simple Class`"}} python/with_statement -.-> lab-132490{{"`Define a Simple Class`"}} python/variables_data_types -.-> lab-132490{{"`Define a Simple Class`"}} python/numeric_types -.-> lab-132490{{"`Define a Simple Class`"}} python/type_conversion -.-> lab-132490{{"`Define a Simple Class`"}} python/conditional_statements -.-> lab-132490{{"`Define a Simple Class`"}} python/for_loops -.-> lab-132490{{"`Define a Simple Class`"}} python/lists -.-> lab-132490{{"`Define a Simple Class`"}} python/tuples -.-> lab-132490{{"`Define a Simple Class`"}} python/function_definition -.-> lab-132490{{"`Define a Simple Class`"}} python/importing_modules -.-> lab-132490{{"`Define a Simple Class`"}} python/standard_libraries -.-> lab-132490{{"`Define a Simple Class`"}} python/classes_objects -.-> lab-132490{{"`Define a Simple Class`"}} python/constructor -.-> lab-132490{{"`Define a Simple Class`"}} python/polymorphism -.-> lab-132490{{"`Define a Simple Class`"}} python/encapsulation -.-> lab-132490{{"`Define a Simple Class`"}} python/file_opening_closing -.-> lab-132490{{"`Define a Simple Class`"}} python/iterators -.-> lab-132490{{"`Define a Simple Class`"}} python/data_collections -.-> lab-132490{{"`Define a Simple Class`"}} python/python_shell -.-> lab-132490{{"`Define a Simple Class`"}} python/build_in_functions -.-> lab-132490{{"`Define a Simple Class`"}} end

Adding a new method

Add a new method sell(nshares) to Stock that sells a certain number of shares by decrementing the share count. Have it work like this:

>>> s = Stock('GOOG',100,490.10)
>>> s.shares
100
>>> s.sell(25)
>>> s.shares
75
>>>

Note:

Complete the sell(nshares) function in the stock.py file.

Reading a portfolio

Add a function read_portfolio() to your stock.py program that reads a file of portfolio data into a list of Stock objects. Here's how it should work:

>>> portfolio = read_portfolio('portfolio.csv')
>>> for s in portfolio:
        print(s)

<__main__.Stock object at 0x3902f0>
<__main__.Stock object at 0x390270>
<__main__.Stock object at 0x390330>
<__main__.Stock object at 0x390370>
<__main__.Stock object at 0x3903b0>
<__main__.Stock object at 0x3903f0>
<__main__.Stock object at 0x390430>
>>>

You already wrote a similar function as part of Exercise 2.3. Design discussion: Should read_portfolio() be a separate function or part of the class definition?

Note:

Add the read_portfolio() function in the stock.py file.

Printing a Table

Table the data read in step 2 and use it to make a nicely formatted table. For example:

>>> portfolio = read_portfolio('portfolio.csv')
>>> for s in portfolio:
           print('%10s %10d %10.2f' % (s.name, s.shares, s.price))

        AA        100      32.20
       IBM         50      91.10
       CAT        150      83.44
      MSFT        200      51.23
        GE         95      40.37
      MSFT         50      65.10
       IBM        100      70.44
>>>

Take this code and put it in a function print_portfolio() that produces the same output, but additionally adds some table headers. For example:

>>> portfolio = read_portfolio('portfolio.csv')
>>> print_portfolio(portfolio)
      name     shares      price
---------- ---------- ----------
        AA        100      32.20
       IBM         50      91.10
       CAT        150      83.44
      MSFT        200      51.23
        GE         95      40.37
      MSFT         50      65.10
       IBM        100      70.44
>>>

Note:

Complete the print_portfolio() function in the stock.py file.

Summary

Congratulations! You have completed the Define a Simple Class lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like