Variables and Constants in Python

This post is lesson 5 of 54 in the subject Python Programming Language

1. Variables in Python

A variable in Python represents a memory location (in RAM) that stores data for the program. We can change the value of a variable. For example:

x = 20
y = 50
sum = x + y
n = 5
n = 8
Illustrate variable memory in python
Illustrate variable memory in Python

In the example above, the memory regions in RAM will store some data (values) (20, 50, 70, 8, 5). A variable name (x, y, sum, n) represents a memory region. We can change the values in these memory regions. For example, the variable n’s value can be changed from 5 to 8.

The naming convention for variables follows the rules for identifiers in Python. A variable created in Python has 4 characteristics: variable name, value of the variable, data type of the variable, and address of the variable.

Assigning a value to a variable in Python

We can use the assignment operator ‘=’ to assign a value to a variable.

Note: In Python, when creating a variable, we must assign a value to a variable.

n = 10
print("The data type of the variable n is ", type(n))
m = 1.1
print("The data type of the variable m is ", type(m))
website = "apple.com"
print("The data type of the variable website is ", type(website))
website = 99
print("The data type of the variable website is ", type(website))

Result

The data type of the variable n is  <class 'int'>
The data type of the variable m is  <class 'float'>
The data type of the variable website is  <class 'str'>
The data type of the variable website is  <class 'int'>

Python is a type-inferred language. Therefore, we do not need to explicitly define the data type of variables. Based on the value assigned to the variable, Python will automatically determine which data type the variable has.

Note: In Python, the value is not assigned to a variable. Instead, Python creates a reference to an object of a data type (int, float, str, etc.) for the variable. Simply put, Python automatically considers which data type the values are. Then, it will create an object of the corresponding type to store that value. Therefore, a variable is assigned an object of a data type, not a value.

Example of assigning multiple values to multiple variables

a, b, c = 5, 3.2, "Hello"

print (a)
print (b)
print (c)

x = y = z = "same"

print (x)
print (y)
print (z)

Address of variables in Python

Using the id() function

Help return the address of a specific object.

# get id of list
a = [1, 2, 3, 4, 5]
print(id(a))
  
# get id of a variable
a = 12
print(id(a))
  
# get id of tuple
a = (1, 2, 3, 4, 5)
print(id(a))
  
# get id of a dictionary
a = {'a' : 1, 'b' : 2}
print(id(a))

Result

2817775580992
2817770586704
2817775539664
2817775483776

Using the addressof() function

The addressof() function is located in the ctypes library. This library is a foreign function library for Python. It is a library that helps call functions from other libraries not part of Python.

# import addressof,
# c_int modules from ctypes module
from ctypes import c_int, c_float, c_wchar_p, addressof
  
# get memory address of variable
a = 44
print(addressof(c_int(a)))
b = 1.1
print(addressof(c_float(b)))
c = "gochocit.com"
print(addressof(c_wchar_p(c)))

Result

2352906862344
2352906862344
2352906862344

Using the hex() function

The hex() function helps to represent the address of an object in hexadecimal format.

# get id of list in hexadecimal representation
a = [1, 2, 3, 4, 5]
print(hex(id(a)))
  
# get id of a variable in hexadecimal representation
a = 12
print(hex(id(a)))
  
# get id of tuple in hexadecimal representation
a = (1, 2, 3, 4, 5)
print(hex(id(a)))
  
# get id of a dictionary in hexadecimal representation
a = {'a': 1,'b' : 2}
print(hex(id(a)))

Result

0x273ee7b3740
0x273ee2e0250
0x273ee7a95d0
0x273ee79bb80

Note: Each time the program is run, the data storage address may be different. This depends on which memory area is available for allocation.

2. Constants in Python

Constant is a type of variable that cannot be changed. That means, data is stored in memory that the constant represents and cannot be changed.

Constants in Python

In Python, we usually declare and assign values of constants within a module. A module is essentially a Python file that contains a variety of elements, including variables and functions. Constants are capitalized and separated by underscores (_). For example:

Create a constant.py file

PI = 3.14
GRAVITY = 9.8
WEBSITE_ADDRESS = "gochocit.com"

Create a file named main.py

import constant

print(constant.PI)
print(constant.GRAVITY)
print(constant.WEBSITE_ADDRESS)

constant.PI = 5.9

print(constant.PI)

Result

3.14
9.8
gochocit.com
5.9

In the above example, we create a module file called constant.py that defines constants PI, GRAVITY, and WEBSITE_ADDRESS. Then, we make a file called main.py and import the constant.py module. Finally, we print out the values of the constants.

Note: In reality, we do not use constants in Python. Naming constants with capital letters in Python is just a convention to easily differentiate them from variables. It does not prevent reassigning values to constants. The above example shows that the constant PI can still be reassigned and its value changed.

5/5 - (1 vote)
Previous and next lesson in subject<< Basic components and syntax in Python programsBasic data types in Python >>

Leave a Reply

Your email address will not be published. Required fields are marked *