0% found this document useful (0 votes)
7 views8 pages

Python Functions for Circle and Catalog

This document outlines two programming assignments focused on Python functions. The first assignment involves creating a function to calculate the circumference of a circle using a given radius, while the second assignment simulates a product catalog system that applies discounts based on the number of items purchased. Both assignments emphasize the use of functions, parameters, and conditional logic in Python programming.

Uploaded by

maeiei1041975
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Python Functions for Circle and Catalog

This document outlines two programming assignments focused on Python functions. The first assignment involves creating a function to calculate the circumference of a circle using a given radius, while the second assignment simulates a product catalog system that applies discounts based on the number of items purchased. Both assignments emphasize the use of functions, parameters, and conditional logic in Python programming.

Uploaded by

maeiei1041975
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1

Variables, Expressions, Statements, and Functions

Myat Ei Zin

University of the People

CS 1101-01 Programming Fundamentals – AY 2025 – T5

Instructor Name: Gbenga Vincent Dania

3rd July, 2025

Unit-2
2

Part 1: Calculating the Circumference of a Circle

In this assignment, we are asked to create a Python function that calculates and prints the

circumference of a circle based on the formula

Circumference = 2 × π × r

π (pi) = 3.14159, and

r = the radius of the circle

The function will accept the radius as a parameter and print the result. We will also call the

function three times with different radii to demonstrate how it works with various inputs.

Python functions allow us to reuse code and make our programs more organized and readable

(Downey, 2015). By using parameters, we can pass different values into our function to calculate

different results dynamically.

Python Code with Comments

Function to calculate and print the circumference of a circle

def print_circum(radius)

Unit-2
3

pi = 3.14159 # Approximate value of pi

circumference = 2 * pi * radius # Formula to calculate circumference

print(f"The circumference of a circle with radius {radius} is {circumference:.2f}")

# Call the function with three different radius values

print_circum(5) # First call with radius = 5

print_circum(10.5) # Second call with radius = 10.5

print_circum(2) # Third call with radius = 2

The circumference of a circle with radius 5 = 31.42

The circumference of a circle with radius 10.5 = 65.97

The circumference of a circle with radius 2 = 12.57

Explanation of the Code and Output

The function print_circum () takes one argument: radius.

Inside the function, pi = 3.14159

The circumference is calculated using the formula 2 * pi * radius.

The result is printed using an f-string with formatting to two decimal places (:.2f).

Unit-2
4

We called the function three times

- With radius 5, resulting in a circumference of 31.42.

- With radius 10.5, giving a larger circumference of 65.97.

- With radius 2, resulting in a smaller circumference of 12.57.

This demonstrates how changing the input parameter affects the output, which is a key concept

when working with functions in Python.

Part 2: Catalog Purchase Function for a Company

In this task, I created a Python function that simulates a product catalog system for a company

that sells three items. The company allows customers to buy items individually, in a combination

of two (combo pack), or all three together (gift pack). The function handles pricing and discounts

based on the type of purchase, as described below

A. No discount is applied for individual items.

B. A 10% discount is applied when two unique items are purchased.

C. A 25% discount is applied for a gift pack containing all three items.

Unit-2
5

Python Code with Comments

# Define item prices

item_prices = {

"ItemA": 50,

"ItemB": 30,

"ItemC": 20

# Function to calculate price with applicable discounts

def calculate_total(*items)

total = sum(item_prices[item] for item in items)

discount = 0

if len(set(items)) == 2

discount = 0.10 # 10% discount for combo pack

elif len(set(items)) == 3

discount = 0.25 # 25% discount for gift pack

discounted_total = total * (1 - discount)

print(f"Items purchased: {', '.join(items)}")

print(f"Original total: ${total:.2f}")

print(f"Discount applied: {int(discount * 100)}%")

Unit-2
6

print(f"Final total after discount: ${discounted_total:.2f}\n")

Output

Online Store

Product (S) Price

Item A $ 50

Item B $ 30

Item C $ 20

Combo 1 (Item A + B) $ 80

Combo 2 (Item B + C) $ 50

Combo 3 (Item A + C) $ 70

Combo 4 (Item A + B + C) $ 100

Items purchased: ItemA

Original total: $50.00

Discount applied: 0%

Final total after discount: $50.00

Items purchased: ItemA, ItemB

Original total: $80.00

Discount applied: 10%

Final total after discount: $72.00

Unit-2
7

Items purchased: ItemA, ItemB, ItemC

Original total: $100.00

Discount applied: 25%

Final total after discount: $75.00

Technical Explanation of the Code and Output

The function `calculate_total` uses variable-length arguments (*items) to allow flexibility in how

many items a customer chooses to purchase. It calculates the total price using a dictionary of

item prices. Based on how many unique items are included in the purchase, it applies the

appropriate discount (0%, 10%, or 25%). The function then prints a detailed breakdown of the

items purchased, original price, discount percentage, and the final amount to be paid.

The function illustrates concepts like dictionaries for item storage, conditionals for logic, and the

use of `*args` to accept multiple parameters dynamically.

Unit-2
8

References

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). O’Reilly

Media. [Link]

Unit-2

You might also like