Python Variables

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

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

What displays at execution: 25

You can also add an explanatory text

Example

Which displays at execution:
The value of n is: 25

Example

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:

  1. The name of a variable must start with a letter or an underscore: like student1, student2. 1student is not accepted!
  2. All characters except the first character can be an alphabet consisting of lowercase (a-z), uppercase (A-Z), underscores, or numbers (0-9).
  3. The name of a variable must not contain spaces or special characters (!, @, #, %, ^, &, *).
  4. The name of a variable must not be identical to a keyword defined in the language.
  5. The name of a variable is case sensitive: for example, Robert and robert are not the same.
  6. Examples of valid variables: bus1 , bus_number_2, _a, b_7, etc.
  7. Examples of invalid variables: 3alpha, x%2, self-driving car, etc.

3. Types of python variables

The variable types offered by python are:

  1. type integer or int, example: 12 , 3 , 77 ...
  2. type float, example: 12.5 , 3.75 , 77.52 ...
  3. type string or str: example "robert" , "car" ...
  4. 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

Which displays at execution: <class 'int'>

Example

Which displays at execution: <class 'float'>

Example

Which displays at execution: <class 'str'>

Example

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

Output:
the type of n is: <class 'int'>
the type of x is: <class 'float'>

Example

# define a float variable

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

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

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

Example

Output:
builtins.NameError: name 'n' is not defined

 

Younes Derfoufi

Leave a Reply