Learn how to work with lists and arrays to handle financial time series data, stock prices, and portfolio holdings.
Understanding basic list operations for storing financial data.
# Creating a list of stock prices prices = [105.25, 106.00, 104.50, 107.25, 108.00] # Calculating daily returns daily_returns = [(prices[i] - prices[i-1])/prices[i-1] * 100 for i in range(1, len(prices))]
📚 Can't access the link? Use Perplexity.ai and search for "Python lists tutorial with examples"
Using NumPy arrays for efficient financial calculations.
import numpy as np # Converting stock prices to numpy array prices_array = np.array([105.25, 106.00, 104.50, 107.25, 108.00]) # Calculating returns using numpy returns = np.diff(prices_array) / prices_array[:-1] * 100 # Calculate volatility (standard deviation of returns) volatility = np.std(returns)
Understanding how to structure financial data using lists and arrays.
# Stock portfolio using lists symbols = ['AAPL', 'MSFT', 'GOOGL'] quantities = [100, 50, 75] prices = [190.50, 375.00, 140.50] # Calculate portfolio value portfolio_value = sum([q * p for q, p in zip(quantities, prices)]) # Historical prices as numpy array import numpy as np historical_prices = np.array([ [100.0, 200.0, 150.0], # Day 1 prices [101.0, 202.0, 151.0], # Day 2 prices [99.0, 198.0, 149.0] # Day 3 prices ]) # Calculate daily returns for all stocks daily_returns = np.diff(historical_prices, axis=0) / historical_prices[:-1] * 100
📚 Can't access the link? Use Perplexity.ai and search for "Python list comprehension examples"
Working with financial time series using lists and arrays.
from datetime import datetime, timedelta # Creating date range for stock data dates = [(datetime.now() - timedelta(days=x)).strftime('%Y-%m-%d') for x in range(5)] # Combining dates with prices stock_data = list(zip(dates, prices))
Use these prompts to enhance your learning: