How to use Variables in Python3?

Variable is a name for a location in memory. It can be used to hold a value and reference that stored value within a computer program. the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store integers, strings, decimals, complex in these variables. The assignment operator in Python 3 is a single equals sign (=). This operator assigns the value on the right-hand side to the variable on the left-hand side. 

Example 1 : Assign value in a variable:

filter_none

edit
close

play_arrow

link
brightness_4
code

# this is a integer type variable
number = 123456  
  
# this is a string type variable
sentence = "this is a string" 
  
# print integer
print(number) 
  
# print string
print(sentence) 

chevron_right


Output:

123456
this is a string

Example 2 : Multiple Assignment : Python allows us to assign one single value to several variables at the same time

filter_none

edit
close

play_arrow

link
brightness_4
code

# assign single value to multiple variables
a = b = c = d = e = 123
  
# print a
print(a)
  
# print b
print(b)
  
# print c
print(c)
  
# print d
print(d)
  
# print e
print(e)

chevron_right


Output



123
123
123
123
123

An integer is created with the value 123, and all the five variables such as ( a b c d e ) are assigned to the same memory location.

Example 3 : Assign multiple objects to multiple variables :

filter_none

edit
close

play_arrow

link
brightness_4
code

# assign multiple objects to multiple variables
var1, var2, var3 = 123, 5.0, "this is string"
  
# print var2
print(var1)
  
# print var2
print(var2)
  
# print var3
print(var3)

chevron_right


Output:

123
5.0
this is string

An integer objects var1 with values 123, a decimal object var2 with value 5.0, and one string object with the value “this is string” is assigned to the variable var3.

Example 4: Global and Local Variables: A global variable is declared in the global scope or outside of any function, which means the global variable is accessible outside or inside in any function. A local variable is declared inside any function or in the scope of any function. The local variable is only accessible in the scope of it’s declared function. If we try to access a local variable outside its function so this is an error.

filter_none

edit
close

play_arrow

link
brightness_4
code

# global variable
g_var = "global variable"
  
# create a function
def fun1():
  
    # declare a local variable
    l_var = "local variable"
  
    # print global variable
    print(g_var)
  
    # print local variable
    print(l_var)
  
# call fun1
fun1()
  
# print global variable again
print(g_var)

chevron_right


Output:

global variable
local variable
global variable

here the global variable print twice because we call global variable two times once in fun1 and second time outside the function.

Example 5 : Nonlocal variables : Nonlocal keyword is use to create nonlocal variables. Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

# declare fun1
def fun1():
  
    # create a local variable for fun1
    var = "local"
  
    # declare fun2
    def fun2():
  
        # declare nonlocal function
        nonlocal var
  
        # assign value in nonlocal variable
        var = "nonlocal"
  
        # print inner var
        print("inner:", var)
  
    # call fun2
    fun2()
  
    # print outer var
    print("outer:", var)
  
# call fun1 for executing the program
fun1()

chevron_right


Output:

inner: nonlocal
outer: nonlocal

Note : If we change the value of a nonlocal variable, the changes appear in the local variable.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.