Start learning free

LESSON 4 OF 410 · WELCOME TO PYTHON

Variables in Python

Lesson

TL ; DR:

Variables are containers for storing values.

This is how you create a variable named car and initialize it with string "Toyota":

car = "Toyota"

This is how you create a variable named experience and initialize it with number 3:

experience = 3


Full lesson:

Variables are containers for storing values. A piece of information / data lives in memory and we use a variable to store and descriptively label that data.


Let's create our first variable:

name = "AlgoCademy"

creates a variable named name which stores the value "AlgoCademy".

A variable creation consists of:

  • An identifier, which is the name of the variable: name
  • An equal sign: =
  • A value to assign to that variable: AlgoCademy

When executing this code, Python will allocate some memory, then it will store the string "AlgoCademy" in that memory and finally will attach this name label to that memory location.

You can think of this memory location as a box. In that box, we put string "AlgoCademy". Then, we put the label name on this box.


Accessing values in variables:

Now we can use this label anywhere in our program to access the value in that box. We can print it for example:

# We create two variables:
name = "AlgoCademy"
age = 10

# We access the variables:
print(name)
print(age)

The output of this code is:

AlgoCademy
10

Assignment
Follow the Coding Tutorial and let's create some variables!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check