1. Defining a Python variable
In any programming language, a variable is a name used to refer to a location in a memory area. Python variable is also known as identifier and used to store a numeric, string or boolean value...
In Python, we don't need to specify the type of variable because Python is a dynamically typed language and smart enough to get the type of variable.
To declare a variable in python, you don't need to declare its type. A variable in python is declared directly by inserting its name and assigning an initial value. Once the variable is inserted, python automatically detects its type:
Example
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
n = 25 print(n)
What displays at execution: 25
You can also add an explanatory text
Example
n = 25 print("The value of n is: ", n)
Which displays at execution:
The value of n is: 25
Example
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
n = 10 print(type(n))
Which displays at execution: <class 'int'>
Example
x = 7.55 print(type(x))
Which displays at execution: <class 'float'>
Example
name = "robert" print(type(name))
Which displays at execution: <class 'str'>
Example
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
#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
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
# 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
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
del <variable_name>
Example
n = 10 del n print(n)
Output:
builtins.NameError: name 'n' is not defined
Younes Derfoufi
my-courses.net