🎓 A1Learner
🔍
0 of 116 lessons completed

Python Variables

A variable is a labelled box for a value. The = sign stores a value in the variable.

Create a variable

Give a name. Then store text in it.

name = "Mia"
print(name)

Change the value

Store a new value in the same name.

score = 10
score = 15
print(score)

Change the type

The same name can hold text later.

value = 10
print(value)
value = "ten"
print(value)

Print a variable

You can print text and a variable together.

city = "Oslo"
print("City:", city)

Example

product = "Notebook"
price = 8
print(product)
print(price)
price = 6
print(product)
print(price)

Key points

  • Variable: a name that holds a value for later use
  • Use = to store a value in a variable
  • You create a variable the first time you assign it
  • Use the same name with = again to change the value
  • Dynamic typing: a variable can later hold a different type

In this example, product stores the text Notebook. The price starts at 8. The first two print lines show that first state. Then price changes to 6. The last two print lines show the new state.

Note

Many languages ask you to declare a variable first.

Python does not ask for that step.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

A variable holds 10. You then store 15 in the same variable. What value does it hold now?