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


What you'll learn in this lesson


Two ways to run Python

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

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:

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:
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


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.

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).


Key takeaways

Next lesson: Variables.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson03-practice.pyDownload the solution

# 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

Save the file as lesson03-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

Check yourself

Five quick questions on this lesson. Take the Lesson 3 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.