Python Variables & Types of Python Variables | Python Variable Examples

Here, we have discussed the variables of Python programming language. Variable in python are defined as a reserved location of memory in order to.....

Hey folks! Welcome to an another interesting article on Python Programming Language. 

Python variables and types of python variables, Python variable examples

Here, we will discuss about the variables of Python programming language. 

So, let's dive in!

Python Variable

Variable in python are defined as a reserved location of memory in order to store values or data. In simple words, python variable is a program which is used to give data for processing to the computer. We can also say that the name given to memory location in python is known as Variable.

Identifiers naming

The name of variables are known as identifiers. In other words we can say that Variables are a example of identifiers as we uses identifiers to identify the literals which have been used in a particular program.

Rules for Naming Variables

The following are the rules that one must consider before naming a variable;

• The first alphabet of variable names must be a underscore character or a letter.

• With a number you can't start a variable name.

• Only alpha-numeric characters and underscores are included in a variable name.

• The names of variable are case-sensitive. For example- TWO, two and Two are three different variables.

• One can't use reserved words or keyword in the variable names.

• There must be no special characters and white-space in the name of variables.

Example of Python Variable
Name = "Deepak"

Age = 22

CGPA = 7.6

print(Name)

print(Age)

print(CGPA)

Output

Deepak

22

7.6

Declaring Variable

Now, let's learn about how to declare the python variable and print it;

Number = 143
print(Number)

Output

143

Re-declaring Variable

After declaring the variable, we can re-declare the variable in python;

Number = 143

print("Before declare: ", Number)

Number = 153.3

print("After re-declare:", Number)

Output

Before declare:  143
After re-declare: 153.3

Assigning value to Variable

Now, let's discuss about the assignment of value to variable. 

Single Value to multiple variables- In python, we can easily assign a single value to multiple variables simultaneously by using the "=" operator.

a = b = c = 101

print(a)

print(b)

print(c)

Output

101

101

101

Different Values to multiple variables- In python, we can also assign different values to multiple variables in a single line by using the "," operator.

a, b, c = 101, 102, 103

print(a)

print(b)

print(c)

Output

101

102

103

Types of Python Variables

Now, let's discuss about the types of Python Variables. There are two types of Python variables which are discussed as follows;

1. Local Variable- Local Variables are those variables which have scope within the function and are also declared within the function.

Example:

def add(): 

a = 70 

b = 30  

c = a + b 

print("The sum of two digits is:", c)  

add()  

Output

The sum of two digits is: 100

2. Global Variable- Global Variable are those variables which can be used everywhere in the program and the scope of global variable is in the entire program.

Example:

x = 6.3

def mainFunction():  

global x  

print(x)

x = 'Hey Folks, Welcome to Answersjet'  

print(x)  

mainFunction()  

print(x)  

Output

6.3

Hey Folks, Welcome to Answersjet

Hey Folks, welcome to Answersjet

Conclusion

Above we have discussed the variables of Python programming language. Variable in python are defined as a reserved location of memory in order to store values or data. In simple words, python variable is a program which is used to give data for processing to the computer. There are two types of Python variables i.e., Local Variable and Global Variable.