Introduction to Python Variables
Oftentimes, you may want to store the outcome of a statement or expression in a way that it can be retrieved later if needed. For example, you may want to store the outcome of the statement
10 + 20 because you might need to reference it or use this information in later parts of your code. While this may be a little abstract currently, variables are an important part of programming and are powerful ways to handle data that might otherwise be difficult to work with and identify.
Variables are nothing but storage locations on your computer disk. Because you might be storing so much information in your code, it would be hard to memorize which information is stored at which memory location on your computer or within your code. Hence, by using variables we assign a meaningful name to those memory locations so that we can just retrieve them by using their name instead of typing the actual memory location address.
Let’s say, we want to store a student’s average grades as we need that information in our code multiple times. We can create a variable called
student_avg_grades that has a value
85 stored in it as follows:
student_avg_grades = 85
We’ve just created our first variable. The value on the left-hand side of the equal sign is the name of the variable. The value on the right-hand side of equal sign is the value stored in the variable of that name. You can imagine a memory location or container on your computer that now has the name
student_avg_grades and in that location, the value stored is
Python Variable Naming Conventions
Every language enforces a certain convention when it comes to naming your variables and other programming language features like functions or constants (more on these later). With variables in Python, variable names with multiple words should be separated by underscores
_. You can see we followed the naming conventions in our example variable above.
Let’s use our code editor and create our first few variables. Type the following code and run it.
operand_one = 10 operand_two = 20 result = operand_one + operand_two print(result)
Let’s see what we just accomplished. As shown above, we created a variable named
operand_one that stores the first value we want to add. We also stored the value
10 in that variable.
Similarly, we created another variable called
operand_two and stored a value of
20 in it. Next, instead of adding 10 and 20 directly, we performed an addition of the above two variables because those variables are instead storing the required values now.
We then assigned the outcome of the addition of
operand_two in a variable called
result. As you can see, creating a variable follows the same pattern – variable name on the left-hand side of the equal sign and the value this variable contains on the right-hand side of the equal sign.
result variable is storing the outcome of
10 + 20. Another way we can interpret this since we are using variables, is that
result is storing the outcome of adding
When you run a cell that is creating a variable for the first time, you won’t see any output. As you can see above, we didn’t see any output being shown in our code editor when we ran our commands that created variables
result. In order to see the value stored in a given variable, we just print the variable. If the variable had been created before, it will output the value stored in it.
Learn Python Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required