Variables in Python

Variables in Python

Variables in Python are containers that store values of different types in it. It is a name given to a reserved memory location. The type of variable in Python is defined by the kind of value that is assigned to it using assignment operators. Value assigned to a variable can be changed throughout its lifetime and it is just a name given to a memory location that holds the value. Variable is just used to easily access the value stored at some memory address.

Rules of giving names to variables:

  • Name of the variable can begin with either a letter or an underscore. It cannot have a digit or any other character in the beginning.
  • They are case-sensitive which means that python, Python and PYTHON are all different.
  • Reserved words which are also called the keywords cannot be used as an identifier.

 

 

Variable Declaration in Python

In Python, you don’t have to declare a variable before using it to make its presence known and to tell the interpreter the amount of memory it needs to reserve. It is declared automatically when a value is assigned to it.

Example of Variable Creation:

# integer variable creation
var_int=34

# string variable creation
var_string="Study Experts"

# printing values of the variables created above
print(var_int)
print(var_string)

Output:

34
Study Experts

 

 

Parallel Assignment in Python

In python, one value can be assigned to multiple variables in a single line using assignment operator “=”.

Example of Parallel Assignment:

# example of parallel assignment
var_a=var_b=var_c=23

# displaying values of the variables created above
print(var_a)
print(var_b)
print(var_c)

Output:

23
23
23

 

 

Assigning different values to multiple variables using a single “=”

Example:

#example of assigning multiple values to multiple variables
#using a single assignment

var_a, var_b, var_c=1,24,56

#printing values of the variables declared above
print(var_a)
print(var_b)
print(var_c)

Output:

1
24
56

 

 

Types of Variables in Python

In Python, there are two types of variables, namely:

    • Local Variables
    • Global Variables

 

 

Local Variables in Python

Local variables are created inside a function and remain in existence as long as the function is. Their scope is limited to the function they are created in. They do not exist outside of a function.

Example of Local Variables in Python:

# example to show local variables in Python

def display():
    # local variable known only inside this function
    str="Welcome to StudyExperts"
    print(str)
 
# call to display()   
display()

Output:

Welcome to StudyExperts

 

 

Global Variables in Python

Global variables are created outside functions and remain in existence as long as the program is. They can be used both inside and outside a function. 

Example of Global Variables:

# example to show global variables in Python

# global variable
str="Welcome to StudyExperts"

def display():
    print(str)
 
# call to display()   
display()

Output:

Welcome to StudyExperts

 

 

Global keyword

If a global and a local variable have the same name, then when used inside the function, then changes made to x inside the function will not be reflected outside it because changes were made to the local version of it. To access a global variable inside the function, global keyword is used.

Example of Global keyword:

# example to show global keyword in Python

# global variable
var_int=23
print("Value of var_int before the call to function change() is ",var_int)
 
# function definition
def change():
    # using the global variable var_int inside the function
    global var_int
    var_int=var_int+2
    print("Value of var_int inside the function change() is ",var_int)

# call to change()
change()

print("Value of var_int outside the function change() is ",var_int)

Output:

Value of var_int before the call to function change() is  23
Value of var_int inside the function change() is  25
Value of var_int outside the function change() is  25

 

 

Share this post

2 thoughts on “Variables in Python

Leave a Reply

Your email address will not be published. Required fields are marked *