You've already seen a few variables in the course so far, but now you can go into more detail about what they are and how they work.
- Writing code typically involves the handling of information. If we're making a game, we need to store the user's score. If we're creating a banking application, we'll need to have their current balance and so forth. We've already seen a few variables in the course so far, but now we're going to go into more detail about what they are and how they work. A variable is simply a container for a value. When we run our programs, the computer gives us space in its memory where we can put data that we want to use as a reference for later. This data is called a variable. Much like how we label jars in our kitchen so we know where to go for our favorite cookies. We declare variables in Python by giving them a name and then setting their value. This is called assigning the value. And this assignment is done with the assignment operator, which is the equals sign. As we see here, age is the name of our variable, and because of the equals sign, we are assigning it the value of 36. That means that somewhere in the computer's memory, it's made space for our variable and stored our value in it. Later, when we ask the computer to print what's inside the age variable, it prints out 36, the value that we assigned previously. Although we're using an equals sign, try not to think of it the same way you did in high school algebra. In programming, the equals sign means I want to take the value on the right and store it in the variable on the left. It doesn't mean the two sides are equal. That's important to keep in mind. Okay, let's look at another example. This time, we've created a variable named email address, and we've assigned it to the following value. John.firstname.lastname@example.org. If we run this program, we can see that the values of our variables are printed out. Now even though we've created both of these variables by giving them a name, there's something different about the value that we assigned them. Can you spot the difference? Yes, it's that with the first value, we are only using numbers, and for the second value, we have double quotes with letters and symbols inside. This is because we want the variables to represent different data types. A data type allows us to put our variable in a particular category so that the computer knows how much space to give us in its memory. And it generally knows how we plan to use our variable later on in the program. In this example, the 36 is an integer, like what we're already familiar with from math class. In case you've forgotten, an integer is a whole number, no decimal places, and the letters and symbols inside of the double quotes is called a string because it's made up of a string of characters. In fact, you can check the type of any value or variable in Python by using the type function. Let's take a look. We'll print out the type of the age and then the email address variables. And look, the age is in INT, or short for integer. And the email address is STR, short for string. There are many other types of values available in Python, as well. We'll get a chance to explore more of them soon.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages