Let’s start with Variables!
Variables are memory locations in our code that store values. Each variable has a name (that we use to access it) and a type. A variable’s type tells us what kind of data it is currently holding.
You can think of variables like boxes that hold on to data. We want to track this data in the program we’re writing.
MarsAge = 7Above is a variable, called MarsAge, and its value is set to 7. That’s how old my dog Mars is!
The name of a variable will never change, but the value of a variable usually changes a lot!
When we create a variable, we include the name, and the value.
// name assignment operator value
playerHealth = 100
There are some naming rules about variables (you can’t name a variable something weird like “M@rs”) but that’s not super important to memorize.
Once a variable is created, we use the assignment operator (the equal sign) to store a value in the variable.
What can we store in variables?
Ultimately, a variable contains a single value. But when we are assigning that value, we can do so in a number of different ways:
x = 40
x = 50
x = x + 10
y = 40
z = x + yWRONG ways to use variables
If you wrote a new code file, and put the lines of code below in your main function, and compile this code… what happens? Did the compiler successfully create a new program?
80 = x
x + 10
x + y = 50
100 + 20What went wrong with the code we wrote above? You should see errors, and get some idea for what didn’t work, and why. Whenever we work with values, they need to be assigned to variables; we shouldn’t just have a value randomly hanging out in our code! Remember, our code needs to be translated into instructions and data that a CPU can use to DO things. With the last line of code above, we’re basically shouting “120!” at our CPU, but that isn’t an instruction to do anything.
We are also encountering a problem when we try to put two variables on the left-hand side of the assignment operator. That doesn’t work! You can only assign a value to a single variable at one time (meaning a single location in memory), so trying to add two variables together and then assign ‘50’ to that… doesn’t make sense.
Assignment vs Equality
When we use the equals sign, it is called the ASSIGNMENT operator, because we are ASSIGNING a value to a variable. We are not checking if a variable is EQUAL to a value.
We CAN check to see if a variable’s value is equal to some other value, but we won’t handle that just yet.
Types
Variables can hold onto many different types of data. Some of the different types are spelled out below:
int, or integers → whole numbers float → decimal numbers string → text data, like a sentence or name bool → a value that can only be one of two values: true or false
So far we have only looked at variables with the int type (integer numbers). But what if we want to store and manipulate other kinds of data in our program?
myFloat = 2.75
myString = 'Q'
isAwesome = trueThere are other data types we can use to represent specific kinds of values.
Floats can represent numbers with decimals (integers only represent whole numbers)
temperature = 0.6543
rotation = -47.8003The boolean type represents only two different values: TRUE or FALSE. A bool can only ever be these two values.
myFault = true
gymBadges = 7
hasAllPokemonBadges = (gymBadges >= 8)
powerLevel = 9001
isOver9000 = (powerLevel > 9000)The name of a variable will stay the same… but if you want to change its type, just assign a different value to it!
x = 5
x = "Now x is a string!"Typing in Python is DYNAMIC
Whaaaaaa, what does this mean? Why are you dropping this weird complex stuff on me?
Typing in Python is Dynamic, meaning variables can change what type they are on the fly! This is a dramatic difference from statically typed languages like C/C++, Java, and others, which are STATICALLY typed.
This also means it can be easier to get confused about how you’re using a particular variable and what you’re using it for. As a general rule of thumb, it makes sense not to assign different TYPES of values to a variable after it is first created.
Each type is a particular SIZE
Integer variables store positive/negative integer values (whole numbers).
But… how BIG is an integer? How large can its value be? Turns out, Integers are usually 32 bits in size (4 bytes). Because of the way integers are represented in binary, they have a min/max size: -2,147,483,648 to 2,147,483,647. But… WHY? Why are integers this size, and why do they have min/max values? Aren’t numbers… well, infinite?
Consider this thought experiment: suppose you want to run a program that loads all the digits of pi into memory, at once. How would you accomplish this? Well imagine that we could store each digit of pi in exactly 1 byte (8 bits) of storage space (this is absolutely NOT how this works in real life, but this is a hypothetical, so go with it). So we start storing off pi:
1st byte = 3
2nd byte = 1
3rd byte = 4
4th byte = 1
5th byte = 5
6th byte = 9
7th byte = 2 …
So, where are we storing these digits, physically, on our PC? Recall that our PC uses RAM for its working memory. So as our program runs, we will be filling up each byte of RAM with digits of pi. But pi is infinite, so we will eventually run out of space!
The whole point of this thought experiment is to bring home the point that numbers on PCs are not infinite. We use particular data types to represent particular ranges of numbers for particular programming purposes.
Integers are positive/negative whole numbers. Usually they are 32 bits in size, and their range is from -2,147,483,648 to 2,147,483,647.
When a CPU operates on data, it uses registers to do so. Registers are basically little storage spaces on the CPU where instructions and data are loaded. Because these registers are a particular size (usually 32 or 64 bits in size) it makes sense that data in our programs will usually align to multiples of this size as well:
//If we want to perform an operation between two pieces of data, the values
//of that data don't matter... as long as the representation of those pieces of data
//are the same size (number of bits) in binary, the CPU can operate on that data!
00100101001011110100000110111111 //This is 32 bits!
10100011011111000001110100100101 //This is also 32 bits!You can almost think of the CPU as a little factory/processing plant; everything flows smoothly when data is of a predetermined standard size, and can fit in exactly the right place in the CPU’s architecture.
Types Wrapup
A variable’s type determines two things: The SIZE of the data we are storing in that variable The way we INTERPRET that data
Keep in mind that even though an integer variable and a float variable are the same size (usually 32 bits), the binary representation of each type is VERY different, even if we interpret the numerical values as the same thing:
| Number and Type | Binary |
|---|---|
| 3, integer | 00000000000000000000000000000011 |
| 3.0, float | 01000000010000000000000000000000 |
Said another way: An integer only holds WHOLE numbers, so we arrange our binary representation a certain way to represent those numbers. A float holds decimal numbers, so we arrange our binary representation a certain way to represent THOSE numbers.