Your First Program: Print, and Running Code

DRAFT — awaiting Dante’s review. Not for publication yet.


What you’ll learn in this lesson

  • What the IDLE shell is for, and what a program file is for
  • How to print messages with print
  • How to write a comment with #
  • How to save a program file and run it in IDLE
  • The two most common first-day mistakes

Two ways to run Python

In IDLE you will use two things, and it helps to keep them straight:

  • The shell is for trying one line at a time. Type a line, press Enter, see the result at once. It is perfect for experiments.
  • A program file is for code you want to keep. You write several lines, save them in a file ending in .py, and run the whole file.

Start in the shell. Open IDLE and type:

print("Hello, world!")

Output:

Hello, world!

print is an instruction that means “show this on the screen.” The words inside the quotes are called a string — text, as opposed to a number. Strings always go in quotes. Without them, Python thinks you mean something else entirely, which is the first common mistake:

print(Hello)

The last line of the error reads (the lines above it just say where the error happened, and they look slightly different in IDLE than in other Python tools):

NameError: name 'Hello' is not defined

That error means: “you used the name Hello, but I have no value stored under that name.” Text needs quotes; names do not. You will see this difference again in the next lesson, on variables.


Comments: notes that do not run

A comment is a note to yourself (or to anyone reading your code) that Python ignores. It starts with a # sign:

# This line is a note. Python skips it completely.
print("Comments help future you remember why you wrote this.")

Output:

Comments help future you remember why you wrote this.

Only the print line runs. Use comments to explain why you wrote something, not what it does — the code itself already says what it does.


Your first program file

Shell experiments vanish when you close IDLE. Real programs live in files. Here is your first one:

  1. In IDLE, choose File → New File. A blank editor window opens.
  2. Type exactly this:
# My first program
print("Hello, world!")
print("My name is Sam.")
print(2 + 3)
  1. Choose File → Save, and save it as hello.py. The .py ending tells the computer this is a Python program.
  2. Choose Run → Run Module (or press F5). IDLE runs the whole file, and the output appears in the shell:
Hello, world!
My name is Sam.
5

Notice the third line: 2 + 3 has no quotes, because it is arithmetic, not text. Python works it out and prints 5. Quoted text is printed as-is; unquoted arithmetic is calculated first. That distinction — text in quotes, numbers bare — is one you will use in every program you ever write.

If IDLE asks you to save before running, that is normal: it always runs the saved version of your file. Save first, then run.


The two most common first-day mistakes

  • Forgetting the quotes: print(Hello) gives a NameError, because Python reads Hello as a name, not text. Text goes in quotes: print("Hello").
  • Running before saving: if you change a file and run it without saving, IDLE runs the old saved version, and your change seems to have no effect. Save, then run, every time.

Your First Program: Print, and Running Code

Python for Absolute Beginners · Lesson 3

What you’ll learn

  • What the IDLE shell is for, and what a program file is for
  • How to print messages with print
  • How to write a comment with #
  • How to save a program file and run it in IDLE
  • The two most common first-day mistakes
1 / 14

Two ways to run Python

  • The shell is for trying one line at a time: type a line, press Enter, see the result at once — perfect for experiments
  • A program file is for code you want to keep: write several lines, save them in a file ending in .py, and run the whole file
  • Start in the shell
2 / 14

In the shell: your first line

print("Hello, world!")

Output:
Hello, world!

print means “show this on the screen.” The words inside the quotes are called a string — text, as opposed to a number.

3 / 14

The first common mistake

print(Hello)

Error:
NameError: name 'Hello' is not defined

Without quotes, Python thinks Hello is a name, not text. The error means: you used the name Hello, but I have no value stored under that name. Text needs quotes; names do not.

4 / 14

Comments: notes that do not run

  • A comment is a note to yourself, or to anyone reading your code, that Python ignores
  • It starts with a # sign
  • Use comments to explain why you wrote something, not what it does — the code itself already says what it does
5 / 14

A comment in code

# This line is a note. Python skips it completely.
print("Comments help future you remember why you wrote this.")

Output:
Comments help future you remember why you wrote this.

Only the print line runs.

6 / 14

