...

/

Built-In Functions and print()

Built-In Functions and print()

Learn Python’s built-in functions and the print() function by building your first digital receipt.

The project

You’ve probably gone shopping more times than you can count. You walk into the store, choose what you need, bring everything to the counter, and watch as the cashier scans each item. The prices appear on the screen, and finally, the receipt prints. That receipt isn’t random; it’s produced by a program that tells the system exactly what to show and in what order. This type of program is known as a point-of-sale (POS) system.

In this lesson, we’ll create our own small version of that system. No calculations yet, no storing of data, just printing the receipt text exactly as it should appear on the screen.

This is what the receipt looks like:

Press + to interact

You have been hired to create this POS system! But here’s the twist, for now, we’ll create this receipt exactly as shown. Our only tool will be Python’s built-in print() function.

The programming concept

The concept for today is built-in functions, special commands that Python already knows. You don’t have to invent them; you just use them.

The one we’ll start with is print(). Its job is simple: show text or numbers on the screen. You put the thing you want to display inside the parentheses, and Python will show it to you.

Example:

Press + to interact
Python 3.5
print("Hello World")

Click the “Run” button to check the output of this code.

Congratulations. You are officially a programmer. This is an age-old tradition in the programming community: start learning a new programming language by printing “Hello, World” on the screen!

The significance of the concept in the project

If you look closely at our receipt, it’s just lines of text and numbers. Without print(), nothing would appear on the screen. For this first version of our point-of-sale app, each line of the receipt will be produced by a separate print() statement.

Concept at a glance:

  • print() is a built-in function. It tells Python to show something on screen.

  • The thing you want to show must go inside parentheses, like this: print(...)

  • If it’s text, you must put it in quotes: "Hello!"

Learn to code the concept

Let’s try printing the first two lines of our receipt:

Press + to interact
Python 3.5
print("=== MART RECEIPT ===")
print("Cashier Name: Alex")

Run this and you’ll see:

=== MART RECEIPT ===
Cashier Name: Alex

To match the full receipt, we just keep adding more print() statements for each line.

Learn to code with AI

If you’re working with an AI assistant, you can simply tell it:

Prompt:

Write a Python program that prints this exact receipt using only print() statements:
=== MART RECEIPT ===
Cashier Name: Alex
Items Purchased:
Milk (1): 4
Bread (1): 2
Eggs (12): 0.5 x 12 = 6
Total bill: 4 + 2 + 6 = 12

The AI will give you working code immediately, ready to run.

Generate code with AI

Here is the Copilot, where you can write your prompt and get it to generate code for you.

Powered by AI
5 Prompts Remaining
Prompt AI WidgetOur tool is designed to help you to understand concepts and ask any follow up questions. Ask a question to get started.
AI Copilot

Test code with AI

Once the AI has generated the code, copy-paste it into the following code widget, run it, ask the “AI Mentor” for feedback, and improve the codeFirst yourself, then through the prompt above, or through the feedback of the AI Mentor if necessary.

Press + to interact
Python 3.5
# Write your code for receipt generation

Food for thought:

  • What happens if you try to print some text without quotes, e.g., print(Hello)?

  • What happens if you try to print some text without parentheses, e.g., print Hello?

  • If it’s text, you must put it in quotes: “Hello!”

Practice makes perfect

Wonderful. You’ve managed to execute a successful Python program that meets your client’s requirements. But the code was generated by AI, let’s see if you’ve learnt to simple enough concept. You have to use the concept you’ve learnt to solve a similar problem.

Problem

Print the following receipt exactly as shown, using only print() statements:

=== MART RECEIPT ===
Cashier Name: Sam
Items Purchased:
Pen (1): 20
Notebook (1): 45
Bag (1): 120
Total bill: 20 + 45 + 120 = 185

Code its solution

The final output should match the sample output shown above:

Press + to interact
Python 3.5
# Write your code for reciept generation

Perfect! Now your POS app has successfully launched and is ready to ship to customers.