Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a variable in Python

Understanding Variables

First and foremost, let's dig into the world of variables. In simple terms, a variable can be thought of as a container that holds a value. It's like a box that we put things in, which we can label and use later. This box is stored in the memory of your computer, and we can put values into it, change those values, or retrieve them whenever we need them.

In Python, creating a variable is as simple as giving it a name and assigning it a value. This is done with the equals sign (=).

For example:

x = 5

In this example, x is the variable, and we have assigned it the value 5. Now, whenever we refer to x in our code, Python will understand that we're talking about the number 5.

Naming Variables

Python has some rules and conventions when it comes to naming variables. They must start with a letter or an underscore (_), but the rest of the name can contain letters, numbers, and underscores. They are also case-sensitive, which means Variable, variable, and VARIABLE are all different variables.

Here's are some examples of valid variable names:

myVariable = 10
variable1 = 20
_this_is_a_variable = 30

Types of Variables

In Python, variables can hold different types of values. The type of a variable determines what operations can be performed on it. Here are the three most common types:


Integers are whole numbers. They can be positive or negative. For example, 5, -3, and 0 are all integers.

my_integer = 10


Floats, or floating point numbers, are numbers with a decimal point. They can also be positive or negative. For example, 5.0, -3.2, and 0.0 are all floats.

my_float = 10.0


Strings are sequences of characters. They are defined by enclosing characters in quotes. You can use either single quotes (') or double quotes ("), as long as you start and end the string with the same one.

my_string = "Hello, World!"

Changing Variable Values

One of the great things about variables is that their values can be changed. You can assign a new value to a variable at any time, and Python will forget the old value.

x = 10
print(x)  # This will print: 10

x = 20
print(x)  # This will print: 20

In this example, x initially holds the value 10, but we then assign it the new value 20. When we print x again, it outputs 20, not 10.


In the realm of programming, variables are like the supporting actors that play a vital role but seldom steal the spotlight. They are the silent workhorses that make our code dynamic and flexible. Without them, our programs would be static and rigid, incapable of handling the ever-changing inputs and conditions they encounter.

Remember our analogy of variables as containers? Well, just as a chef uses various containers to hold different ingredients while cooking a meal, a programmer uses variables to hold values while crafting a program. And just as a master chef knows how to use different containers effectively to bring a delicious dish to life, a skilled programmer knows how to leverage variables to bring a program to life.

So, while variables might seem like a trivial concept at first, understanding how to create and use them is a foundational skill in programming. It's the first step in turning rigid code into dynamic programs that can interact with users, handle different conditions, and even learn from data.

In Python, creating and working with variables is a breeze, thanks to its straightforward syntax and dynamic typing. So, whether you're just starting out in programming or brushing up on your Python skills, I hope this article has shed some light on the wonderful world of variables. Happy coding!