Sidebar and Body Layout

Python code to get current Stock Price

The following Python code snippet utilizes the yfinance library to get the current stock price. This library provides a convenient interface to access financial data from Yahoo Finance. With this code, users can easily retrieve real-time stock prices for analysis, monitoring, or other financial applications.

For a detailed explanation, check out the video here.

Description of the image

import yfinance as yf

def get_tata_motors_price():
    # Ticker symbol for Tata Motors on the Bombay Stock Exchange (BSE)
    ticker_symbol = "TATAMOTORS.BO"  # .BO is the suffix for stocks listed on the BSE

    # Fetching stock data for Tata Motors
    tata_motors_data = yf.Ticker(ticker_symbol)

    # Getting the most recent price
    latest_price = tata_motors_data.history(period="1d")["Close"].iloc[-1]

    return latest_price

# Calling the function to get Tata Motors price
tata_motors_price = get_tata_motors_price()

# Printing the fetched Tata Motors price
print("Tata Motors Price:", tata_motors_price)