By the end of this lesson you will be able to
- make Python display text on the screen
- write a program of several lines and know the order in which they run
- display text and numbers together on one line
- recognise and fix the four mistakes beginners make most often
Every code box on this page is real Python that you can change. Press Run to run it, and the result appears underneath. Reset puts back the original code. Nothing needs to be installed.
The first time you run code, Python takes a few seconds to load in your browser (longer on mobile data). After that it is instant.
On a phone: brackets ( ) and quotes " are on the symbols keyboard (the ?123 key). If your keyboard types curly quotes “ ” instead of straight ones, this page will offer to fix them for you.
On a computer: press Ctrl + Enter (⌘ + Enter on a Mac) to run. Inside a code box the Tab key adds spaces; to move out of the box with the keyboard, press Esc and then Tab.
Hello, world
For more than fifty years, programmers learning a new language have started with a program that says “Hello, world”. Here it is in Python. Press Run.
That single line is a complete program. You told the computer to do something, and it did it. Now change the word world to your own name, for example print("Hello, James!"), and run it again.
What each part means
Let us look at that line piece by piece.
printthe instruction: display something(opening bracket"Hello, world!"the text to display, inside quotes)closing bracketprintis a built-in instruction that displays things. (Python calls instructions like this functions. You will meet that word again later in the course.)- The brackets
( )hold whatever you are giving toprint. - The quotes mark where the text begins and ends. Python calls a piece of text a string: a row of letters, numbers, spaces and symbols, one after another, like beads on a string. The quotes are not displayed; only the text between them is.
Early computers really did print their results on paper. The name stayed, even though today the result appears on a screen.
A program runs from top to bottom
A longer program is simply more lines. Python runs them one at a time, in order, starting at the top.
Add a fourth line at the end, such as print("Let us begin."), and run the program again. Your new line appears last, because Python always follows your lines in the order you wrote them.
To leave an empty line in the output, use print() with nothing between the brackets:
Text and numbers
Numbers do not need quotes. Compare these two lines:
The first line displays the text exactly as written, because it is inside quotes. The second line has no quotes, so Python works out the sum first and displays the answer, 8.
So the quotes make a big difference. Anything inside quotes is text, and Python shows it as it is. Anything without quotes is treated as part of the program: digits become a number Python can calculate with, and a word becomes a name that Python tries to look up. That is why print(Hello), without quotes, gives an error: Python has never heard of anything called Hello. You will use Python as a calculator properly in the next lesson.
Several items on one line
You can give print several items at once by separating them with commas. Python displays them on one line with a single space between the items.
Notice that you did not type any spaces between the items; print added them. The commas themselves are not displayed. In the last line, 3 * 45 is a calculation (the star * means “multiply”), so Python displays 135. Text and results can sit side by side on one line without any extra work.
Single or double quotes
Python accepts text inside either double quotes "…" or single quotes '…'. The main rule is that the two ends must match.
If your text contains an apostrophe (', as in It's), put double quotes around it. If your text contains double quotes, put single quotes around it.
Many Python programmers use double quotes unless there is a reason not to, and this course does the same.
Four common mistakes and how to fix them
Everyone who writes code makes mistakes, and professional programmers see error messages every day. What matters is being able to read them. Each box below contains a mistake. Run it to see what Python says, then fix it.
Start with the last line. It gives the kind of error (such as SyntaxError or NameError) followed by a short description. Above it, Python shows the line number and a copy of the line where it noticed the problem. The ^ marks point to the exact spot.
main.py is simply the name this page gives your program. You can ignore words like Traceback and <module> for now. Below every error, this page also explains in plain English what it means.
One more thing: before running any line, Python first reads the whole program to check that it is written correctly. If it finds a SyntaxError (a writing mistake) on any line, no line runs at all, not even the correct ones above it.
1. A capital letter in print
Python is case-sensitive: to Python, Print and print are two different names, and only the lowercase one exists. You will see a NameError, and Python even suggests the correct spelling. Phones often add a capital letter at the start of a line without asking, so watch for this.
2. A missing quote
Without the closing quote, Python cannot find where the text ends, so it reports a SyntaxError saying the string is “unterminated” (not finished). Add the missing " before the closing bracket.
3. A missing bracket
Every opening bracket needs a closing bracket. Python reports that the ( “was never closed”. Add the ) at the end.
4. Curly quotes
Look closely: these quotes are curly (“ ”) instead of straight (" "). Programs such as Word and Google Docs, chat apps, and many phone keyboards change straight quotes into curly ones automatically, and Python does not accept them. Python's message says invalid character '“' (U+201C); “U+201C” is just the computer's code number for the curly quote. This is the most common reason that code copied from a document or a chat message will not run.
Exercises
Now it is your turn. Type your code in each box and press Check my answer. The checker runs your program and compares its output with the expected output. It must match exactly: every letter, capital, space and punctuation mark. If you get stuck, Show solution becomes available after your first check. Try on your own first, because that is how the learning happens.
Exercise 1 · Your own greeting
Write a program that displays exactly this:
Expected output
Exercise 2 · A three-line card
Write a program that displays the three lines below, using the names exactly as shown. The first line is already written for you: add two more print() lines, one for each remaining line.
Expected output
Exercise 3 · Let Python do the sum
A notebook costs 45 rupees. Write a program that displays the total cost of 3 notebooks, exactly as below. Do not work out the answer yourself: write the calculation 3 * 45 in your code (without quotes) and let Python work it out. Use commas to separate the three items.
Expected output
Exercise 4 · Quotes and an empty line
Write a program that displays these three lines. The middle line is empty. Choose the right kind of quotes for each line.
Expected output
Exercise 5 · Find and fix the mistakes
This program should display the three lines below, but it contains three mistakes, one on each line. Fix them all.
Run the code after each fix. Each error message shows you one problem. Remember that Python checks the whole program for writing mistakes (such as missing quotes) before it runs any line, so the first error you see may not be on line 1.
Expected output
Quick check
Four questions to confirm the main ideas. Choose an answer to see the explanation.
1. What does print("5 + 3") display?
The quotes make it text, so Python displays it exactly as written, without the quotes. Without quotes, print(5 + 3) would display 8.
2. What does print("Rice", 60, "rupees") display?
The commas separate the items inside your code; they are not displayed. print puts one space between the items.
3. Which line runs without an error?
Single quotes are fine as long as both ends match. The first line has a capital P, the second has quotes that do not match, and the fourth has no quotes, so Python looks for something named Hi and cannot find it.
4. Python shows an error message several lines long. Which part should you read first?
The last line says what kind of error happened and why. Then use the line number to find where. Running the same code again without changing it gives the same error.
Summary
print()displays whatever you put between its brackets.print()with nothing inside makes an empty line.- Text goes inside matching quotes, either
"…"or'…'. The quotes themselves are not displayed. - Numbers need no quotes, and Python can calculate with them. A word without quotes is treated as a name, not as text.
- Commas let one
print()display several items, with a space between the items. - A program runs from the top line to the bottom line, in order. Writing mistakes (syntax errors) stop the whole program before any line runs.
- Python is case-sensitive, needs straight quotes, and every bracket must be closed.
- When there is an error, read the last line of the message first.
Found a mistake on this page, or something unclear? Report a problem and mention “Python Lesson 1.2”.
