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.
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:
_
) are allowed to define a variable name in Python._
).name
, Name
and NAME
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.
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:
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
or abbreviated as int
)long
).float
)complex
)x = 5
y = 5.0
z = 5 + 3j
We check the type of each variable using the type()
function:
print(type(x)) //
print(type(y)) //
print(type(z)) //
As you can see, the types of all three variables are different from each other, but they are all numbers.
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.
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
.