Python Quest

Track 1 · Lesson 1 of 11

Your first program

What a program is, how Python runs it line by line, and how print() shows results.

About 15 minFirst steps3 exercisesFree

You will be able to

  • Explain what a program is and what the Python interpreter does with it
  • Run a program and read its output
  • Use print() to display text and numbers
  • Predict the order in which lines run

A program is a list of instructions written in a language a computer can follow. Python is one of those languages. When you press Run, a piece of software called the Python interpreter reads your instructions from the top of the file to the bottom and carries out each one in turn.

Most programs do three things: they take in some data, do something with it, and show or save a result. In this lesson you'll do the last part — showing a result — with Python's print() function.

A complete Python program
print("Hello from Python Quest!")

Try it: Change the text between the quotes to your own message and run it again.

Here's what happens when you run it:

  1. Python reads the line print("Hello from Python Quest!").
  2. print is the name of a function — a ready-made instruction that does a job. Its job is to display whatever you put inside the parentheses.
  3. The text inside the quotes is called a string. The quotes tell Python "this is text, not an instruction".
  4. The text appears in the output panel.

The quotes themselves are not printed. They only mark where the text starts and ends. You can use double quotes "…" or single quotes '…', as long as both ends match.

Several lines run in order

A program usually has many lines. Python runs them one after another, top to bottom, and each print() starts a new line of output.

Three instructions, three lines of output
print("Step 1: open the file")
print("Step 2: read the numbers")
print("Step 3: show the total")

Try it: Swap the first and last lines, then run it. The output order follows the code order.

Predict the output

print("Good")
print("morning")
print("Riyadh")

Printing numbers

print() can display numbers too. Numbers are written without quotes. Python can also work out a calculation before printing it:

Text versus numbers
print(250)
print(250 + 75)
print("250 + 75")

Try it: Predict the three lines before you run it. Why does the last line not show 325?

The second line prints 325 because 250 + 75 is a calculation, and Python works it out first. The third line prints the characters 250 + 75 exactly as written, because the quotes make it a string.

You can give print() several values separated by commas. It prints them on one line with a space between each:

print("Total items:", 12)
print("Price per item:", 15, "SAR")

Guided practice · ●difficulty 1 of 5 · about 2 min

Say hello

Write a program that prints exactly this line:

Hello, Python!

Capital letters, the comma and the exclamation mark all count.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●difficulty 1 of 5 · about 4 min

A receipt header

A small shop wants its receipts to start with this header. Write a program that prints these three lines, in this order:

QUICK MART
Receipt
Items: 3

For the last line, practise printing the number 3 as a number, not inside quotes, by giving print() two values: the text "Items:" and the number 3. (Writing "Items: 3" as one string prints the same line, so the checker accepts that too.)

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Debugging · ●difficulty 1 of 5 · about 4 min

Fix the cafe bill

Cedar Cafe's bill program has two problems. When it runs, it prints:

Total: 12 + 18 SAR
Coffee: 12 SAR
Cake: 18 SAR

The bill should look like this, with the total worked out by Python:

Coffee: 12 SAR
Cake: 18 SAR
Total: 30 SAR

Fix the program so it prints the correct bill. Keep 12 + 18 as a calculation instead of typing 30 yourself.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Summary

  • A program is a sequence of instructions; the Python interpreter runs them from top to bottom.
  • print() displays values. Each call starts a new line of output.
  • Text (a string) goes inside quotes; numbers don't. A calculation is worked out before it is printed.
  • print() with several values separated by commas puts a space between them.