Course 1.2Lesson 1 of 5
A variable is a name that holds a value. The = sign stores the value on its right in the name on its left, so age = 12 is correct and 12 = age is an error. We use the name, without quotation marks, to get the value back. A variable can be given a new value, and then the old value is forgotten. A variable must be made before it is used, or Python shows a NameError.
Try the code and the simulations beside the lesson.
After this lesson you can
print() and in sums,A game must remember your score. A shop must remember the price of a pen. A weather program must remember today's temperature. In each case, the program keeps a value and gives it a name, so that it can find the value again later.
Think of a box with a label. You put something in the box. Later, you find it by reading the label. In Python, the box is a variable, the label is its name, and the thing inside is its value.
We make a variable with the equals sign =. The value on the right goes into the name on the left.
A line such as age = 12 is called an assignment. Python does it in two steps.
=.In mathematics, = says that two things are equal. In Python it is an instruction: "store this value in this name". So we say it as "age gets 12", or "age is set to 12". This is why 12 = age is an error. You cannot put a name into the number 12.
There is one thing that confuses almost every beginner. Look at these three lines.
Now change the first line to city = "Delhi" and run it again. Which lines changed, and which did not?
Python reads a program from the top to the bottom. When it meets a name, it looks for a variable with that name. If there is none yet, it stops with an error.
Now fix it. Move the line score = 10 to the top, above the print(), and run it again. Python must meet score = 10 before it meets print(score).
Look at this program. What do you think it shows? Guess first, then run it.
Many beginners guess that b becomes 11, because b = a + 1 and a is now 10. But b shows 5. At the moment of b = a + 1, the value of a was 4. Python worked out 4 + 1 = 5 and stored 5 in b. After that, b has no link to a. Changing a later does not change b.
A variable is a box with a value in it, and not a formula that stays alive.
The line b = a + 1 works out a number once, when it runs, and stores it in b. It is not a rule that keeps b one more than a.
A good programmer can follow a program without running it. A table helps. Each row is one line of the program, and each column is one variable. Here is the table for the last program.
| Line | What happens | a | b |
|---|---|---|---|
a = 4 | 4 goes into a | 4 | not made yet |
b = a + 1 | 4 + 1 = 5 goes into b | 4 | 5 |
a = 10 | 10 goes into a | 10 | 5 |
print(...) | shows both | 10 | 5 |
This kind of table is called a trace table. We use it again in the next lesson.
print("age") shows the word age. Write print(age) to show the value.NameError. Make the variable first.12 = age is an error. The name goes on the left.b = a + 1, a new value of a does not change b.score and Score are different names. We study this in the lesson on names.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Work in this order. Do not look at the answers before you have tried.
=, and the value on the right goes into the name on the left.NameError.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
x = 5 and then x = 8, what is x?10 = score correct? Why?print("name") show, if name = "Asha"?p = 3, q = p * 2 and p = 100, what are p and q?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.