A variable is a memory address that can change, and when the memory address cannot change then that variable is known as a constant. Variable is a name of the memory location where data is stored. Once a variable is stored, the space is allocated in memory. It defines a variable using a combination of numbers, letters, and the underscore character.
In this module, we will learn all about variables in Python. Following is the list of all topics that we are going to cover in this module:
Python does not have a specific command just to declare or create a variable; however, there are some rules that we need to keep in mind while creating Python variables.
A variable in Python is created as soon as we assign a value to it. Python also does not require specifying the data type of the variable unlike other programming languages.
There is no need for an explicit declaration to reserve memory. The assignment is done using the equal to (=) operator.
Example: a = 10 b = “Intellipaat” print (a) # a is an int type variable because it has an int value in it print (b) # b is a string type variable as it has a string value in it
We can assign a single value to multiple variables as follows: a = b = c = 5 Also, we can assign multiple values to multiple variables as follows: a, b, c = 2, 25, ‘abc’
Note: Python is a type inferred language, i.e., it automatically detects the type of the assigned variable.
Example 1: test=1 type(test) Output: int Example 2: test1=”String” type(test1) Output: str
After we have declared a variable, we can again declare it and assign a new value to it. Python interpreter discards the old value and only considers the new value. The type of the new value can be different than the type of the old value.
Example: a = 1 print (a) a = ‘intellipaat’ print(a) Output: 1 intellipaat
A variable that is declared inside a python function or a module can only be used in that specific function or Python Module. This kind of variable is known as a local variable. Python interpreter will not recognize that variable outside that specific function or module and will throw an error if that variable is not declared outside of that function.
Example: a=100 print (f) def some_function() f = ‘Intellipaat’ print(f) some_function() print(f) Output: 100 Intellipaat 100
Here, in this example, when the variable f is declared the second time inside the function named some_function, it becomes a local variable. Now, if we use that variable inside the function, there will be no issues as we can see that in the output of second print(f), it prints the value assigned to f in the function, that is, Intellipaat.
Whereas, when we try to print the value of f outside the function, it prints the value assigned to it outside the function as we can see that in the output of the first and the third print(f), it prints 100.
On the other hand, global variable in Python is a variable that can be used globally anywhere in the program. It can be used in any function or module, and even outside the functions, without having to re-declare it.
Example: a = 100 print (a) def som_function(): global a print (a) a = ‘Intellipaat’ some_function() print (a) Output: 100 100 Intellipaat
Here in this example, we have re-declared the variable a in the function as a global variable. Now, if we change the value of this variable inside the function and then print the value of this variable outside the function, then it will print the changed value as we can see in the output of the third print(a). Since variable a was declared globally, it can be used outside the function as well.
Python provides a feature to delete a variable when it is not in use so as to free up space. Using the command del ‘variable name’, we can delete any specific variable.
Example: a = 10 print (a) del a print (a)
If we run the above program, Python interpreter will throw an error as ‘NameError: name a is not defined’ in the second print (a), since we have deleted the variable a using the del a command.
If we want to concatenate Python variables of different data types, let’s say a number variable and a Python String variable, then we will have to declare the number variable as a string. If the number variable is not declared as a string variable before concatenating the number variable with a string variable, then Python will throw a TypeError.
Example: a = ‘Intellipaat’ b = 100 print a+b
Here, this block of code will throw a TypeError as variable a is string type and variable b is number type. To remove this error, we will have to declare the number variable as a string variable as shown in the example below:
a = ‘Intellipaat’ b = 100 print(a + str(b)) Output: Intellipaat100
A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned on a different module/file.
Example: #Declare constants in a separate file called constant.py PI = 3.14 GRAVITY = 9.8 Then, they are imported to the main file. #inside main.py we import the constants import constant print(constant.PI) print(constant.GRAVITY)
This brings us to the end of this module in Python Tutorial. Now, if you are interested in knowing why python is the most preferred language for data science you can go through this Python for Data Science blog.
Download Interview Questions asked by top MNCs in 2019?