Python Variables and Constants
Python Variables
Variables are named locations used to store values in the memory. This means when you create a variable, you reserve some space in memory. Variables can be seen as containers that store data values.
In the following example, we create a variable x
and assign the value 5
to it.
x = 5
You can also change the value that you assign to a variable as follows:
x = 5
x = 5.3
x = 'Hello World'
Initially, the value of the variable x
was 5
then 5.3
, then finally 'Hello World'
.
Note: In Python, we don't actually assign values to the variables. Instead, Python passes the reference to the object(value) to the variable.
Declaring and Assigning values to Variables in Python
Python has no command for declaring a variable. A variable is created the first moment you assign a value to it.
In Python, to assign a value to a variable, we use the operator =
.
Python is a type-inferred language, so it doesn't need to define the variable type explicitly. Python automatically knows the type of the value to be assigned and declares a corresponding variable of that type.
Assigning a value to a variable
To assign a value to a variable, you can use the =
operator. The first time you assign a value to a variable, Python will create this variable.
In the following example, we assign the value Hello World
to the variable greeting
. Then, we print out the value assigned to the greeting
variable.
greeting = "Hello World"
print(greeting)
Output:
Hello World
Changing the value of a variable
In Python, we can change a variable's value by assigning a new value to this variable.
In the following example, we assign the value Hello
to the greeting
variable. Then, we change the value to Hello World
.
greeting = "Hello"
print(greeting)
# assigning another valuer to the greeting variable
greeting = "Hello World"
print(greeting)
Output:
Hello
Hello World
Assigning multiple values to multiple variables
In Python, we can assign multiple values to multiple variables in the same line using the comma ,
separator.
In the following example, we assign different values 10, 6.3, "Hello"
to different a, b, c
variables.
a, b, c = 10, 6.3, "Hello"
print(a)
print(b)
print(c)
Output:
10
6.3
Hello
You can also assign the same value to different variables in the same line.
In the following example, we assign the value "Hello" to all the three variables a
, b
and c
.
a = b = c = "Hello"
print(a)
print(b)
print(c)
Output:
Hello
Hello
Hello
Get the Type of a Variable
In Python, you can get the data type of a variable using the type()
function.
In the following example, we output the data type of two assigned variables.
a = 10
b = "Hello"
print(type(a))
print(type(b))
Output:
<class 'int'>
<class 'str'>
Constants
A constant is a type of variable whose value cannot be changed. Constants can be seen as containers that hold data that cannot be changed later.
In real life scenario, we rarely use constants in Python.
Assigning value to constant in Python
In Python, constants are usually declared and assigned on a different module/file.
In our examples, the module is the new file containing variables, functions, etc ... that is imported to the main file.
Inside the module, constants are written in uppercase letters, and underscores are separating the words.
Declaring and assigning value to a constant
To declare constants, we declare them in a new file/module. Here we will create a new module file named constant.py
. Then, we assign values to PI
and COLUMN
. After that, we create a main.py
file and import the constant module. In the main module, we print the constant value.
constant.py
:
PI = 3.1415926535;
COLUMNS = 80;
main.py
:
import constant
print(constant.PI)
print(constant.COLUMNS)
Output:
3.1415926535
80
Note: In reality, we don't use constants in Python. It is a convention to write constants in all capital letters to separate them from variables. However, it does not actually prevent reassigning new values to them.
Rules and Naming Convention for Variables and Constants
The following list shows rules and naming convention to apply when creating variables and constants in Python:
In Python, variable names are case-sensitive.
x = 6 X = "Hello" # x and X are different variables
Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
Use underscore (_) when you need to separate variable names with more than one word.
my_age client_name
Use uppercase if you want to declare a constant.
PI MASS TABLE_COLUMNS
Never use special symbols like
#, $, @, !, %
etc.Don't start a variable name with a digit.