Python, properly · Chapter 1 of 42

Running Python

By the end of this chapter you can try Python in the REPL, write and run a script, control exactly what print shows, and read a traceback to fix your own mistakes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 4 tasks
  • Python
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

DabbaDrop delivers tiffins across Mumbai, and today it runs on WhatsApp messages and an Excel sheet that Farah, the founder, retypes every morning: the day's special, the dishes for the kitchen screen, the bills. She has hired you to replace the retyping with programs that do it the same way every time.

Before a program can read orders or add up bills, you need the ground under everything else: how Python runs your code, how a program shows something, and what to do when Python says no. Every chapter after this one is built on these three things.

The lesson

Python answers in the REPL

Python is a program that reads your code and runs it: the interpreter. Type python3 in the terminal and it starts in its interactive mode, the REPL (read, evaluate, print, loop). It shows a >>> prompt, runs each line as you press Enter, and shows the result:

>>> 2 + 3
5
>>> "Dal" + " Tadka"
'Dal Tadka'
>>> print("Kitchen open")
Kitchen open
>>> exit()
  • An expression on its own, like 2 + 3, shows its value. Text shows with its quotes ('Dal Tadka'): the REPL is showing you a value, not printing for a user.
  • print(...) shows text the way a user sees it, without quotes.
  • exit() (or Ctrl+D) leaves the REPL.

The REPL is for trying ideas. Anything you want to keep goes in a file.

A script runs from top to bottom

A script is a text file ending in .py. python3 welcome.py reads the whole file, then runs it one line after another, from the top. Each line here is one statement, one instruction:

print("Kitchen open")
print("Orders start at 11")
Kitchen open
Orders start at 11

A line starting with # is a comment: Python skips it. Comments are for people reading the code, like the task description at the top of each of your files.

print and its quotes

print is a function: a named piece of code you run by writing its name and brackets. Whatever you put in the brackets is what it shows.

Text, called a string in Python, goes inside quotes. Single '...' and double "..." quotes mean exactly the same; the only rule is that the string ends at the next quote of the same kind. That is why an apostrophe breaks a single-quoted string:

print('Today's special: Veg Thali')
  File "/workspace/chapters/01-running-python/quotes.py", line 1
    print('Today's special: Veg Thali')
                                     ^
SyntaxError: unterminated string literal (detected at line 1)

Python read 'Today' as a whole string, then could not make sense of the rest. Put the line in double quotes and the apostrophe is just a character.

Several values, sep and end

print takes any number of values, separated by commas. It puts one space between them, and a newline after the last one:

print("Veg Thali", 210)
print("Andheri", "Bandra", "Dadar", sep=", ")
print("Packing", end="... ")
print("ready")
print()
print("DabbaDrop", "since", 2026, sep="-")
Veg Thali 210
Andheri, Bandra, Dadar
Packing... ready

DabbaDrop-since-2026
  • Values do not have to be text: 210 is a number, and print shows it.
  • sep= (separator) is what goes between the values. It is a keyword argument: a setting you name in the call, written after the values.
  • end= is what goes after the last value. Changing it from a newline lets the next print continue on the same line.
  • print() with nothing in it prints an empty line.

When Python says no: tracebacks

Mistakes are normal; reading Python's report of them is a skill. Here the third line has a typo:

print("Kitchen open")
print("Today's special:", "Veg Thali")
pront("Riders ready")
print("Orders start at 11")
Kitchen open
Today's special: Veg Thali
Traceback (most recent call last):
  File "/workspace/chapters/01-running-python/kitchen_open.py", line 3, in <module>
    pront("Riders ready")
    ^^^^^
NameError: name 'pront' is not defined. Did you mean: 'print'?

Read a traceback from the bottom up:

  1. The last line names the error and explains it: a NameError is a name Python does not know. Python even guesses what you meant.
  2. Above it, Python shows the line of code, with ^^^^^ under the part it is complaining about.
  3. line 3 tells you where it is in your file.

Notice the first two lines did run: Python stops at the error, and nothing after it runs. Names are exact, capitals included: Print is as unknown as pront.

There are two errors Python finds before running anything, while reading the file: a SyntaxError (the code is not valid Python, like the broken quotes above) and an IndentationError (a line starts with spaces it should not have):

  File "/workspace/chapters/01-running-python/indent.py", line 2
    print("Riders ready")
IndentationError: unexpected indent

In Python, spaces at the start of a line mean something (chapter 6 uses them). For now, every line starts at the very left. Fix one error, run again, and the next one appears: that is the normal rhythm.

Your turn

Your four tasks are at the top, one file each: run each with python3 <file>.py in the terminal before you press Check. Task 1's apostrophe and task 3's end are the traps, and the interview question hands you a broken bill: fix it one traceback at a time.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    welcome.py: two lines for the website

    Farah wants the top of DabbaDrop's page printed by a program. Print exactly these two lines, and nothing else:

    Welcome to DabbaDrop!

    Today's special: Misal Pav

    Careful: the second line has an apostrophe in it.

  2. Task 2

    menu_line.py: three dishes, one line

    Bhavna wants today's three dishes on one line for the kitchen screen. With a single print call, print exactly:

    Dal Tadka | Roti | Masala Chaas

    Give print the three dish names as three separate values, and let print put the separator between them.

  3. Task 3

    kitchen.py: two prints, one line

    The kitchen shows progress on one line. With two print calls, the first printing Cooking and the second printing done!, the output must be exactly one line:

    Cooking... done!

    Change how the first print ends its line, instead of joining the words yourself.

  4. Task 4 · interview question

    Interview: fix_me.py, read the tracebacks

    fix_me.py prints a bill, but it has three mistakes and stops with an error. Run it, read each traceback, fix the line it points at, and run it again until it prints exactly:

    Order D0007 for Ravi

    2 x Roti

    Total: Rs 30

    Fix only what is broken: keep the three print lines and their text.

What you will learn

  • The REPL: Python answering one line at a time, for trying ideas.
  • Scripts: a .py file Python runs from the top line to the bottom.
  • print: showing text, several values at once, and the sep and end settings that control the spacing.
  • Tracebacks: reading Python's error report, from the bottom up.

More in First steps

Build "Running Python" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.