Assignment: Python Variables, Operators, and Functions
Part 1: Circumference of a Circle
The circumference of a circle is calculated using the formula: Circumference = 2πr, where π
≈ 3.14159. Below is the Python function `print_circum` that calculates and prints the
circumference of a circle given its radius.
Code:
# Part 1: Function to calculate and print the circumference of a circle
def print_circum(radius):
"""
This function calculates and prints the circumference of a circle
given the radius. The formula used is: Circumference = 2 * π * r
"""
pi = 3.14159 # Define the value of π (pi)
circumference = 2 * pi * radius # Calculate the circumference
print(f"The circumference of a circle with radius {radius} is {circumference:.2f}")
# Call the function with three different radii
print_circum(5) # Radius = 5
print_circum(10) # Radius = 10
print_circum(15) # Radius = 15
Explanation:
1. The function `print_circum(radius)` calculates the circumference of a circle using the
formula 2πr.
2. The function is called three times with radii of 5, 10, and 15.
3. Outputs:
- For radius 5: Circumference = 31.42
- For radius 10: Circumference = 62.83
- For radius 15: Circumference = 94.25
Part 2: Online Store Catalog
This part involves creating a catalog for an online store that sells three items. The catalog
includes prices for individual items, combo packs with two items, and a gift pack with all
three items. The discounts applied are:
- 10% for combo packs with two items.
- 25% for the gift pack containing all three items.
Code:
# Part 2: Function to create and display the online store catalog
def online_store_catalog():
"""
This function calculates the prices for individual items,
combo packs with two items (10% discount), and the gift pack with all three items (25%
discount).
It then displays the catalog in a structured format.
"""
# Prices of individual items
item1_price = 200.0
item2_price = 400.0
item3_price = 600.0
# Calculating discounted prices
combo1_price = (item1_price + item2_price) * 0.9 # 10% discount
combo2_price = (item2_price + item3_price) * 0.9 # 10% discount
combo3_price = (item1_price + item3_price) * 0.9 # 10% discount
gift_pack_price = (item1_price + item2_price + item3_price) * 0.75 # 25% discount
# Displaying the catalog
print("Online Store")
print("----------------------")
print(f"{'Product(s)':<25}{'Price':<10}")
print(f"{'Item 1':<25}{item1_price:<10.2f}")
print(f"{'Item 2':<25}{item2_price:<10.2f}")
print(f"{'Item 3':<25}{item3_price:<10.2f}")
print(f"{'Combo 1 (Item 1 + 2)':<25}{combo1_price:<10.2f}")
print(f"{'Combo 2 (Item 2 + 3)':<25}{combo2_price:<10.2f}")
print(f"{'Combo 3 (Item 1 + 3)':<25}{combo3_price:<10.2f}")
print(f"{'Combo 4 (Item 1 + 2 + 3)':<25}{gift_pack_price:<10.2f}")
print("-" * 22)
print("For delivery Contact: 98764678899")
# Call the function to display the catalog
online_store_catalog()
Explanation:
1. The catalog includes three individual items with prices of 200.0, 400.0, and 600.0.
2. Combo packs include:
- Combo 1 (Item 1 + Item 2): 540.0 (10% discount)
- Combo 2 (Item 2 + Item 3): 900.0 (10% discount)
- Combo 3 (Item 1 + Item 3): 720.0 (10% discount)
3. The gift pack includes all three items with a 25% discount, priced at 900.0.
4. The function calculates prices dynamically using arithmetic expressions.
References:
Downey, A. B. (2012). Think Python: How to Think Like a Computer Scientist. Green Tea
Press.