Variables

Declaring Variables

A variable is a name (series of characters) that we give to a piece of information that we want to store inside of a computer.

For example, say my favorite number was 6.

If I wanted to store this number in the computer's memory to be retrieved at some later point, I might give it a name. In this case I'm going to call my variable favorite_number. Why that name? Because it's describes the kind of information that I'm going to store to it in a way that will help me identify it and use it appropriately later on.

favorite_number = 6

If I run the code cell above the variable favorite_number will be assigned the value six and will represent that value until it is assigned a different value.

Imagine you're moving to a new house. You put all of your shoes in a box and you write "SHOES" on the outside of the box in black marker. Why? -To help you remember what is stored inside of the box. Even though the box has the word written on it, the word doesn't refer to the box, it refers to what's inside of it. The box is just a container to hold the shoes until you're ready to take them out later. Same principle here -a variable holds a value until we're ready to do something with it and its name should reflect what's stored inside.

The print() statement

If I ever want to see what has been stored to a specific variable I can use the print() statement. To do this I will write the name of the variable inside of the parentheses, this will cause the variable's internal value to be printed out for me to see. This value is displayed just below the code cell in the cell's output section.

print(favorite_number)
6

Assigning a new value to a variable

Say I was getting tired of having 6 as my favorite number and wanted to change my favorite number to be 10. Yeah, 10 sounds pretty good.

I could do this in exactly the same way that I created the variable in the first place, by simply assigning the value 10 to the variable. This will override the original value and reassign the inner value of the variable to be 10 instead of 6.

favorite_number = 10
print(favorite_number)
10

Variable Naming Rules

It may have sounded earlier like you could give a variable any name you like, but that's not 100% true. There are a few rules that we have to follow when picking variable names.

  1. Variable names can only contain "alpha-numeric" characters and underscores. That means any value from A-z, 0-9, or _.

  2. Variable names are case sensitive so capitalization matters. Letters can be either upper or lower case.

  3. Variable names can't start with numbers only letters or underscores.

Lets make some illegal variable names and look at some of the error messages that the python interpreter gives us.

# Start a variable with a number (don't do this)
9_lives = 9
File "<ipython-input-18-53fdd53b1c37>", line 3
9_lives = 9
^
SyntaxError: invalid token
# Use a non-alpha-numeric character (don't do this)
my-age = 30
File "<ipython-input-22-75331fe3ea77>", line 3
my-age = 30
^
SyntaxError: can't assign to operator
# Use a non-alpha-numeric character (don't do this)
cats&dogs = 5
File "<ipython-input-23-6b1eac2d6fec>", line 2
cats&dogs = 5
^
SyntaxError: can't assign to operator

You'll see these error messages talking about "operators." An operator is a symbol that has specific functionality in Python. For example, the dash - is used as the subtraction operator, so it's confusing if something that is used to denote subtraction finds its way into a variable name. If it was allowed in variable names, then how would Python know if we meant to do subtraction or just have a dash in a variable name?

Variable Naming Conventions and Best Practices

Here are some recommendations for how to name your variables so that they're readable and useful. You don't necessarily have to follow these suggestions, but if you pick a convention from the beginning it will make your life easier.

These aren't just suggestions that I have made up they are part of a larger body of Python Programming Style conventions known more commonly as "PEP8" but PEP8 has a lot more to say about it than I do.

  1. Typically variable names should be lower-case with underscores to separate words.

  2. Very rarely should variable names be single letters or abbreviations unless their meaning is well established.

  3. You'll read more code than you write, so invest time in naming things well. Future you will thank you when you come back to old code that you have written months later.