Run your very first Python program

Write and run a program, meet print() and the REPL, and learn to read comments and common errors.

🌱 Module 0 6 min read Not started

Why this matters

Every programmer remembers the first time they made a computer do something. You are about to do that. By the end of this lesson you will have written a real Python program, run it two different ways, and understood β€” at a comfortable pace β€” what the words "running a program" actually mean. Nothing ahead in this course will make sense until you can reliably get a computer to say "hello" back to you, so let's start there.

Learning objectives

After this lesson you will be able to:

  1. Open a way to talk to Python (the REPL and a script).
  2. Write and run a program that prints text to the screen.
  3. Read and write comments.
  4. Explain what a string is, without needing to "learn it all" at once.
  5. Spot and fix the common "it printed nothing / it crashed" mistakes.

1. The two ways you run Python

You do not need to memorise everything now. Just know there are two ways people run Python code, and we will use both.

1a. The REPL β€” talk to Python line by line

Open a terminal (PowerShell on Windows, Terminal on macOS/Linux) and type:

powershell
python

You will see a prompt like >>>. This is the REPL β€” Read, Evaluate, Print, Loop. It runs one line of Python as soon as you press Enter, prints the answer, and waits. It is a calculator and a scratchpad.

Type this at the >>> prompt and press Enter:

python
2 + 3

Python prints 5. Now type:

python
print("hello")

Python prints hello. The print(...) function is how you ask Python to show something on the screen. You will use it constantly.

To leave the REPL, type exit() and press Enter.

Note: In the AskAiml Python playground you are already "inside" a REPL that runs in your browser. You can type 2 + 3 in the editor and press Run to see 5 β€” no install needed. That is a great place to practise everything in this lesson.

1b. A script β€” write a whole program to a file

A script is a text file containing Python. You name it with a .py ending. Create a file called hello.py and put this inside it:

python
print("hello from a script!")

Then run it from the terminal:

powershell
python hello.py

The difference: the REPL forgets everything when you leave. A script is a program you can save, run again, and eventually give to other people.


2. print() β€” showing text on the screen

print() is your first function. A function is a named action you can ask Python to perform. You "call" it by writing its name and parentheses, and you give it the thing to work on inside the parentheses.

python
print("Hello, world!")

The "Hello, world!" part is a string β€” a piece of text. Strings are surrounded by quotes so Python knows where the text begins and ends.

You can print more than one thing. Separate them with commas:

python
print("The answer is", 42)
print("a", "b", "c")

Try these in the playground now. Notice Python prints them with a space between each.


3. A tiny bit of vocabulary

Before we go further, these words will come up constantly. Do not memorise them β€” just understand the idea.

  • String β€” text, in quotes: "hello".
  • Function β€” a named action: print.
  • Calling a function β€” asking Python to run it: print(...).
  • Argument β€” the thing you hand to a function: "hello" above.
  • Running / executing β€” making Python actually do the work.

That is enough for now. The rest of the course will make these sharp.


4. Comments β€” notes for humans that Python ignores

A comment starts with a #. Python skips everything after the # on that line, so you can write notes to yourself or to teammates.

python
# This is a comment. Python ignores it.
print("hello")   # You can also put a comment after code.

You will see lots of # comments in the rest of this course (the little # True β€” bool subclasses int notes after expressions are comments). They are how the lessons tell you what a line of code means.


5. Common mistakes (and the fix)

Beginners run into these three almost immediately. Here is how to read them.

Mistake 1: "the quotes are wrong"

python
print(hello)        # ERROR: hello is treated as a variable name (not defined)
print("hello")      # OK: text inside quotes

If Python says NameError: name 'hello' is not defined, you forgot the quotes. The word without quotes is a name Python looks for; the quoted text is just text.

Mistake 2: "it printed SyntaxError: invalid syntax"

Usually a missing quote, a missing closing ), or a typo. Python is telling you the shape of the line is wrong. Read the error, look at the line it points to, and check quotes and parentheses.

Mistake 3: "nothing happened"

If you typed 2 + 3 in a script (not the REPL) and saw nothing, that is correct behaviour! In a script, 2 + 3 computes a result but Python does not show it to you. You have to ask, with print:

python
print(2 + 3)    # shows 5

The REPL shows results automatically; scripts only show what you print. Remembering this one idea will save you a lot of confusion.


Hands-on lab (30 minutes)

Try each of these in the playground. They are small on purpose β€” finish all of them before moving on.

  1. Print a greeting with your own name: print("Hi, Ada").
  2. Print the result of 5 * 6.
  3. Print three words on one line with commas: print("alpha", "beta", "gamma").
  4. Add a comment above a line of code that says why the line exists.
  5. Predict what print(10 - 4) will output, then run it.
  6. Challenge: print("I have", 3, "apples") β€” what do you think it prints?

Did you know you can run these right now? Open the Python playground, paste a snippet, press Run. Everything here runs in your browser.


Self-check

  1. What is the difference between the REPL and a script?
  2. What does print(...) do?
  3. What is a string?
  4. Why does 2 + 3 show nothing in a script, but print(2 + 3) shows 5?
  5. What does a line starting with # do?

References

Sign in to save your progress and earn badges.