Lockers with labels
Picture a row of lockers at college. Each locker has a name on the door. Inside is one thing.
A program stores data in the same way. Each place has a name. Inside is a value. The name lets you find the value again.
Key words
| Word | What it means | Example |
|---|---|---|
| Variable | A name that holds a value. The value can change. | score = 10 |
| Constant | A name that holds a value that never changes. | PI = 3.14 |
| Declare | Create a variable or constant and give it a first value. | age = 16 |
| Assignment | Putting a value into a variable. Python uses the = sign. | score = 0 |
| Data type | The kind of value, such as a whole number or text. | integer, string |
| Integer | A whole number. | 42 |
| Float | A number with a decimal point. | 2.5 |
| String | Text inside quote marks. | "Sam" |
| Boolean | True or False. | True |
The cells students fill in are left blank in this preview.
Try it: make a variable
name = "Sam"
age = 16
height = 1.75
is_student = True
print(name)
print(age)
print(height)
print(is_student)
# Change the values and press Run.
What is a variable?
A variable is a name that holds a value. The value can change while the program runs.
You make a variable by writing its name, then =, then the value. This is called declaring it. Python finds the data type from the value.
The variables in the code above
| Name | Value | Data type |
|---|---|---|
| name | "Sam" | String |
| age | 16 | Integer |
| height | 1.75 | Float |
| is_student | True | Boolean |
The cells students fill in are left blank in this preview.
The = sign is not "equal to"
data-callouts.warning.label
In Python, = means "put this value into this name". It copies the value from the right into the name on the left. Two equals signs, ==, ask a question. You meet that later.
Try it: change a variable
score = 0
print(score)
score = score + 10
print(score)
score = score + 10
print(score)
# The value changes each time.
# Add one more line that adds 5.
Rules for a good name
| Rule | Good | Not allowed or poor |
|---|---|---|
| Use letters, numbers and underscores. | total_score | total-score |
| Never start with a number. | player1 | 1player |
| No spaces. | first_name | first name |
| Capital letters make a different name. | Score and score are two names | Using both by mistake |
| Choose a name that says what it holds. | ticket_price | x |
The cells students fill in are left blank in this preview.
Which names work in Python?
Have a go
Which names work in Python?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.