Python Variables
What are Variables?
Well, let's break down the word "variable". It sounds like the words "vary" and "able" chained together, right? So, the word variable means some thing that can be changed, or varied.
You can think of a variable as a box and the name of the variable as box's label. You can't change the label, but you can change what's inside the box.
Why use Variables?
You might think that variables are super useless, but they are one of the most important things in all of programming. Why?
Well, imagine you were programming a 2d game. When making games, you need to know the position of the player. But you can't know the position. The player decides that. So, you put the position in a variable.
Using Variables
Let's say that we want to declare a variable called x
and set it to the value of 3. We type:
x = 3
If we now want a variable called count
and set it to 10, we type:
count = 10
If we now want our variable x
to be equal to 5, we type:
x = 5
A word a caution
In math class, our teacher told us that x = y is the same as y = x. That's not true in programming. For example if we type, it's perfectly fine:
x = 3
We're simply making a variable called x
and setting it to 3.
Now, see what happens when we type:
3 = x
We're telling Python to set the value of 3 to x
. Nonsense! So, Python gives us an error.