Applying the Stock Data Class
Now that we have designed the StockData
class, let's explore how to use it in your Python projects.
Creating and Manipulating StockData Objects
To use the StockData
class, you can create instances of the class and interact with the stock data:
## Create a StockData object
stock = StockData(
ticker="AAPL",
company_name="Apple Inc.",
current_price=150.25,
open_price=148.90,
high_price=151.00,
low_price=147.80,
volume=25_000_000,
date="2023-04-24"
)
## Access the stock data
print(stock.ticker) ## Output: AAPL
print(stock.company_name) ## Output: Apple Inc.
print(stock.current_price) ## Output: 150.25
## Call the display_stock_info() method
stock.display_stock_info()
This demonstrates how you can create a StockData
object and access its attributes, as well as call the display_stock_info()
method to print the stock information.
Storing and Retrieving StockData Objects
You can store StockData
objects in various data structures, such as lists or dictionaries, to manage your stock data more effectively.
## Create a list of StockData objects
stock_data = [
StockData("AAPL", "Apple Inc.", 150.25, 148.90, 151.00, 147.80, 25_000_000, "2023-04-24"),
StockData("MSFT", "Microsoft Corporation", 280.50, 278.00, 282.75, 277.25, 18_000_000, "2023-04-24"),
StockData("AMZN", "Amazon.com, Inc.", 105.75, 103.90, 106.80, 102.50, 35_000_000, "2023-04-24")
]
## Retrieve and display stock data
for stock in stock_data:
stock.display_stock_info()
print()
This example demonstrates how you can store a collection of StockData
objects in a list and then iterate over them to display the stock information.
By applying the StockData
class, you can now efficiently manage and work with stock data in your Python projects.