Your First Program: print, Comments & input
Write your first lines of Python: show text and numbers with print, leave notes with comments, ask the user a question with input, and read your first error messages without panic.
A program is a list of instructions
A program is a list of instructions the computer follows exactly, one line at a time, from top to bottom. Think of a recipe: "boil water, add pasta, wait 10 minutes". The computer is a very fast but very literal cook: it does exactly what you wrote, even when that is not what you meant.
Each instruction is called a statement. Your first statement shows something on the screen:
print("Hello, world!")
print("I am learning Python.")print: show things on the screen
print is a function: a named action you use by writing its name followed by brackets. What you put in the brackets is what it shows. Text must be inside quotes, so Python knows it is text and not a command; numbers do not need quotes.
- Give print several things separated by commas, and it puts a space between them.
- Python can do arithmetic:
+ - * /, and**for powers. - Each print starts a new line. An empty
print()prints a blank line.
print("Total:", 12 + 30) # several things: separated by spaces
print("7 times 6 is", 7 * 6)
print() # a blank line
print("2 to the power 10 is", 2 ** 10)
print("one", "two", "three", sep=" | ") # choose the separator
print("no new line here...", end=" ")
print("...so this continues on the same line")Comments: notes for humans
Anything after a # on a line is a comment. Python ignores it completely. Comments are notes for people: they explain why the code does something. You can also put # in front of a line to switch it off temporarily while experimenting.
input: ask the user a question
input() pauses the program, shows a question, waits for the person to type an answer and press Enter, and gives back what they typed as text.
Because input always gives text, use int(...) for whole numbers and float(...) for decimals before doing maths. Without it, "27" + 1 is an error: you cannot add text and a number.
Try input() in the playground ↗: type the answers in the Program input box under the code, one line per question, then press Run.
Your first error messages
Everyone makes mistakes when typing code, all the time. Python tells you what went wrong in an error message. Read it from the bottom: the last line says what kind of error and why, and a line above says which line of your code it was on. Run each of these to see the message, then fix it:
# 1. Missing quote: SyntaxError (Python cannot even read the line)
# print("hello)
# 2. Misspelt name: NameError
# prnt("hello")
# 3. Text plus a number: TypeError
# print("Age: " + 30)
# Remove the # in front of one line at a time and run it.
# Then fix it. The fixes are:
print("hello")
print("Age: " + str(30)) # or: print("Age:", 30)Indentation matters
Spaces at the start of a line (indentation) mean something in Python: they show which lines belong together, for example inside an if or a loop (you will meet both soon). For now, the rule is simple: start ordinary lines at the very left. An unexpected space at the start gives IndentationError: unexpected indent.
Practice: check yourself
Write the code, then press ✔ Check my answer: hidden tests run your code and tell you exactly what is right. ▶ Run just shows the output. Stuck? Use a hint; the solution is there if you need it.
Write a program that prints exactly these two lines:
Hello, Python! I am learning to code.
A café sells coffee at $4.50. Print the cost of 3 coffees so the output is exactly:
3 coffees cost 13.5
Let Python do the multiplication: write 3 * 4.5 inside print, not 13.5.
Ask for the user's name with input("What is your name? ") and then print Welcome, NAME! (with their name). For the check, the user types Mia, so the last line must be Welcome, Mia!
Summary
A program runs top to bottom, one statement at a time. print shows things, # starts a comment, input asks a question and returns text, and error messages are read from the bottom up.
Key words
- program
- a list of instructions the computer follows in order
- statement
- one instruction, usually one line
- function
- a named action, used with brackets:
print(...) - string
- text, written in quotes:
"hello" - comment
- a note after
#that Python ignores - input
- a function that asks the user a question and returns their answer as text
- error message
- Python's report of what went wrong and where
Common mistakes
- Forgetting the quotes around text:
print(hello)looks for a variable called hello and gives NameError. - Mismatched quotes:
"hello'. Start and end with the same kind. - Capital letters:
Print("hi")fails; Python is case-sensitive, so it must beprint. - Doing maths with input directly:
input("Age? ") + 1. Convert first withint(...). - Curly "smart quotes" pasted from a word processor (“ ”). Python only understands straight quotes (" and ').
Quiz
1. What does print("3 + 4") show?
2. What does print("a", "b", "c") show?
3. What type of value does input() always return?
4. Python says NameError: name 'prnt' is not defined. What is wrong?
5. Which line is a comment?