Unit 2: Programming Assignment: Variables, Expressions, Statements, and Function
CS 1101-01 Programming Fundamentals - AY2025-T5
)
Part 1
Calculating the Circumference of a Circle
Circumference = 2πr
π (pi) would approximately be 3.14159.
And r stands for the radius of the circle.
The Print_ Circum(radius) function finds the circumference for the circle and then prints the
output.
The circumference of a circle of `radius` is obtained by the formula 2πr, where π is taken
approximately as 3.14159. The function is a typical example in Python for arithmetic
operations, variable assignment, and formatted output (Downey, 2015).
# Function to calculate and print the circumference of a circle
def print_circum(radius):
pi=3.14159 # Pi approximated to five decimal places
circumference=2*pi*radius # Apply the formula
print(f"The circumference of a circle with radius {radius} is {circumference:.2f}")
# Call the function three times with different radius values
print_circum(5)
print_circum(10)
print_circum(15)
Output of the Function:
The circumference of a circle with radius 5 is 31.42.
The circumference of a circle with radius 10 is 62.83.
The circumference of a circle with radius 15 is 94.25.
This function uses f-strings for clear printing and shows how to reuse code.
Part 2
The `display_store` function simulates an online product catalog, with a list of products and
combo purchase discounts. Python arithmetic is used to apply the 10% and 25% discounts,
putting into perspective conditional logic in practical use and modular functions (Severance,
2016; Lau & Johnson, 2011).
def display_store():
item1 = 200.0
item2 = 400.0
item3 = 600.0
# Combo discounts
combo1 = (item1 + item2) * 0.90 # 10% off
combo2 = (item2 + item3) * 0.90
combo3 = (item1 + item3) * 0.90
combo4 = (item1 + item2 + item3) * 0.75 # 25% off gift pack
Output:
Explanation
This function sets up a very basic product catalog along with discounts on a logical basis:
Individual items get no discounts.
Combos of 2 items get a 10 percent discount.
Gift pack (all 3 items) gets a 25-percent discount.
Python variables and arithmetic operations are used throughout pricing-calculation lines of
the script, followed by formatting with print() for clarity.
Conclusion
Part 1 highlights transforming a geometric formula into a reusable function and how Python
can be used to model real-world formulas (Downey, 2015). Part 2 models the online shopping
situation with pricing rules and discount policies. The function takes into account arithmetic
calculations and conditional structures that are common practices in business systems and e-
commerce environments (Lau & Johnson, 2011). This assignment gives an example of
modular programming, logic application, and formatted output-generic skills defined in
Python-based development.
References
Downey, A. (2015). *Think Python: How to Think Like a Computer Scientist* (2nd ed.).
O’Reilly Media.
Lau, T., & Johnson, L. (2011). *The legal and ethical environment of business* (Vol. 1). Flat
World Knowledge.
Severance, C. (2016). *Python for Everybody: Exploring Data in Python 3*. CreateSpace
Independent Publishing Platform.