Introduction
In this tutorial, we'll explore how to analyze stock market data and investment offers using Python. This tutorial is inspired by the recent news about Uber's takeover offer for Delivery Hero. You'll learn how to fetch stock data, calculate key metrics like discounts, and visualize investment opportunities. This beginner-friendly guide requires no prior financial analysis experience.
Prerequisites
- Basic understanding of Python programming
- Python 3.x installed on your computer
- Internet connection
Step-by-Step Instructions
Step 1: Install Required Python Libraries
First, we need to install the necessary Python packages for data analysis and visualization. Open your terminal or command prompt and run:
pip install yfinance matplotlib pandas
This installs three essential packages: yfinance for fetching stock data, matplotlib for creating charts, and pandas for data manipulation.
Step 2: Create a Python Script
Create a new file called stock_analysis.py and open it in your code editor. This will be our main analysis script.
Step 3: Import Required Libraries
At the top of your script, add these import statements:
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
These imports bring in the functionality we need: yfinance for stock data, pandas for data handling, and matplotlib for visualization.
Step 4: Fetch Stock Data
Now we'll fetch the stock data for both Uber and Delivery Hero:
# Fetch stock data for Uber and Delivery Hero
uber = yf.Ticker("UBER")
delivery_hero = yf.Ticker("DLHR.DE")
# Get current stock prices
uber_price = uber.info['currentPrice']
delivery_hero_price = delivery_hero.info['currentPrice']
print(f"Uber current price: ${uber_price}")
print(f"Delivery Hero current price: ${delivery_hero_price}")
This code fetches real-time stock prices from Yahoo Finance. The 'currentPrice' field gives us the most recent trading price.
Step 5: Calculate the Offer Discount
Based on the news, Uber offered €33 per share for Delivery Hero. Let's calculate how this compares to the current market price:
# Uber's offer for Delivery Hero
offer_price = 33.0 # €33 per share
# Calculate discount percentage
if delivery_hero_price > 0:
discount = ((delivery_hero_price - offer_price) / delivery_hero_price) * 100
print(f"Offer discount: {discount:.2f}%")
else:
print("Could not calculate discount")
This calculation shows how much below the current market price Uber's offer sits, which is exactly what the news article mentioned.
Step 6: Create a Data Visualization
Let's create a simple chart to visualize the comparison:
# Create a simple bar chart
stocks = ['Delivery Hero Current', 'Uber Offer']
prices = [delivery_hero_price, offer_price]
plt.figure(figsize=(8, 6))
plt.bar(stocks, prices, color=['blue', 'green'])
plt.title('Delivery Hero Stock Comparison')
plt.ylabel('Price (€)')
plt.grid(axis='y')
# Add value labels on bars
for i, (stock, price) in enumerate(zip(stocks, prices)):
plt.text(i, price + 0.5, f'€{price:.2f}', ha='center')
plt.tight_layout()
plt.show()
This visualization clearly shows the relationship between the current market price and the offer price, making it easy to see the discount.
Step 7: Analyze the Investment Scenario
Let's add some logic to understand what happens if you were to invest:
# Simulate investment analysis
investment_amount = 10000 # €10,000 investment
# Calculate how many shares you could buy at current price
shares_current = investment_amount // delivery_hero_price
shares_offer = investment_amount // offer_price
print(f"With €{investment_amount:,}:")
print(f" Current price shares: {shares_current}")
print(f" Offer price shares: {shares_offer}")
print(f" Difference: {shares_offer - shares_current} more shares with offer")
This analysis helps investors understand the potential benefits of the offer in terms of purchasing power.
Step 8: Run Your Analysis
Save your script and run it in your terminal:
python stock_analysis.py
You should see output showing current prices, discount percentages, and investment calculations.
Summary
In this tutorial, you've learned how to analyze stock market data using Python. You created a script that fetches real-time stock prices, calculates the discount percentage of Uber's offer compared to Delivery Hero's current market price, and visualized the comparison. This hands-on approach gives you practical experience in financial data analysis that you can apply to other investment scenarios.
While this tutorial focuses on a specific news event, the skills you've learned can be applied to analyze any stock or investment opportunity. The key takeaway is understanding how to compare offer prices against current market values, which is exactly what investors need to do when evaluating takeover bids.



