Using Python to Calculate Percent Increase
In this activity, you will create a Python program to perform percent increase calculations.
Background
For the last week, you’ve been watching your coworker Harold use a calculator to manually track stock price fluctuations. Harold spends three hours every day tracking his stocks this way, writing down the stock’s original price (from the day before), current price, and the percentage of increase/decrease. You know that if this calculation were automated, Harold could streamline his daily process and spend more time making investment decisions. Your newly acquired Python skills can help you achieve this for him.
For this activity, create a Python program to automate Harold’s process by programmatically implementing the percent increase calculation.
Start small by identifying the percent increase in price for Apple stock.
- Yesterday at 9:00 a.m., Apple’s stock price was $198.87.
- At close of market today, the stock price was $254.32.
- Calculate the percent increase using the formulas below.
- Increase = Current Price – Original Price
- Percent Increase = Increase / Original x 100
Starter file
# Hello Variable World Activity
# Formulas Needed:
# Increase = Current Price - Original Price
# Percent Increase = Increase / Original x 100
# Create float variable for original_price
# Create float variable for current_price
# Calculate difference between current_price and original_price
# Calculate percent_increase
# Print original_price
print(f"Apple's original stock price was ${original_price}")
# Print current_price
print(f"Apples current stock price is ${current_price}")
# Print percent_increase
# CHALLENGE:
# Use a format specifier with the f-string to print the percent increase with two decimal places:
Instructions
Using the starter file, complete the following steps:
- Create variables for the following:
original_pricecurrent_priceincreasepercent_increase
- Derive
increase. - Derive
percent_increase. - Print
original_price,current_price, andpercent_increaseto the screen.
Challenge
Use a format specifier with the f-string to print the percent increase with two decimal places: 27.88%.
Hint
For additional help with f-strings, visit Python 3’s f-Strings.
Solution
# Hello Variable World Activity
# Formulas Needed:
# Increase = Current Price - Original Price
# Percent Increase = Increase / Original x 100
# Create float variable for original_price
original_price = 198.87
# Create float variable for current_price
current_price = 254.32
# Calculate difference between current_price and original_price
increase = current_price - original_price
# Calculate percent increase
percent_increase = (increase / original_price) * 100
# Print original_price
print(f"Apple's original stock price was ${original_price}")
# Print current_price
print(f"Apple's current stock price is ${current_price}")
# Print percent increase
print(f"Apple's stock price increased by ${percent_increase}")
# CHALLENGE:
# Use a format specifier with the f-string to print the percent increase with two decimal places:
print(f"Apples percent increase in price is {percent_increase:.2f}%")