1. What is a Python variable
A variable in Python is a named storage location used to store data during the execution of a program. It is essentially a container that holds a value or a reference to an object. Variables can have different data types, such as numbers, strings, lists, dictionaries, etc., and their values can be changed or reassigned throughout the program.
In Python, variables are created on-the-fly by assigning a value to them using the assignment operator "=" (e.g., x = 10). The variable name is on the left side of the assignment operator, and the value or expression to be assigned is on the right side. Python is dynamically typed, which means that you don't need to declare the type of a variable explicitly. The type is inferred from the assigned value.
Example
1 |
n = 25 |
here we have declared a variable named 'n' and which has 25 as value. At this time, python detects the type of variable and classyfy it in the integer category.
To display the variable, we use the print instruction (we will see that it is a built-in function in python)
Example
1 2 |
n = 25 print(n) |
What displays at execution: 25
You can also add an explanatory text
Example
1 2 |
n = 25 print("The value of n is: ", n) |
Which displays at execution:
The value of n is: 25
Example
1 2 |
name = "Farid" print("The name is: ", name) |
Which displays at execution:
The name is: Farid
2. Variable Naming Conventions
The Python language allows the user to define their own variables provided they respect a set of rules:
- The name of a variable must start with a letter or an underscore: like student1, student2. 1student is not accepted!
- All characters except the first character can be an alphabet consisting of lowercase (a-z), uppercase (A-Z), underscores, or numbers (0-9).
- The name of a variable must not contain spaces or special characters (!, @, #, %, ^, &, *).
- The name of a variable must not be identical to a keyword defined in the language.
- The name of a variable is case sensitive: for example, Robert and robert are not the same.
- Examples of valid variables: bus1 , bus_number_2, _a, b_7, etc.
- Examples of invalid variables: 3alpha, x%2, self-driving car, etc.
3. Types of python variables
The variable types offered by python are:
- type integer or int, example: 12 , 3 , 77 ...
- type float, example: 12.5 , 3.75 , 77.52 ...
- type string or str: example "robert" , "car" ...
- Boolean type: this type takes two values: True (true) and False (False)
To display the type of a variable, we use the function type()
Example
1 2 |
n = 10 print(type(n)) |
Which displays at execution: <class 'int'>
Example
1 2 |
x = 7.55 print(type(x)) |
Which displays at execution: <class 'float'>
Example
1 2 |
name = "robert" print(type(name)) |
Which displays at execution: <class 'str'>
Example
1 2 3 |
var = 5 > 7 print("The value of the variable is var = ", var) print("The type of var is " , type(var)) |
Which displays at execution:
The value of the variable is var = False
The type of var is <class 'bool'>
4. Convert or change the type of a variable
A Python variable can be easily converted using the following functions: int(), str() , float() etc.
Example
1 2 3 4 5 6 7 |
#define an integer variable n = 10 # converting variable to float type x = float(n) print("the type of n is: ", type(n)) print("the type of x is: ", type(x)) |
Output:
the type of n is: <class 'int'>
the type of x is: <class 'float'>
Example
# define a float variable
1 2 3 4 5 6 7 |
x = 10.0 # converting variable to int type n = int(x) print("the type of x is: ", type(x)) print("the type of n is: ", type(n)) |
Output:
the type of x is: <class 'float'>
the type of n is: <class 'int'>
5. Multiple Assignment
The python language offers the possibility to assign values to several variables at the same time
Example
1 2 3 4 5 |
# multiple assignement n, m = 10, 20 print("n = ", n) print("m = ", m) |
Output:
n = 10
m = 20
6. Variable Object Identity
In Python, each created object uniquely identifies itself in Python. Python provides the guarantee that no two objects will have the same identifier. The built-in id() function is used to identify the id object. Consider the following example.
Example
1 2 3 4 5 6 7 |
n = 10 m=n print("Id of n: ", id(n)) print("Id of m: ", id(m)) # change the value of variable n n = 20 print("Id of n: ", id(n)) |
Output:
Id of n: 8651024
Id of m: 8651024
Id of n: 8651184
7. Destroy a variable
To delete or destroy a python variable, just use the del() function
Syntax
1 |
del <variable_name> |
Example
1 2 3 4 |
n = 10 del n print(n) |
Output:
builtins.NameError: name 'n' is not defined
Younes Derfoufi