Variables
What are Variables?
When there is something that we don’t know, we can represent that with a variable. Let’s say that I have two friends. I know one of them is 14 years old, but I don’t know how old the other friend is. I can write their combined age as 14 + x, where x represents the age of the friend that I don’t know. x could equal 14, or 15, or even 51. I just don’t know. Variables are important because you don’t have to know. Many times when you are writing a program you don’t know a value. For instance, Google doesn’t know what you are going to search for, but they have to tell the computer what to do with that search once you have made it. To do that you can represent it with a variable.
What are Variables?
When there is something that we don’t know, we can represent that with a variable. Let’s say that I have two friends. I know one of them is 14 years old, but I don’t know how old the other friend is. I can write their combined age as 14 + x, where x represents the age of the friend that I don’t know. x could equal 14, or 15, or even 51. I just don’t know. Variables are important because you don’t have to know. Many times when you are writing a program you don’t know a value. For instance, Google doesn’t know what you are going to search for, but they have to tell the computer what to do with that search once you have made it. To do that you can represent it with a variable.
How do we define variables?
How can we create variables of our very own? Well there is a very important syntax so that the computer understands exactly what you want it to do.
How can we create variables of our very own? Well there is a very important syntax so that the computer understands exactly what you want it to do.
The general syntax is to say variablename = value. In this case, the variable name is x, but it could have been ?, or y, or any other name that I wanted. More on this in a minute. The value in the case below is 5, but I could have set it to any number, or even other types of data. Try this out on your own to get a sense for yourself.*
… the computer remembers. You can use this method to define as many variables as you want, but you have to make sure that you use different variables every time. Otherwise, the values will override. For example: in the following statements:
x = 5
x = 8
x would first equal 5, but then it would equal 8. A variable can only correspond to one value.
*From the main page of repl.it, you can find a way to test your code by going to languages and then selecting python3. This will give you a python editor where you can practice writing and running your very own code, use it to follow along with this presentation. The page on the left will save code, but you can use the page on the right to enter code directly into an editor. For most of the course we will just use this page.
x = 5
x = 8
x would first equal 5, but then it would equal 8. A variable can only correspond to one value.
*From the main page of repl.it, you can find a way to test your code by going to languages and then selecting python3. This will give you a python editor where you can practice writing and running your very own code, use it to follow along with this presentation. The page on the left will save code, but you can use the page on the right to enter code directly into an editor. For most of the course we will just use this page.
What are some naming conventions?
One of the problems with this is that it can become confusing if you define a lot of variables. If you are just using one variable, using a simple name like x is fine, but in a complex program it becomes hard to tell what variable is what. Even more importantly, if someone else is reading your code, they won’t have the slightest idea what each of your variables mean.*
Good programming practice is to define a variable by stating exactly what it means. Look at the following code, and decide for yourself which is easier to understand:
One of the problems with this is that it can become confusing if you define a lot of variables. If you are just using one variable, using a simple name like x is fine, but in a complex program it becomes hard to tell what variable is what. Even more importantly, if someone else is reading your code, they won’t have the slightest idea what each of your variables mean.*
Good programming practice is to define a variable by stating exactly what it means. Look at the following code, and decide for yourself which is easier to understand:
Additionally, sometimes you might even use multiple words to define a variable. For example, say for whatever reason you needed to define the length * width as a new variable (length width).
The computer doesn’t understand what this means, and so returns an error. This is because the computer doesn’t understand the space between length and width in the variable name. To fix this, python programmers use what is called (pun intended) snake_case. Instead of defining variable length width, we can define a variable length_width.
Programmer beware, the computer also won’t understand if a variable begins with a number. Any other character is normally okay, but be careful using numbers in a variable name. In the world of professional programming, code is very rarely written all by one person. In fact, hundreds of people, from different countries and speaking different languages can all collaborate on the same code, so it is important to give your variables descriptive names so everyone can understand the code better.
Mutating variables
You can also change variables. For instance, say I decide that the length is no longer 6. I can redefine length to be a different value, say I want my new length to be 7.
Programmer beware, the computer also won’t understand if a variable begins with a number. Any other character is normally okay, but be careful using numbers in a variable name. In the world of professional programming, code is very rarely written all by one person. In fact, hundreds of people, from different countries and speaking different languages can all collaborate on the same code, so it is important to give your variables descriptive names so everyone can understand the code better.
Mutating variables
You can also change variables. For instance, say I decide that the length is no longer 6. I can redefine length to be a different value, say I want my new length to be 7.
Now the computer knows that length = 7. But remember that any variable that you have already defined with length is not changed. For example, volume still is equal to 80, even though know length * width * depth (7 * 2 * 8) is equal to 112. Remember if you change one variable, you may have to change other variables that rely on this variable too.