0% found this document useful (0 votes)
15 views5 pages

Programming Assignment Unit 2

The assignment explores Python programming fundamentals, focusing on variables, operators, expressions, and functions. Part 1 demonstrates calculating the circumference of a circle using a parameterized function, while Part 2 simulates an online store with conditional logic for pricing and discounts. The exercises reinforce key concepts from the textbook 'Think Python' and highlight Python's strengths in simplicity and readability.

Uploaded by

harrygamtel
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)
15 views5 pages

Programming Assignment Unit 2

The assignment explores Python programming fundamentals, focusing on variables, operators, expressions, and functions. Part 1 demonstrates calculating the circumference of a circle using a parameterized function, while Part 2 simulates an online store with conditional logic for pricing and discounts. The exercises reinforce key concepts from the textbook 'Think Python' and highlight Python's strengths in simplicity and readability.

Uploaded by

harrygamtel
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

Assignment: Python Variables, Operators, Expressions, and Functions

Student Name: Harry Joof


Course Name: CS 1101 - Programming Fundamentals
Date: 16th September, 2025

Part 1: Calculating the Circumference of a Circle

Technical Explanation

In Python, we use functions to encapsulate reusable logic. This part demonstrates how to define a
function with parameters and use arithmetic expressions to perform calculations. According to
the formula for the circumference of a circle, the circumference CCC is given by:
C=2πrC = 2 \pi rC=2πr
Where:

 π≈3.14159\pi \approx 3.14159π≈3.14159

 rrr is the radius of the circle.

We will define a function called print_circum that takes the radius as an input parameter and
prints the calculated circumference.

Python Code:

Explanation of Code and Output:

We define the function print_circum(radius) which uses the value of pi = 3.14159.


The expression 2 * pi * radius calculates the circumference.
The function is then called three times using different radii: 5, 10.5, and 0.
The output is printed with two decimal places using Python’s formatted string literals (f-strings).

Sample Output:

Each function call correctly calculates and prints the circumference based on the input radius.
This illustrates how parameters work in functions and how expressions are evaluated in Python.

Part 2: Catalog Function for an Online Store

Technical Description

This part simulates a simple online store scenario using Python functions and conditional logic.
The function will handle:
 Individual item purchases (no discount),
 Two-item combos (10% discount),
 Gift packs (all three items, 25% discount).
This exercise demonstrates the use of:
 Function arguments
 Conditionals (if, elif, else)
 Calculations and formatted output

Python Code:
Explanation of Output:
This function simulates an e-commerce transaction. It calculates the total price based on the
items selected and applies applicable discounts based on the number of unique items:
 No discount for a single item
 10% discount for two unique items
 25% discount for all three items
The use of a dictionary (item_prices) makes it easy to update prices in the future. The
calculate_total() function uses Python's list, string, and arithmetic operations to compute and
display the output neatly.

Sample Output:

This demonstrates conditionals, functions, and formatted output in Python, while also modeling a
real-world scenario.

Descriptive Explanation
In this assignment, I explored the foundational principles of Python programming including
variables, expressions, arithmetic operators, and functions. In Part 1, the function print_circum
calculates the circumference of a circle using a parameterized approach, allowing for reusable
logic across multiple inputs. This reinforces how values can be passed into functions, how
mathematical formulas are implemented in code, and how results can be printed in a readable
format.
Part 2 expanded upon function design by introducing conditionals and more complex logic. It
simulated an online store’s billing system where different discounts are applied based on the
combination of items purchased. The design used a dictionary to hold item prices, which is a
common Python data structure for managing key value pairs. The function calculate total
processes a list of item names, applies the appropriate discount logic using conditional
statements, and calculates the final price. The output is clearly structured, providing subtotal,
discount, and total just like a real receipt.
These exercises illustrate Python’s core strengths: simplicity, readability, and power. They also
reinforce Chapter 2 and Chapter 3 concepts from the textbook Think Python, such as function
calls, return values, type conversion, and debugging strategies. The ability to write flexible,
readable code is foundational for solving more complex programming problems in the future.

References

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). Green
Tea Press. [Link]

You might also like