What Is a Variable in Python?
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.
Watch this video on ‘Variables in Python’:
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:
- Creating and Declaring a Variable
- Re-declaring a Variable
- Local Variable
- Global Variable
- Deleting a Variable
- Concatenating Variables
Creating and Declaring Python Variables
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.
- Name of a variable cannot start with a number. It should start with either an alphabet or the underscore character.
- Variable names are always case sensitive and can contain alphanumeric characters and the underscore character.
- Reserved words cannot be used as variable names.
- Python Variables are always assigned using the equal to sign followed by the value of the variable.
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.
Become a master of Python by going through this online Python Course in Toronto!
Assigning Values to Python Variables
There is no need for an explicit declaration to reserve memory. The assignment is done using the equal to (=) operator.
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
Multiple Variable Assignment
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.
test=1 type(test) Output: int
test1=”String” type(test1) Output: str
Get certified from this top Python Course in Singapore today!
Re-declaring a Variable in Python
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.
a = 1 print (a) a = ‘intellipaat’ print(a) Output: 1 intellipaat
Local Variables in Python
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.
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.
Learn end-to-end Python concepts through the Python Course in Hyderabad to take your career to a whole new level!
Watch this video on ‘Python Tutorial for Beginners’:
Global Variables In Python
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.
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.
Deleting Python Variables
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.
a = 10 print (a) del a print (a)
Are you interested in learning Python from experts? Enroll in our Python Course in Bangalore now!
Concatenating Python Variables
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.
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.
#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.
Further, take a look at our Python Certification course and prepare to excel in career with our free Python interview questions created by the experts.