What is 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 the 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:
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.
- The 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 commonly used programming languages.
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.
Watch this video on ‘Variables in Python’:
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
a = ‘intellipaat’
Constants in Python
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
Further, take a look at our Python Course and prepare to excel in your career with our free Python interview questions created by the experts.
Python Class Variables
A class variable in Python is shared by all the object instances of the class. They are declared when the class is constructed and not in any of the methods of the class. Because these Class Variables in Python are owned by the class itself, they are shared by all instances of that class.
lang_name = “python”
Here, the variable lang_name is given the value “python”.
Python Private Variables
In Python, ‘Private’ instance variables that can’t be accessed except inside an object, do not practically exist. But most Python coders use two underscores at the beginning of any variable or method to make it private.
A variable __intellipaat will be treated as a non-public or a private variable.
How to get the types of Variables in Python
You can get the data type of a variable with the type() function.
a = 2
b = Python
Output will be -
Scope of Variables in Python
All variables in python may not be accessible at all locations. This will depend on where these variables were declared. Scope of the variable defines where in the program the variable will be accessible. The scope can be either Local or Global. So let’s learn about Local and Global Variables in Python –
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.
f = ‘Intellipaat’
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!
Global Variables In Python
On the other hand, the 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
a = ‘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
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.
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
Here, this block of code will throw a TypeError as variable a is a string type and variable b is a 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))
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 Data Science tutorial.