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"
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)
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.
= " "
()
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!
Wrapping Up Level Two
In Level Two, you accomplished the following:
- Learned what variables are
- Learned how to create variables in Python
- You learned the naming convention for variables
- 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.