Learn Python – Python Variables- Basic and advance

Variable is a title that is used to refer to reminiscence location. Python variable is also known as an identifier and used to hold value.

In Python, we do not want to specify the type of variable because Python is a infer language and clever adequate to get variable type.

Variable names can be a crew of both the letters and digits, however they have to begin with a letter or an underscore.

It is recommended to use lowercase letters for the variable name. Rahul and rahul each are two one of a kind variables.

Identifier Naming

Variables are the example of identifiers. An Identifier is used to perceive the literals used in the program. The regulations to title an identifier are given below.

The first character of the variable must be an alphabet or underscore ( _ ).

All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9).

Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).

Identifier name must not be similar to any keyword defined in the language.

Identifier names are case sensitive; for example, my name, and MyName is not the same.

Examples of valid identifiers: a123, _n, n_9, etc.

Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values

Python does now not bind us to declare a variable before using it in the application. It lets in us to create a variable at the required time.

We don’t need to declare explicitly variable in Python. When we assign any fee to the variable, that variable is declared automatically.

The equal (=) operator is used to assign cost to a variable.

Object References

It is crucial to apprehend how the Python interpreter works when we declare a variable. The method of treating variables is extremely one-of-a-kind from many different programming languages.

Python is the exceedingly object-oriented programming language; it’s why each data object belongs to a unique kind of class. Consider the following example.




The Python object creates an integer object and shows it to the console. In the above print statement, we have created a string object. Let’s test the type of it the usage of the Python built-in type() function.



<class 'str'>

In Python, variables are a symbolic identify that is a reference or pointer to an object. The variables are used to denote objects through that name.

Let’s understand the following example

a = 50   

In the above image, the variable a refers to an integer object.

Suppose we assign the integer fee 50 to a new variable b.

a = 50

b = a

The variable b refers to the same object that a factors to because Python does no longer create another object.

Let’s assign the new price to b. Now both variables will refer to the exclusive objects.

a = 50

b =100

Python manages memory correctly if we assign the same variable to two exclusive values.

Object Identity

In Python, each and every created object identifies uniquely in Python. Python affords the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example.

a = 50  
b = a  
# Reassigned variable a  
a = 500  



We assigned the b = a, a and b both point to the identical object. When we checked by means of the id() feature it returned the same number. We reassign a to 500; then it referred to the new object identifier.

Variable Names

We have already mentioned how to declare the valid variable. Variable names can be any size can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following instance of valid variables names.

name = "Devansh"  
age = 20  
marks = 80.50  



Consider the following valid variables name.

name = "A"  
Name = "B"  
naMe = "C"  
NAME = "D"  
n_a_m_e = "E"  
_name = "F"  
name_ = "G"  
_name_ = "H"  
na56me = "I"  
print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, na56me)  



In the above example, we have declared a few valid variable names such as name, _name_ , etc. But it is no longer encouraged due to the fact when we attempt to read code, it can also create confusion. The variable identify be descriptive to make code greater readable.

The multi-word key phrases can be created by way of the following method.

Camel Case – In the camel case, each word or abbreviation in the middle of begins with a capital letter. There is no intervention of whitespace. For example – nameOfStudent, valueOfVaraible, etc.

Pascal Case – It is the same as the Camel Case, but here the first word is also capital. For example – NameOfStudent, etc.

Snake Case – In the snake case, Words are separated by the underscore. For example – name_of_student, etc.

Multiple Assignment

Python lets in us to assign a cost to a couple of variables in a single statement, which is also known as multiple assignments.

We can observe a couple of assignments in two ways, both by way of assigning a single price to a couple of variables or assigning more than one values to more than one variables. Consider the following example.

  1. Assigning single value to multiple variables





2 Assigning more than one values to a couple of variables:


print a    
print b    
print c    



The values will be assigned in the order in which variables appear.

Python Variable Types

There are two sorts of variables in Python – Local variable and Global variable. Let’s understand the following variables.

Local Variable

Local variables are the variables that declared interior the feature and have scope inside the function. Let’s recognize the following example.

Example –

# Declaring a function  
def add():  
    # Defining local variables. They has scope only within a function  
    a = 20  
    b = 30  
    c = a + b  
    print("The sum is:", c)  
# Calling a function  


The sum is: 50


In the above code, we declared a function named add() and assigned a few variables inside the function. These variables will be referred to as the local variables which have scope solely interior the function. If we try to use them backyard the function, we get a following error.

# Accessing local variable outside the function   


The sum is: 50
NameError: name 'a' is not defined

We tried to use nearby variable outside their scope; it threw the NameError.

Global Variables

Global variables can be used at some stage in the program, and its scope is in the complete program. We can use international variables inside or outside the function.

A variable declared outside the feature is the world variable with the aid of default. Python gives the global keyword to use international variable interior the function. If we do not use the global keyword, the characteristic treats it as a nearby variable. Let’s understand the following example.

Example –

# Declare a variable and initialize it  
x = 101  
# Global variable in function  
def mainFunction():  
    # printing a global variable  
    global x  
    # modifying a global variable  
    x = 'Welcome To Javatpoint'  


Welcome To Javatpoint
Welcome To Javatpoint


In the above code, we declare a global variable x and assign a cost to it. Next, we described a feature and accessed the declared variable using the global key-word inside the function. Now we can regulate its value. Then, we assigned a new string value to the variable x.

Now, we called the function and proceeded to print x. It printed the as newly assigned cost of x.

Delete a variable

We can delete the variable the use of the del keyword. The syntax is given below.

Syntax –

del <variable_name>  

In the following example, we create a variable x and assign fee to it. We deleted variable x, and print it, we get the error “variable x is not defined”. The variable x will no longer use in future.

Example –

# Assigning a value to x  
x = 6  
# deleting a variable.   
del x  


Traceback (most recent call last):
  File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in 
NameError: name 'x' is not defined

Maximum Possible Value of an Integer in Python

Unlike the other programming languages, Python would not have lengthy int or flow records types. It treats all integer values as an int statistics type. Here, the question arises. What is the maximum possible value can hold by the variable in Python? Consider the following example.

Example –

# A Python program to display that we can store  
# large numbers in Python  
a = 10000000000000000000000000000000000000000000  
a = a + 1  
print (a)  


<class 'int'>

As we can see in the above example, we assigned a massive integer value to variable x and checked its type. It printed class now not long int. Hence, there is no quandary variety by using bits and we can amplify to the restriction of our memory.

Python does not have any extraordinary records kind to shop larger numbers.

Print Single and Multiple Variables in Python

We can print more than one variables inside the single print statement. Below are the example of single and more than one printing values.

Example – 1 (Printing Single Variable)

# printing single value   
a = 5  



Example – 2 (Printing Multiple Variables)

a = 5  
b = 6  
# printing multiple variables  
# separate the variables by the comma  
Print(1, 2, 3, 4, 5, 6, 7, 8)   


5 6
1 2 3 4 5 6 7 8

Basic Fundamentals:

This part contains the fundamentals of Python, such as:

i)Tokens and their types.

ii) Comments


The tokens can be defined as a punctuator mark, reserved words, and each word in a statement.

The token is the smallest unit inside the given program.

There are following tokens in Python:





We will talk about above the tokens in element subsequent tutorials.