• Articles
  • Tutorials
  • Interview Questions

Python Variables - Constant, Global & Static Variables

Tutorial Playlist

Table of content

Show More

What is Variable in Python?

A variable’s memory address can change, and when the memory address cannot change, then such a variable is known as a constant. The variable is the name of the memory location where data is stored. Once a variable is stored, 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 a Variable in Python

Python does not have a specific command to declare or create a variable; however, there are some rules that we need to keep in mind while creating Python variables, some of which are mentioned below:

  • The name of a variable cannot start with a number. It should start either with an alphabet or the underscore character.
  • Variable names are always case-sensitive; they can contain alphanumeric or underscore characters.
  • Reserved words cannot be used as variable names.
  • Python variables are always assigned using the equal 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 you to specify the data type of the variable, unlike other commonly used programming languages.

Learn Python Django from industry experts. Enroll now in Python Django Training.

Get 100% Hike!

Master Most in Demand Skills Now!

Assigning Values to Python Variables

There is no need for an explicit declaration to reserve memory. The assignment is done using the equal sign (=) 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

Multiple Variable Assignment

We can assign a single value to multiple variables in the following manner:

a = b = c = 5

Also, we can assign multiple values to multiple variables in the following manner:

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

Watch this video on ‘Variables in Python’:

Video Thumbnail

Re-declaring a Variable in Python

After we have declared a variable, we can declare it once again and assign a new value to it. The Python interpreter discards the old value and considers only 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

Constants in Python

A constant is a type of variable that holds values that cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned to 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)

Further, take a look at our Python Course and prepare to excel in your career with our Python interview questions created by experts.

Python Class Variables

In Python, a class variable 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. Since in Python, these class variables are owned by the class itself, they are shared by all instances of that class.

class languages:
 lang_name = “python”

Here, the variable lang_name is given the value “python”.

Python Private Variables

In Python, ‘Private’ instance variables can’t be accessed except inside an object; they do not practically exist. However, 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 in the following manner:

a = 2
b = Python
print(type(a))
print(type(b))

Output will be –



Scope of Variables in Python

Not all variables in Python may be accessible at all locations. This depends on where these variables were declared. The 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 module can only be used in that specific function or Python Module. This kind of variable is known as a local variable. The Python interpreter will not recognize this variable outside that specific function or module and it will throw an error message 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

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 in the output of the 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 in the output of the first and 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, Python global variables are global variables that can be used anywhere within your program without needing to be declared every time they are needed. They can be utilized within functions or modules and even outside of them without having to do it every time!
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 declared variable a as a global variable and changed its value inside of a function while printing outside of it; it will print its changed value, as can be seen from print(a). Since variable a was declared globally, it can also be used outside its function scope.

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.
Example:

a = 10
print (a)
del a
print (a)

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 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.
Example:

a = ‘Intellipaat’
b = 100
print a+b

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))

Output:

Intellipaat100

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.

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.