Level Two: Variables in Python

At this level, you will learn about Variables in Python and even create one! Let’s get started!

A variable is a simple data type that has a name and a value. Variables are used to store information.

Let me explain with an example. Imagine a box with some toys inside. In our case, the box is a variable. The name of the variable is “box”.

The box has a label – toys. This is the value of the variable.

So, the variable (box) stores toys.

This is how our variable looks in Python:

box = "toys"

Let’s break it down:

First, we named our variable – “box”.

Then we set the equal sign =

Finally, we assigned the value “toys” to our variable.

The value of a variable is always enclosed in quotes, as in our example. Otherwise, the variable will not work.

Now that we understand what a variable is and how to write it, let’s create our first variable and print it to the screen!

How to Create and Print Variables in Python

Let’s start by writing our variable from the example above into the compiler and running it:

box = "toys"
Code
Run
#write code here

Wait, nothing happened? Exactly! That’s because a variable by itself is just a container for storing data, not something that shows up on your screen.

To print the variable to the screen, we will have to use the print function, which you are already familiar with.

Here’s how we’ll do it. Write the following code into the compiler and run it:

box = "toys"
print(box)
Code
Run
#write code here

If you do everything correctly, the code will return the value of the variable, which is toys.

Now let’s break down the code line by line:

First, we declared a variable and gave it a name – box.

Then we assigned a value to the variable – “toys”.

Then on the second line, we wrote the print function, and passed the name of our variable to this function, placing it in the function parentheses.

Every time we create a variable and pass its name to the print function, this function will print the value of the variable to the screen, as in our example.

By the way, if the name of your variable consists of more than one word, then you need to connect these words with an underscore. For example: red_planet. This naming convention improves your code readability and is widely accepted.

Well, now you know how programmers create and display variables in Python.

It’s not that hard, is it?

Let’s practice a little more. Here is the code that is missing something. You need to fix it so it creates a variable and displays its value.

= " "
()
Code
Run
#write code here

By now, you should have enough knowledge and skills to complete this easy task.

Once you’ve nailed it, go ahead and practice some more. You can change the name and value of the variable. And print more variables.

The more you practice, the faster you learn to code.

Level Two Quiz

You have to pass the quiz to complete Level Two!

Level Two Quiz

1 / 4

We need variables to…

2 / 4

If a variable name consists of two or more words, you should connect them using:

3 / 4

To display a variable on a screen we need to…

4 / 4

Which is the correct way of creating a variable?

Your score is

The average score is 88%

0%

Wrapping Up Level Two

In Level Two, you accomplished the following:

  1. Learned what variables are
  2. Learned how to create variables in Python
  3. You learned the naming convention for variables
  4. Learned how to print the values of variables

Great job! Let’s move on to Level Three: Numbers in Python. I promise, no boring math! Let’s get started!


Python Online is an online compiler where you can write, run, and check your code without installing any software.