Chapter 1 · Foundations
Hello, Python
Step 1 of 15 · Concept
Your first Python program
By the end of this lesson you can write short Python programs and say what runs them.
A program is a list of instructions for a computer. In this course you write them in Python. You will:
- run a real Python program,
- see what happens when it runs,
- write programs of your own.
Step 2 of 15 · Demo
Run your first program
This program prints one line of text.
Press Run. The output's first line shows the command Run typed. Under it is what the program printed.
Step 3 of 15 · Concept
What Run did
Run typed one command for you, python3 main.py. You see it on the output's first line.
| part | what it is |
|---|---|
python3 | Python itself: the program that runs Python programs |
main.py | the file it reads: your program |
This is the real tool. Python programmers type the same command.
Step 4 of 15 · Quiz
Who reads your file?
Run typed python3 main.py. Which of these reads your file main.py?
Step 5 of 15 · Concept
A program is lines, run from the top
Python runs the lines of main.py from the top: one statement per line.
print("...")prints the text in its quotes,"..."or'...'.- The end of the line ends the statement: no
;needed. #starts a comment, a note for people. Python skips it.
NameError: name 'Print' is not defined. Did you mean: 'print'?
Step 6 of 15 · Quiz
Predict the output
Type exactly what this program prints.
print("Hello!")
# This line is a note for people.
print('I am Python.')
print("Bye.")
Step 7 of 15 · Demo
See it run
Run it and compare with your answer. The comment printed nothing.
Step 8 of 15 · Exercise
Your turn: two lines
Time to write code. Change one line, then add one.
Change the program so it prints exactly these two lines:
Hello, Python!
I am learning.
You need two statements: one for each line.
Change the text in the quotes to Hello, Python!. Then copy the whole line (on a desktop, Shift+Alt+↓ copies the line the cursor is on) and change the copy's text.
print("I am learning.")
Step 9 of 15 · Concept
print takes several values
print can take several values, with commas between them. It prints them on one line, one space apart.
| you write | it prints |
|---|---|
print("Name:", "Ada") | Name: Ada |
print("Age:", 15) | Age: 15: a number needs no quotes |
print() | an empty line |
Step 10 of 15 · Quiz
Where do the spaces come from?
This line prints Hi Ada. Where does the space between the words come from?
print("Hi", "Ada")
Step 11 of 15 · Try it
Try it: an empty line
Run it: two lines. Then add print() between them, so an empty line separates them:
Name: Ada
Age: 15
Put a new line 2 between the two: print(), with nothing in its brackets.
Step 12 of 15 · Exercise
Your turn: a name card
This time you start from a file with only a comment. Here is the shape to use:
print("...")
print()
print("...", ...)
Write a name card. The program must print exactly:
Ada Lovelace
Born: 1815
Three lines of output: three print statements, one per line, from the top.
The empty line is print(). The last line has two values: the text "Born:" and the number 1815.
print("Born:", 1815)
Step 13 of 15 · Check yourself
Check yourself: about me
No shape this time. You know it.
Write the whole program. It must print exactly:
About me
Name: Ada Lovelace
Lessons done: 1
One print for each line of the output, from the top. The empty line has its own print.
Step 14 of 15 · Challenge · optional
Challenge: a rocket
Optional.
Print this rocket, exactly:
/\
/ \
| |
/____\
Run it first. Python says unterminated string literal (detected at line 1); perhaps you escaped the end quote? Inside quotes, \ starts a special code: \" is a quote mark in the text, not its end.
To print one \, write two: "\\". So " /\\" prints /\. A single \ before a space prints a SyntaxWarning, so write \\ for every \.
print(" /____\\")
Step 15 of 15 · Recap
Recap
- A program is the file
main.py. Python runs its lines from the top, one statement per line. - Run types
python3 main.py:python3reads your file and does each line. print("...")prints text in"..."or'...'.print("Age:", 15)puts a space between;print()prints an empty line.#starts a comment. Capital letters count:Printis notprint.
Next: lesson 2 looks inside the computer: where your program lives while it runs.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
print("Hello, world!")
print("Hello!")
# This line is a note for people.
print('I am Python.')
print("Bye.")
print("Hello, world!")
print("Name:", "Ada")
print("Age:", 15)
# write three print lines here
# write your program here
print(" /\")