7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
{body}
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python Variables

Ready to start your career in Data? Find out more about BrainStation's Python Course

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 85.

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). Variables in Python must be typed in lower case characters and 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 open up a notebook and create our first few variables. Type the following code and run each cell one by one.

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_one and 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.

Now, the 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 operand_one with operand_two.

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 notebook when we ran our cells that created variables operand_one, operand_two, and result. In order to see the value stored in a given variable, just type the name of that variable and run the cell. If the variable had been created before, it will output the value stored in it. As you can see, typing the name of the variable result again and running the cell shows the value stored in it i.e. 30.

Set Password

You already have an account with BrainStation, but you still need to set up a password.