Your first program file

  • Shell experiments vanish when you close IDLE — real programs live in files
  • In IDLE, choose File → New File; a blank editor window opens
  • Type your program, then choose File → Save, and save it as hello.py — the .py ending tells the computer this is a Python program
  • Choose Run → Run Module, or press F5; IDLE runs the whole file, and the output appears in the shell
  • If IDLE asks you to save before running, that is normal: it always runs the saved version of your file
7 / 14

hello.py

# My first program
print("Hello, world!")
print("My name is Sam.")
print(2 + 3)

Output:
Hello, world!
My name is Sam.
5

Notice the third line: 2 + 3 has no quotes, because it is arithmetic, not text. Python works it out and prints 5.

8 / 14

Text in quotes, numbers bare

  • Quoted text is printed as-is; unquoted arithmetic is calculated first
  • That distinction — text in quotes, numbers bare — is one you will use in every program you ever write
9 / 14

The two most common first-day mistakes

  • Forgetting the quotes: print(Hello) gives a NameError, because Python reads Hello as a name, not text
  • Running before saving: if you change a file and run it without saving, IDLE runs the old saved version, and your change seems to have no effect
  • Save, then run, every time
10 / 14

Hands-on exercise

  • In the shell, print your own name and the result of 10 * 5
  • Create a file called first.py that prints three lines: a greeting, your name, and one arithmetic result
  • Run it with Run → Run Module (F5) and confirm all three lines appear
  • Add a comment at the top saying what the program does, save, and run it again to confirm the output is unchanged
11 / 14

Key takeaways

  • The shell is for experiments; files are for programs you keep
  • print shows output; text goes in quotes, numbers do not
  • # starts a comment, which Python skips
  • Save as .py, then run with F5 — save first, every time
  • A NameError usually means you forgot quotes around text
12 / 14

Take the quiz

Take the Lesson 3 quiz — 5 quick questions to check what you learned.

13 / 14

Next lesson

Variables: Storing Things

14 / 14

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 3 slides (PowerPoint, with narration)

Download the Lesson 3 slides (PDF)

Hands-on exercise

  1. In the shell, print your own name and the result of 10 * 5.
  2. Create a file called first.py that prints three lines: a greeting, your name, and one arithmetic result.
  3. Run it with Run → Run Module (F5) and confirm all three lines appear.
  4. Add a comment at the top of first.py saying what the program does, save, and run it again to confirm the output is unchanged.

Lesson 03: Your First Program: Print, and Running Code

The lesson’s first program: print a message, then print an arithmetic result. Change the message and run it again.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

Try this: Change the message to your own greeting and press Run again — the first output line changes, and 5 stays the same.

The code that runs



Output

Press Run to see the output here.

Practice file

Save this as lesson03-practice.py and run it with F5 in IDLE.

# Lesson 3: Your First Program
# WHAT TO DO: This program should print three lines: a greeting, your
# name, and one arithmetic result. The third line is unfinished.
# Fill in line 12: replace the placeholder with a print of an arithmetic
# result, for example print(10 * 5).
# HINT: Numbers do not need quotes. print(10 * 5) prints 50.
# Run with F5 in IDLE. You are done when the third line is a number.

# This program prints a greeting, a name, and an arithmetic result.
print("Hello!")
print("I am Coder.")
print("TODO - an arithmetic result goes here")  # <-- CHANGE THIS LINE

Key takeaways

  • The shell is for experiments; files are for programs you keep.
  • print shows output; text goes in quotes, numbers do not.
  • # starts a comment, which Python skips.
  • Save as .py, then run with F5 — save first, every time.
  • A NameError usually means you forgot quotes around text.

Next lesson: Variables.

Quick quiz

  1. What is the IDLE shell for, and what is a program file for?
  2. What does print("Hi!") do?
  3. Why does print(Hello) give an error?
  4. What does a # at the start of a line do?
  5. What file ending do Python program files use, and what key runs a file in IDLE?

Answer key: 1. The shell tries one line at a time; a program file keeps several lines to run together. 2. It displays Hi! on the screen. 3. Without quotes, Python reads Hello as a name with no stored value, giving a NameError. 4. It makes the line a comment, which Python ignores. 5. .py; F5 (Run → Run Module).