A variable is a memory address that can change and when memory address cannot change then that variable is known as constant. Variable is a name of the memory location where data is stored. Once a variable is stored then 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 the topics that we are going to cover, in case you want to jump to a specific one.
Watch this video on Variable in Python
Python does not have a specific command just to declare or create a variable however, there are some rules that you need to keep in mind while creating variables.
A variable in Python is created as soon as you 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 (=) operator.
You can assign a single value to the multiple variables as follows:
Also, we can assign multiple values to the multiple variables as follows:
Note: Python is type inferred language i.e. it automatically detects the type of assigned variable.
For instance, example 1:
Learn Python in 16 hrs from experts
After you have declared a variable you can again declare it and assign a new value to it. The 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.
A variable that is declared inside a function or a module can only be used in that specific function or module. This kind of variable is known as local variable. The Python interpreter will not recognize that variable outside that function or module and will throw an error if that variable is not declared outside of that function.
Here in this Python example, when the variable f is declared the second time inside the function named some_function, it becomes a local variable. Now if you use that variable inside the function there will be no problem as we can see in the output of second print (f), it prints the value assigned to f in the function that is, Intellipaat.
While 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 in the output of first and third print (f), it prints 100.
Global variable on the other hand is a variable that can be used globally anywhere in the program. It can be used in any function or module and even outside of functions without having to re-declare it.
Here in this Python 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 that 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 the space. Using the command del “variable name”, we can delete any specific variable.
If we run the above program, the 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.
In Python, if you want to concatenate variables of different datatypes, let’s say a number variable and a string variable, then you will have to declare the number variable as string. If the number variable is not declared as string variable before concatenating the number variable with a string variable, then python will throw a TypeError.
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 string variable as shown in the example below:
Constant is a type of variable that holds values, whose value cannot be changed. In reality we rarely use constants in python. Constants are usually declared and assigned on a different module/file.
Then import it to the main file.
This brings us to the end of this module. Next module highlights Python datatypes and number datatype in Python. See you there!Previous Next
Download Interview Questions asked by top MNCs in 2018?