Storing Information with Variables
The idea
Imagine visiting your favorite café. You order a coffee, a sandwich, and a muffin. The cashier enters your items, and the system prints a neat little bill showing each item, its price, and the final amount. That bill is not typed by hand every time, it’s generated by a program.
In this lesson, we’ll design our own simple café billing system. No complicated features yet, just a receipt with correct calculations.
This is what the bill looks like:

You’ve been hired to code this café system! Look closely at the sample receipt.
You already know how to print text on the screen, but now we don’t want to hardcode everything. Instead, we’ll use variables so that if the price or quantity changes, the program adjusts the total automatically.
Solve idea with Python
Today’s focus is variables. In Python, a variable is a name that stores a piece of information, like a number or text. With variables, we can store quantities and prices, then use them in calculations.
Python also allows basic math operations:
- + for addition
- - for subtraction
- * for multiplication
- / for division
This is exactly what billing systems need to calculate totals, subtotals, and discounts.
Example:
1 2 3 4coffee_price = 3 coffee_quantity = 2 total_coffee = coffee_price * coffee_quantity print(total_coffee)
This will print 6, since two coffees at three dollars each cost six. Look at one item in the bill:
1Coffee (2): 3 x 2 = 6
There are three important parts:
- Quantity: how many coffees were ordered.
- Price: the cost of a single coffee.
- Subtotal: price × quantity.
Later, the program adds all subtotals to show the total bill. Without variables, you’d have to type numbers everywhere. With variables, you update the values in just one place, and the program updates the entire receipt.
- item = 5 creates a variable and stores a value.
- Use +, -, *, / for arithmetic.
- Variables can be used directly in print().
Try it step by step
Let’s begin with coffee.
1 2 3 4 5coffee_quantity = 2 coffee_price = 3 total_coffee = coffee_quantity * coffee_price print("Coffee (2): 3 x 2 =", total_coffee)
Here, total_coffee stores the subtotal. Try changing coffee_quantity to 4 and run the code—you’ll see the bill updates automatically without touching anything else. That’s the power of variables.
Learn with AI prompt
Now that you know variables and math, let’s expand to the full café bill. We’ll need to add a sandwich and a muffin, and compute the total.
Prompt:
Write a Python program that:
- Creates variables for quantity and price of coffee, sandwich, and muffin.
- Calculates each subtotal.
- Adds them all together for the final bill.
- Prints the receipt in this format:
1 2 3 4 5 6 7 8=== CAFE BILL === Cashier: Anna Coffee (2): 3 x 2 = 6 Sandwich (1): 5 x 1 = 5 Muffin (3): 2 x 3 = 6 Total bill: 6 + 5 + 6 = 17
Copy and paste this into the AI Copilot to generate the full code. Use the Copilot, paste your prompt, and let it build the code for you.
Brilliant! You’ve created a working café billing system. Now let’s push it further.
Task
Add a discount feature. Imagine a customer has a coupon for 20% off. The receipt should now show the discount and the adjusted total.
Like this:
1 2 3 4 5 6 7 8 9 10=== CAFE BILL === Cashier: Anna Coffee (2): 3 x 2 = 6 Sandwich (1): 5 x 1 = 5 Muffin (3): 2 x 3 = 6 Subtotal: 17 Discount (20%): 3.4 Final Bill: 13.6
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26coffee_quantity = 2 coffee_price = 3 total_coffee = coffee_quantity * coffee_price sandwich_quantity = 1 sandwich_price = 5 total_sandwich = sandwich_quantity * sandwich_price muffin_quantity = 3 muffin_price = 2 total_muffin = muffin_quantity * muffin_price subtotal = total_coffee + total_sandwich + total_muffin discount = subtotal * 0.20 final_bill = subtotal - discount print("=== CAFE BILL ===") print("Cashier: Anna") print() print("Coffee (2): 3 x 2 =", total_coffee) print("Sandwich (1): 5 x 1 =", total_sandwich) print("Muffin (3): 2 x 3 =", total_muffin) print() print("Subtotal:", subtotal) print("Discount (20%):", discount) print("Final Bill:", final_bill)
Perfect! Now your café billing system is more realistic and customer-friendly.
Frequently Asked Questions
Python variables store data values, created by assigning a value to a name using the = operator, like x = 5.
The four types of variables in Python are local, global, instance, and class variables, each with different scopes.
The == operator compares two values to check if they are equal and returns True if they are, otherwise False.
Three common types of variables in Python are integers (whole numbers), strings (text), and lists (ordered collections).
Python variables can be of types such as integers, strings, lists, tuples, dictionaries, booleans, floats, and sets.
Still have questions?Contact our support team