Variables – Intro to Computer Science

So our answer to the last quiz would’ve been a lot easier to read and a lot more useful if we used names to keep track of values, instead of writing out those big numbers, especially numbers as big as the speed of light. Python provides a way to do it. It’s called the Variable. We can use the variable to create a name and use that name to refer to a variable. So the way to introduce a variable is using an assignment statement. And an assignment statement looks like this. We have a name, followed by an equal symbol, followed by an expression. After the assignment statement, the name that was on the left side refers to the value that the expression has. The name can be any sequence of letters and numbers, as well as underscores, as long as it starts with a letter or an underscore. So here’s an example, we could create the name, speed_of_light, and we can assign to it the value of the speed of light in meters per second. So after that assignment, the name speed_of_light refers to that value. One way to think of that is to have an arrow, so we can have the name speed_of_light, and that’s a name which refers to a value. And the value it refers to is this long value, which is the speed of light in meters per second. So once we’ve done the assignment, we can use the name and the value of the name is the value that it refers to. In this case it’s the speed of light in meters per second. So let’s try that in the Python interpreter. So here we’ve introduced the variable speed of light and we’ve assigned to it the value 299,792,458, the speed of light in meters per second. And now we’ve got that, assign it a variable. Instead of having to type out that whole number, we can use it directly. When we print out the speed of light, it will be the value that that name refers to. So we’ll see, instead of seeing speed of light, we’ll see the 299 million value here. We can use in expressions as well. So if we want to convert it into centimeters instead of meters, we can multiply by 100 and now we see the result is the speed of light in centimeters per second. So let’s define another variable. This one will define billionth, which means 1 divided by 1,000,000,000. That’s hard enough to remember how many zeros to type, so it’s nice to have that in a variable. And now we can define nanostick, which is the length of Grace Hopper’s nanostick, as the speed of light times a billionth. That would be the length of the nanostick in meters. If we want it in centimeters, we can multiply by 100. And so now we’ve defined a variable, nanostick, which is the length light travels in a nanosecond, in centimeters, and we can print that out. So, variables are really useful. They’re going to make our programs a lot easier to understand. They also mean that we can use the same expression, changing the values of the variables to compute different things. So, now it’s time for a quiz to see if you understand variables.

Leave a Reply

Your email address will not be published. Required fields are marked *