π Day 1: Python for Business Analytics - First Steps
Welcome to the Course
Welcome, future business leader! You're about to take your first step into a larger world of data-driven decision-making. In today's business landscape, the ability to understand and leverage data is not just a technical skillβit's a core competency for effective management. This course is designed specifically for MBA students like you. We'll skip the abstract computer science jargon and focus on one thing: using Python as a powerful tool to solve real-world business problems.
You don't need any prior coding experience. We'll start from zero and build your skills step-by-step. By the end of this 50-day journey, you'll be able to manipulate data, generate insights, and even build predictive models.
Why Python for Business?
- The Lingua Franca of Data: Python is the most widely used language for data science, machine learning, and analytics.
- Automation of Tedious Tasks: Automate cleaning messy Excel sheets, gathering web data, or generating weekly reports.
- Powerful Analytics at Your Fingertips: Python's extensive libraries allow for complex statistical analysis and compelling data visualizations.
- Strategic Advantage: Understanding the language of your data science team gives you a significant strategic advantage.
Environment Setup
Before you begin, please follow the setup instructions in the main README.md at the root of this repository. This will guide you through:
- Cloning the project.
- Setting up a virtual environment.
- Installing all the necessary libraries from
requirements.txt.
Once you have completed those steps and activated your virtual environment, you are ready to start this lesson.
Your First Python Script: A Business Calculation
Let's explore the code for today's lesson. The script for this lesson is helloworld.py.
- Review the Code: Open
Day_01_Introduction/helloworld.pyin your code editor. You will see that the code is now organized into functions, which is a best practice for writing clean and reusable code. - Run the Script: To run the script, make sure your terminal is in the root directory of this project (the
Coding-For-MBAfolder) and your virtual environment is active. Then, execute the script by running:python Day_01_Introduction/helloworld.py
You will see the output of the business calculations printed to your console.
π» Exercises: Day 1
The exercises are designed to help you practice the fundamental concepts introduced in the script.
-
Company Introduction:
-
Create a new Python file named
my_solutions.pyin theDay_01_Introductionfolder. - In your new script, use the
print()function to introduce a fictional company. -
Example:
print("Welcome to InnovateCorp Analytics") -
Quarterly Sales Calculation:
-
A company had quarterly sales of $110,000, $120,000, $135,000, and $140,000.
-
In your script, use the
print()function to calculate and display the total annual sales. (Hint: you can do math right inside the print statement:print(110000 + 120000 + ...)). -
Checking Data Types in Business:
-
Use the
type()function to check the data types of the following business-related data points.1500(e.g., number of units sold)1500.75(e.g., a price or a financial metric)'InnovateCorp'(e.g., a company name)True(e.g., is the product in stock?)
π Congratulations! You've just run your first refactored Python script and are on your way to becoming a data-savvy leader.
Previous: None (First Lesson) β’ Next: Day 02 β Day 2: Storing and Analyzing Business Data
You are on lesson 1 of 108.
Additional Materials
- helloworld.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
- solutions.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
helloworld.py
"""
Day 1: Python for Business Analytics - First Steps (Refactored)
This script demonstrates basic Python concepts using business-relevant examples.
We will perform a simple profit calculation and check the types of various
business-related data points. This version is refactored to use functions.
"""
def calculate_gross_profit(revenue, cogs):
"""
Calculates the gross profit from revenue and COGS.
Gross Profit is a key business metric that shows how much money
a company makes after deducting the costs associated with making
and selling its products.
Parameters
----------
revenue : float or int
Total sales revenue (money earned from sales)
cogs : float or int
Cost of Goods Sold (direct costs of producing goods)
Returns
-------
float or int
The gross profit (revenue minus COGS)
Example
-------
>>> calculate_gross_profit(500000, 350000)
150000
"""
# Simple subtraction: Revenue - Costs = Profit
return revenue - cogs
def calculate_gross_profit_margin(gross_profit, revenue):
"""
Calculates the gross profit margin as a percentage.
Gross Profit Margin shows what percentage of revenue is actual profit
after accounting for direct costs. It's a key indicator of business efficiency.
Parameters
----------
gross_profit : float or int
The gross profit amount
revenue : float or int
Total revenue amount
Returns
-------
float
The gross profit margin as a percentage (0-100)
Example
-------
>>> calculate_gross_profit_margin(150000, 500000)
30.0
"""
# Prevent division by zero - edge case handling
if revenue == 0:
return 0
# Formula: (Gross Profit / Revenue) Γ 100
return (gross_profit / revenue) * 100
def display_business_analytics(revenue, cogs):
"""
Calculates and displays key business metrics in a formatted dashboard.
This function demonstrates how to combine multiple calculations
and present them in a readable format - a common task in business analytics.
Parameters
----------
revenue : float or int
Total revenue for the period
cogs : float or int
Cost of Goods Sold for the period
"""
# Print a welcoming header for the dashboard
print("Welcome to the Quarterly Business Review Dashboard")
print() # Empty line for spacing
# Step 1: Calculate the gross profit using our helper function
gross_profit = calculate_gross_profit(revenue, cogs)
# Step 2: Calculate the gross profit margin percentage
gross_profit_margin = calculate_gross_profit_margin(gross_profit, revenue)
# Step 3: Display all metrics in a formatted way
# The f-string (f"...") allows us to embed variables directly in strings
print(f"Total Revenue: ${revenue}")
print(f"Cost of Goods Sold: ${cogs}")
print(f"Gross Profit: ${gross_profit}")
print()
# The :.2f formats the number to 2 decimal places
print(f"Gross Profit Margin: {gross_profit_margin:.2f}%")
print("-" * 20) # Visual separator
def display_data_types():
"""
Displays the types of various business-related data points.
Understanding data types is fundamental in Python. Different types
of data have different capabilities and uses in business analytics.
"""
print("Checking the types of some common business data points:")
# Integer (int): Whole numbers like counts or quantities
units_sold = 1500
# Float: Decimal numbers like prices or percentages
product_price = 49.99
# String (str): Text data like names or descriptions
company_name = "InnovateCorp"
# Boolean (bool): True/False values for yes/no questions
is_in_stock = True
# List: Ordered collection of items (can be mixed types)
quarterly_sales = [110000, 120000, 135000, 140000]
# The type() function tells us what data type a variable holds
print(f"Data: {units_sold}, Type: {type(units_sold)}")
print(f"Data: {product_price}, Type: {type(product_price)}")
print(f"Data: '{company_name}', Type: {type(company_name)}")
print(f"Data: {is_in_stock}, Type: {type(is_in_stock)}")
print(f"Data: {quarterly_sales}, Type: {type(quarterly_sales)}")
if __name__ == "__main__":
# This special condition checks if this file is being run directly
# (not imported as a module). It's a Python best practice.
# --- Basic Business Calculations ---
# Define our input values (in dollars)
revenue_main = 500000 # Half a million in revenue
cogs_main = 350000 # $350k in costs
# Call our function to calculate and display analytics
display_business_analytics(revenue_main, cogs_main)
# --- Understanding Data Types in a Business Context ---
# Show examples of different Python data types commonly used in business
display_data_types()
solutions.py
"""
Day 1: Solutions to Exercises
"""
# --- Exercise 1: Company Introduction ---
print("--- Solution to Exercise 1 ---")
print("Welcome to InnovateCorp Analytics")
print("-" * 20)
# --- Exercise 2: Quarterly Sales Calculation ---
print("--- Solution to Exercise 2 ---")
# The calculation is done directly inside the print function.
print("Total Annual Sales:")
print(110000 + 120000 + 135000 + 140000)
print("-" * 20)
# --- Exercise 3: Checking Data Types in Business ---
print("--- Solution to Exercise 3 ---")
# Using the type() function to inspect each data point.
print("Data point: 1500, Type:", type(1500))
print("Data point: 1500.75, Type:", type(1500.75))
print("Data point: 'InnovateCorp', Type:", type("InnovateCorp"))
print("Data point: True, Type:", type(True))
print("-" * 20)