A variable is a name that is used to refer to a memory location. Variables in Python are used to store data and are also referred to as "identifiers."
Since Python is an inferring language and intelligent enough to determine the type of a variable, we don't need to specify it.
Variable names must start with a letter or an underscore and can contain both letters and numbers.
It is recommended to use lowercase letters for the variable name. Orange and orange are both two different variables.
In a Python program, values are stored in variables, which are designated areas of memory. This implies that you set aside some memory when you create a variable.
The Python interpreter decides how much memory needs to be allocated and what can be stored in the allocated memory based on the data type of a variable. Therefore, you can store integers, decimals, or characters in Python variables by giving them different data types.
Identifier Naming
Variables are an example of identifiers. The literals used in the program are designated by an Identifier. Below are the guidelines for naming an identifier.
- The variable's first character must be an alphabetic character or an underscore (_).
- All the characters—aside from the first one—can be either a digit (0–9), an alphabet of lowercase letters (a–z), uppercase letters (A–Z), or underscores.
- No white space or special characters (!, @, #,%,, &, *) are allowed in the identifier name.
- The name of the identifier cannot be similar to any language-defined keyword.
- Names used as identifiers are case-sensitive; for instance, my name and MyName are not the same.
Examples of acceptable identifiers include a123, _n, n_9, etc.
Invalid identifiers include 1a, n%4, n 9, etc.
Creating Python Variables
When you want to create a variable in Python, you don't need to explicitly declare it first. When you assign a value to a Python variable, it automatically creates one. Assigning values to variables is done with the equal sign (=).
The name of the variable is the operand to the left of the = operator, and the value that is stored in the variable is the operand to the right of the = operator. For illustration,
range = 100 # Creates an integer variable
volume = 1000.0 # Creates a floating point variable
staff = "John Doe" # Creates a string variable
Printing Python Variables
The print() function can be used to output a Python variable after we've created it and given it a value. The example that follows continues the previous one and demonstrates how to print various variables in Python:
range = 100 # Creates an integer variable
volume = 1000.0 # Creates a floating point variable
staff= "John Doe" # Creates a string variable
print (range)
print (volume)
print (staff)
Here, the range, volume, and staff variables are each given the values 100, 1000.0, and "John Doe," respectively. Running the Python program mentioned above results in the output shown below:
100
1000.0
John Doe
Delete a Variable
Using the del statement, you can remove the reference to a number object. The del statement has the following syntax:
del var1[,var2[,var3[....,varN]]]]
The del statement allows you to delete one or more objects at once. For example:
del var
del var_a, var_b
Example
The examples below demonstrate how to delete a variable, and the Python interpreter will throw an error if you attempt to use a deleted variable:
counter = 100
print (counter)
del counter
print (counter) # printing the deleted variable
This will produce the following result:
100
Traceback (most recent call last):
File "main.py", line 7, in <module>
print (counter)
NameError: name 'counter' is not defined
Multiple Assignment
You can create multiple variables at once in Python because it allows you to assign a single value to many variables at once. Consider this:
a = b = c = 100
print (a)
print (b)
print (c)
The outcome is as follows:
100
100
100
Here, the three variables are all assigned to the same memory location along with the creation of an integer object with the value 1. Additionally, you can link several objects to various variables. For instance,
a,b,c = 1,2,"Jane Doe"
print (a)
print (b)
print (c)
This produces the following result:
1
2
Jane Doe
Here, variables a and b are each given two integer objects with the values 1 and 2, and the string object "Jane Doe" is given to variable c.
Local Variable in Python
Local variables in Python are defined within a function. Outside of the function, we are unable to access a variable.
Here is an illustration of how to use local variables:
def sum(x,y):
sum = x + y
return sum
print(sum(5, 10))
15
Global Variable in Python
Any variable created outside of a function has global scope because it is possible to access it from inside of any other function. Here is an illustration of a global variable:
x = 5
y = 10
def sum():
sum = x + y
return sum
print(sum())
This will produce the following result:
15