Python Tutorial: Variables
Store information so your program can use it, change it, and reuse it.
Give your data a name
In Chapter 1 you printed text directly. But what if you want to use the same value in ten places? Or change it later?
A variable is a named container. Put a value in it, give it a name, and use that name anywhere you need the value.
Ten steps.
What you'll learn in this Python variables tutorial
This interactive Python tutorial has 10 hands-on exercises. Estimated time: 18 minutes.
- What even is a variable? — Remember in Chapter 1, you typed your name directly inside print()? Like `print("Alex")`?
- Create your own — Your turn! Create a variable called `name` and store YOUR name in it. Then print it.
- Variables hold numbers too — Variables are not just for text. They hold any kind of value — including numbers.
- Variables can change — Here is something that surprises some beginners: **a variable can change its value**. That is literally why they are cal…
- Use the current value to make a new one — Here's a pattern you will use *all the time*:
- Two variables in one line — Python lets you assign multiple variables at once — it's called tuple unpacking and it looks like this:
- The Python swap trick — In most languages, swapping two variables requires a third 'temporary' variable:
- Rules for naming variables — Variable names have rules. Break them and Python gets angry.
- del — goodbye variable — `del` removes a variable completely. After you delete it, trying to use it causes a `NameError` — Python has no idea wha…
- Build your profile — Time to put it all together. No starter code — you are writing this one from scratch.
Python Variables concepts covered
- Give your data a name