
Variables are like containers used to temporarily store data. Defining variables in Python is very simple. In this post, we will learn about python variables and python data types.
In a more specialized view, a variable is a reserved space in the memory that stores the values assigned to it in that part of the memory. At the time of variable creation, part of the memory is allocated to it and occupied.
In some programming languages, when defining a variable, we must also specify its type.
One of the good (and maybe bad) things about the Python programming language is that it doesn't need to define variable data types. That is, whether we want to put a number in the variable or a text string, in any case, it is enough to write the name of the variable and perform the assignment operation!
In this article, I will first discuss how to define and name the variables in Python and then check the common data types. Then we will get acquainted with some tricks of working with variables. Finally, I will talk about constants in Python.
Variable declaration in Python
Whenever we assign a value to a variable, that variable is automatically created in the program.
Python variable assignment has three main parts:
- Variable name
- Assignment operator
- Variable value
I'll talk about how to name variables in the next section. The assignment operator is the equal sign (=
). By placing the equals operator in front of the variable name, we can assign a new value to it.
In the following code snippet, we have defined several different variables and assigned arbitrary values to them.
name = "Elina"
age = 25
score = 86.7
In this code, we have three variables, name
, age
, and score
, which will have the values of elina
, 25
, and 86.7
, respectively.
With the print
function in Python, we can print the values of these variables in the output:
print(name)
print(age)
print(score)
The print output of the variables is as follows:

Changing a Python variable
During the execution of the program, whenever we want to change the value of a variable, we just need to assign the new value to it. So easily!
In the code below, I created a variable and then printed it. Then I increase its value to the power of 2 and print it again in the output.
num = 17
print(num)
num = num ** 2
print(num)
After executing the code, first the first number is printed and then its second power is printed.
17
289
Simultaneous assignment of multiple variables
If in your program you want to value several variables at the same time, you can use the method of assigning multiple values to multiple variables.
There are two modes for simultaneous assignment:
The first case is that the value of all variables is equal to each other.
In order to assign a value to several variables in Python at the same time, we must write the names of the next variables after the first equal operator and write their value after the last operator.
For better understanding, consider the following example:
a = b = c = 157
In this code, three variables a
, b
and c
have the same value 157
.
In the second case we have several variables with different values. For this, we act as follows.
name, code, car = "Omid", 6257, "BMW X7"
In this case, three variables name
, code
and car
are defined and each one will contain its own value.

Python variable naming conventions
Variable names in Python follow four simple and general rules that we must follow. If we do not follow these rules, the variable name will not be correct and we will encounter an error.
These 4 laws are:
- Only letters of the alphabet (lowercase and uppercase) plus the underscore (
_
) are allowed to define a variable name in Python. - The variable name must start with an English letter or an underscore (
_
). - The beginning of the variable name cannot be a number!
- Python variable names are case sensitive. Therefore, the variables
name
,Name
andNAME
are three different variables.
In the code snippet below, I have written several different and correct types:
myvar = "Test"
myvar2 = "Test"
my_var = "Test"
_my_var = "Test"
myVar = "Test"
MYVAR = "Test"
I suggest that the names you choose for your variables be as meaningful as possible. It means that using name
instead of n
will be more professional.
Variable types in Python
Almost anything you can think of can be represented in Python in some way. By default, commonly used data types are defined in Python.
If we need data in another type, we either have to create it ourselves or look for related libraries.
The most important data types in Python are:
- number (integer, real and mixed)
- string
- list
- tuple
- dictionary
- object
Definition of number in Python
A numeric variable is defined in Python like any other variable. We can define and assign the following four numeric types to variables:
- Integer and signed numbers (
integer
or abbreviated asint
) - Very large integers (
long
). - floating point real numbers (
float
) - Different mathematical numbers (
complex
)
x = 5
y = 5.0
z = 5 + 3j
We check the type of each variable using the type()
function:
print(type(x)) // <class 'int'>
print(type(y)) // <class 'float'>
print(type(z)) // <class 'complex'>
As you can see, the types of all three variables are different from each other, but they are all numbers.
Tricks of working with Python variables
In this section, I will review 2 tricks or methods to do more professional things with Python variables. Timely and correct use of these methods depends on creativity. The more you work with a programming language, the more proficient you will become in using its features.
Python global variables
A program may consist of various sections and functions.
Every function or class we define has a scope. This range starts from the beginning of its definition and will continue until the end of its indentations.
This section is called scope. Any variable defined in a scope will be usable and available only in that scope.

Sometimes we need to define a variable in a general way so that it is available in all the sub-ranges and functions of that file.
For this, it is enough to define the variable in the outermost scope of the program. Usually, the outermost part is the beginning of the program codes.
When we define a variable inside a function scope, by default that variable will be available locally in the same function.
To define a global variable within a specific scope, we use the global
keyword.
By writing the global
keyword to the Python interpreter, we understand that this variable should be treated as a global variable and be valid outside the scope.
In the following code, the variable level is defined for the first time in getLevel()
but globally. That's why we can use it in the last line.
def getLevel():
global level
level = "fantastic"
getLevel()
print("Albro is " + level)
Deleting a variable in Python
In writing a small program, we usually don't care about unused variables and leave them alone. In large programs, removing extra variables may help greatly in reducing main memory consumption.
The del
statement is used to delete a variable from memory (RAM). It is enough to write the desired variable name in front of this statement.
msg = "Hello World!"
print(msg)
del msg
print(msg)
By executing this code, the value of the msg
variable is printed in the second line, but in the fourth line, we will face the following error because the variable no longer exists.

Constant value in Python
In fact, we do not have a fixed data type in Python! If you have followed the programming basics, you know that in many languages there is a value called constant
(sometimes also called static
). Fixed values are set once and cannot be changed.
A constant
in Python does not exist in the sense that it cannot be changed anymore! We just don't change the value of the defined variable.
As an unwritten rule, most programmers capitalize the names of constant values. For example, they use the URL name to define a constant url.
A basic and standard way to define constants in Python is to put the constants in another file and add it to the main program as a Python module.
With this, if there is ever a need to change the constant values in the whole program, we just need to change the file containing them.
Let's take a simple example. I create a file called configs.py
and put the following code inside it.
URL = "sampleserver.com"
PORT = 2456
KEY = "s52#dgu83vE$"
Now I create a file called run.py
next to it and write the following code (somehow the main code of the program) inside it.
import configs
print(configs.URL)
print(configs.PORT)
In this code we use the seemingly constant values that we created in the configs.py
file. As you can see, to call the PORT
value, we write it as configs.PORT
.