…
…
Python Programming Fundamentals · Lesson 1 / 40 · Introduction to programming
PreviewIn this lesson you will
print, let Python do the math, and switch off a line with #You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
Ada runs a small café in Port Alder, the Corner Café, where every evening she adds up the day's orders by hand from a notebook and repeats the same sums from scratch. Throughout this course we will write an order program for her, one step at a time. Today's first step: make the computer print the café's sign and the first price lines.
By the end of this lesson, the program you write will print this on the screen:
OutputWelcome to the Corner Café!
Latte: 4.25
Latte + cookie: 6.25Three lines, but they hold the three ideas of this lesson: giving the computer instructions in order, printing strings and numbers on the screen, and letting the computer do the math. In this lesson you will:
print, and let Python calculate the total.# without deleting it.You do not need to install anything: the code boxes on this page use a Python that runs inside your browser. You write the code, press the Run button, and see the result right below it.
Ada does the same job every evening: she reads the orders one by one, writes down the amounts, adds them up, and copies the result into the notebook. The job is made of the same steps every day; only the numbers change. A job that repeats the same steps in the same order is the best kind of job to hand to a computer. To do that, you have to write the steps in a form the computer understands.
The whole set of instructions that tells a computer what to do, step by step, is called a program, and writing those instructions is called programming. Every app on your phone, every website, and every game is a program. In this course we will write instructions in a programming language called Python. Python runs code line by line: it starts at the top line, finishes it, moves to the next one, and keeps going until the last line. It never skips a line and never changes the order on its own.
The recipe analogy has one important limit. A cook can guess a missing step in a recipe; a computer cannot. A computer follows instructions literally: it does what you wrote, not what you meant. That is why a large part of programming is learning to describe a job clearly and precisely.
#### Example 1: The café sign
A three-line program for the screen at the café door:
print("Welcome to the Corner Café!") print("Today's special: latte") print("Enjoy!")
OutputWelcome to the Corner Café!
Today's special: latte
Enjoy!The lines in the output are in the same order as the lines in the code. This is not a coincidence: Python ran line 1 first, then line 2, and line 3 last.
Now it is your turn. First run the code below as it is. Then replace the word latte in line 2 with another item and run it again. Finally, swap line 1 and line 3 (cut one and paste it in place of the other): what changed in the output? Keep your prediction in mind before you run it.
Try it now· not graded; doesn't affect progress
When you swapped the lines, "Enjoy!" appeared at the top of the screen. The computer does not think "the welcome message always comes first"; it follows the order you gave. Order is part of a program, just like "first crack the egg, then whisk" in a recipe. Try the same idea on an everyday job: if we wanted to explain Ada's evening count to a computer, in which order should we give the steps?
Ada wants the computer to do her evening count. Put the steps of the program in the right order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
print: what the program writes on the screenWhatever a program does inside, you only see what it prints on the screen. What Ada wants in the end is lines she can read on the screen: the sign, the prices, the total. In Python, the way to print something is print(). Whatever you put inside the parentheses, print writes it on the screen, and every print starts its own new line.
We will put two kinds of values inside the parentheses. String is written in quotes: "Corner Café". The quotes tell Python "this is text, write what is inside exactly as it is". A number is written without quotes: 4.25. You can do math with numbers: print(4.25 + 2) first finds the sum and then prints 6.25. As soon as you put quotes around something, it becomes a string, and Python does not treat a sign inside it as math. Test this with a prediction:
What does this code print?
print("3 + 4")
Most of the time we want both a string and a number on one line: something like "Latte: 4.25". To do that, you separate the values inside the parentheses with a comma: print("Latte:", 4.25). Python prints the values separated by commas in order, and puts a space between them by itself. The comma itself does not appear on the screen; it only tells Python "one value ends here, a new one starts". So you do not need to add a space at the end of the string "Latte:".
#### Example 2: Price lines
We add the first prices under Ada's sign. On the last line, Python calculates the total:
print("Latte:", 4.25) print("Cookie:", 2.00) print("Latte + cookie:", 4.25 + 2.00)
OutputLatte: 4.25
Cookie: 2.0
Latte + cookie: 6.25Look at the last line carefully. The "Latte + cookie:" inside the quotes was printed as it is, because it is a string. The 4.25 + 2.00 outside the quotes was calculated, and 6.25 was printed in its place. Ada no longer has to add two prices in her head. Notice one more detail: 2.00 was printed as 2.0, because Python prints a number without extra trailing zeros. We will see how to control that in lesson 4. The table below sums up the relationship between the code and what you see on the screen:
| Code | What appears on the screen |
|---|---|
print("Latte") | Latte |
print("Latte:", 4.25) | Latte: 4.25 |
print(4.25 + 2) | 6.25 |
print("4.25 + 2") | 4.25 + 2 |
print() | (blank line) |
You do not have to memorize the table; apply the rule once more yourself. Below, match each line of code with what you would see on the screen. The list has one extra, wrong option:
Match each line of code with the output you would see on the screen.
For each row, pick its match from the list, then check.
print("Latte")print("Latte:", 4.25)print(4.25 + 2)print("4.25 + 2")print()There is one more misunderstanding: "If I write two print lines in a row, both end up on the same line." They do not. Every print ends the line when it finishes its work, and the next print starts on the line below. Run the four-line program below one step at a time with the Step button. At each step, look at which line is running and what was added to the output; pay special attention to the difference between the last two lines:
Run the code line by line: see which line runs next, the variable boxes and the output.
print("Corner Café") print("Welcome!") print(3 + 4) print("3 + 4")
As programs grow, you forget why a line is there. Ada's program will be dozens of lines long in a few weeks. Sometimes you also want to stop a line for a while without deleting it: for example, if there is no tea today, you can switch off the tea line and turn it back on tomorrow. Both problems have the same solution: a comment.
If you put a # sign at the start of a line, Python skips that line completely: it does not run it and does not print anything. A comment is a note left for the people who will read the code (often your future self). You can also write a # at the end of a line: the code to the left of the sign runs, and the note to the right is skipped.
#### Example 3: Menu prices and the switched-off tea line
The tea was not brewed today. Instead of deleting the tea line, Ada switches it off with #:
# Menu prices in $ print("Latte:", 4.25) print("Cookie:", 2.00) print("Water:", 0.75) # cheapest item # print("Tea:", 1.25)
OutputLatte: 4.25
Cookie: 2.0
Water: 0.75There are five lines in the code but only three lines in the output. The first line is a note from start to end, so it was skipped. On the water line, print ran, and the note # cheapest item on the right was skipped. The last line is actually a correct print; it did not run because of the # at its start. Now try it yourself: delete the # sign at the start of the tea line and run it, and the output will be one line longer. Then put a # at the start of the cookie line and run it again.
Try it now· not graded; doesn't affect progress
Do not think of a comment as an instruction to Python: writing # calculate the total calculates nothing. A comment is only for people; the code line below it does the work.
Now you know everything you need to write the program from the start of the lesson. Let's see the three ideas together: steps named with comments, lines that run in order, and math that Python does.
# 1. Sign print("Welcome to the Corner Café!") # 2. Price and total print("Latte:", 4.25) print("Latte + cookie:", 4.25 + 2.00)
OutputWelcome to the Corner Café!
Latte: 4.25
Latte + cookie: 6.25The same three ideas work outside the café too. Think of a screen at a bus stop that shows the route number and how many minutes until the buses arrive. The first bus comes in 5 minutes, and the next one 12 minutes after that:
print("Route 34") print("Arrives in:", 5, "min") print("Next:", 5 + 12, "min")
OutputRoute 34
Arrives in: 5 min
Next: 17 minThis time we gave print three values: a string, a number, and a string. Python printed all three in order and put a space between them. The "Route 34" on the first line is a single string: 34 here is not a number to be calculated, it is the name of the route.
The person building the display writes the last line as a single string. What appears on the screen?
print("Next: 5 + 12 min")
While adding a second line to the sign, Ada forgets the closing quote:
print("Corner Café") print("Welcome to the café)
Python does not run this code. It gives this message:
File "<kod>", line 2
print("Welcome to the café)
^
SyntaxError: unterminated string literal (detected at line 2)"The computer understands what I mean." It does not; it does what you wrote. If you start the word print with a capital letter while writing the sign, Python does not recognize it:
Print("Corner Café")OutputNameError: name 'Print' is not defined. Did you mean: 'print'?Python tells uppercase and lowercase apart: Print and print are two different names for it, and it knows nothing called Print. The last line of the message again says everything: the error type is NameError (an undefined name), the reason is that the name 'Print' is not defined, and there is even a suggestion: "Did you mean: 'print'?". The same error appears when you forget the quotes around a one-word string: if you write print(Latte), Python takes Latte to be a name. The correct way: write the function name in lowercase and put the string in quotes: print("Corner Café"). Read an error message as a clue, not as a failure; experienced programmers get errors every day and start from the last line in the same way.
print() writes the value in the parentheses on the screen: strings go in quotes, numbers go without quotes, a comma puts values side by side with a space, and Python does the math that is outside the quotes.# is a comment and Python skips it; in an error message, read the last line first (type and reason), then the line it points to.Next lesson: Instead of typing prices by hand on every line, we will put them into variables.
Exercises and quiz are for members
This lesson has 6 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
When you see a red message, your first reaction may be "I failed". But an error message is Python's way of helping you. Read the message from the bottom up. The last line tells you the type and the reason of the error: SyntaxError means a syntax error, and "unterminated string literal" means a string that was never ended. The line 2 above tells you which line to look at, and the ^ marker shows where the problem starts: a quote was opened but never closed. Notice one more thing: line 1 is correct, yet it was not printed either. Python never runs code that has a syntax error; it checks all the syntax first. The two errors you will see most often in the first lessons are these:
| Error type | What it means | Example |
|---|---|---|
SyntaxError | A syntax rule is broken: a quote or parenthesis is not closed, a comma is missing | print("Corner Café) |
NameError | Python does not know a name: a word written without quotes, or a misspelled name | print(Latte), Print("a") |
We will read error messages in detail in lesson 5. For now, build a habit: read the last line, go to the line it points to, and count the quotes and parentheses.
Which line of this three-line program raises an error?
print("Corner Café") print(Welcome guests) print("Latte:", 4.25)