print#In IDLE you will use two things, and it helps to keep them straight:
.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.
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.
Shell experiments vanish when you close IDLE. Real programs live in files. Here is your first one:
print("Hello, world!")
print("My name is Sam.")
print(2 + 3)
hello.py. The .py
ending tells the computer this is a Python program.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.
print(Hello) gives a NameError,
because Python reads Hello as a name, not text. Text goes in
quotes: print("Hello").10 * 5.first.py that prints three lines: a
greeting, your name, and one arithmetic result.first.py saying what the program
does, save, and run it again to confirm the output is unchanged.print("Hi!") do?print(Hello) give an error?# at the start of a line do?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).
print shows output; text goes in quotes, numbers do not.# starts a comment, which Python skips..py, then run with F5 — save first, every time.NameError usually means you forgot quotes around text.Next lesson: Variables.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
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.
Five quick questions on this lesson. Take the Lesson 3 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.