Skip to main content
Back to module
Python Fundamentals · Getting Started

Running Python

Reading19mLesson 1 of 3 in this module

Run your first Python program, print output and use Python as a calculator.

Why it matters

Python is one of the most widely used programming languages in the world. Data analysts use it to clean spreadsheets, engineers use it to process sensor readings, scientists use it to run simulations, and web developers use it to build servers. It is popular with beginners because the code reads almost like English, and popular with professionals because a huge library of ready-made tools sits behind it.

In this lesson you will run your first program, see what Python prints back, and learn the habits that make every later lesson easier.

Where Python code runs

A Python program is a plain text file, usually ending in .py, that the Python interpreter reads from top to bottom and executes one line at a time. There are three common ways to run it:

  • In the browser. The Do tab of this course runs Python for you, with nothing to install. It uses Pyodide, a version of Python that runs inside your web browser.
  • In a terminal. After installing Python from python.org, you save a file such as hello.py and run python3 hello.py (on Windows, often py hello.py).
  • In the interactive shell (REPL). Typing python3 on its own opens a prompt (>>>) where each line runs as soon as you press Enter. It is handy for quick experiments.

All three run the same language. What you learn here works everywhere.

Your first program

print("Hello, world!")

print is a function: a named piece of code that does a job. You call it by writing its name followed by parentheses, and you put the thing you want it to work on, the argument, inside. Here the argument is the text "Hello, world!". Text in quotes is called a string. Single quotes ('Hello') and double quotes ("Hello") both work; just use the same kind at both ends.

print can take several arguments separated by commas. It puts a space between them:

print("Lessons in this course:", 15)
print("Modules:", 5, "Lessons per module:", 3)

Output:

Lessons in this course: 15
Modules: 5 Lessons per module: 3

Python as a calculator

Python understands arithmetic. Each line below is an expression, which Python works out to a single value:

print(7 + 3)      # 10
print(7 - 3)      # 4
print(7 * 3)      # 21
print(7 / 2)      # 3.5   (true division always gives a decimal)
print(7 // 2)     # 3     (floor division drops the remainder)
print(7 % 2)      # 1     (remainder, called "modulo")
print(2 ** 10)    # 1024  (power)

Python follows the usual order of operations, so 2 + 3 * 4 is 14, and (2 + 3) * 4 is 20. When in doubt, add parentheses: they make your intent clear to the next reader too.

Everything after a # on a line is a comment. Python ignores it. Comments are notes for humans, and they are a good place to explain why a line exists.

Giving a value a name

You will often want to keep a result and use it again. The = sign stores a value under a name:

seconds_per_day = 24 * 60 * 60
print("Seconds in a day:", seconds_per_day)
print("Seconds in a week:", seconds_per_day * 7)

The name on the left is a variable. The next lesson covers variables in detail; for now, read = as "store the value on the right under the name on the left".

Order matters

Python runs your file top to bottom. A line can only use names that were created on an earlier line:

print(total)   # NameError: name 'total' is not defined
total = 10

Swap the two lines and it works. When something goes wrong, Python stops and prints an error message that names the problem and the line number. Lesson 6 of this course is all about reading these messages; for now, look at the last line of the error first, because that is where Python says what went wrong.

Common mistakes

  • Missing quotes. print(Hello) makes Python look for a variable called Hello. Text needs quotes: print("Hello").
  • Mismatched quotes or brackets. print("Hello') or print("Hello" is a SyntaxError. Every opening quote and bracket needs a matching closing one.
  • Capital letters. Python is case-sensitive. Print and print are different names, and only print exists.
  • Expecting / to give a whole number. 10 / 2 is 5.0, not 5. Use // when you want whole-number division.
  • Stray indentation. A line that starts with spaces for no reason is an IndentationError. Start top-level lines at the left edge.

Quick recap

  • A Python program is text that the interpreter runs from top to bottom.
  • print(...) shows values; separate several values with commas.
  • Strings are text in quotes; numbers are written without quotes.
  • Python does arithmetic with + - * / // % ** and follows the order of operations.
  • name = value stores a value so you can use it later.
  • When an error appears, read its last line first.

Try it

Open the Do tab. You will store a greeting, work out how many minutes are in a week, and print both.

Also in this lesson: