• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells

Loading Runtime

The first concept to learn with any programming language is how to make a variable and how to store values inside of that variable.

A variable is like a labeled storage box where we can put things for later use.

Labeled Storage Boxes

Whenever we create a variable we put the name of the variable first, then a single equals sign, and then the value that we want to store to that variable on the right.

declare a variable in python

The technical name for this is called "declaring a variable" or is also known as "variable assignment".

If I were to execute this line of code it would be as if I was taking the number 30 and storing it inside of the variable my_age.

Variables are like boxes that we can store values inside of

Now if I use the variable my_age anywhere in my code it will represent the number 30 –because that's the value that was assigned to it.

Code Cell

Let's try it in a code cell.

I'll type out my_age, then an equals sign =, and the number 30.

Now I'll run the code cell by clicking on the play button on the left-hand side. Running the code cell is what actually executes the code and causes the variable to be created inside of my notebook's memory.

You Try: Run the code cell below by clicking on the green play button on the left-hand side.

At first glance it might look like nothing has happened, but if the code cell has executed successfully you should see a number appear on the left-hand side in place of the play button.

This number is called the "execution count". Every time I run this code cell that number will change.

You Try: Run the code cell above a few more times and watch the execution count change each time.

The changing of the execution count indicates to us that indeed the code cell has been run and that behind the scenes the number 30 has been successfully stored to the variable my_age

Another thing you can do if you want to be absolutely certain that this variable has been assigned the correct value is to write the variable my_age on a lower line within the same code cell. When the notebook sees a variable name and nothing else as the last piece of code in a code cell it will try to automatically display the value assigned to that variable in the output section of the code cell.

You Try: Run the code cell below to see the variable's value be automatically displayed.

30

Now it's your turn. Use the exercises found below this video to practice creating some variables of your own. We'll ask you to name the variables something specific so that we can grade your code and give you points for completing the exercises.