Python Variables



It is a name of storage space that is used to store data.

Its value may be changed.

It always contains the last value stored in it.

There is no need to declare a variable type in python.

Variable is created by assigning a value to it.

 



Rules for define a variable

The first letter of a variable should be alphabet(A-Z/a-z) or underscore(_).

 

The first letter of the variable should not be a digit.

After the first character, it may be a combination of alphabets and digits.

Blank spaces are not allowed in a variable name.

A variable name should not be a keyword.

Variable names are case sensitive for example marks and MARKS are different variables.


Example :

1code = 3

Output :

SyntaxError: invalid syntax

Example :

code = 123
print(code)

Output :

123

Example :

_code = 5
print(_code)

Output :

5

 Example :

code = 123
print(Code)

Output :

NameError: name 'Code' is not defined


Reserved words (keywords) cannot be used as identifier names.

and def False import not True
as del finally in or try
assert elif for is pass while
break else from lambda print with
class except global None raise yield
continue exec if nonlocal return  

 


Initialize variable in python


name = "Python"
number = 786
marks = 78.44

Here name, number, and marks are the name of variables.

We can print the value of a variable using the print() function.



Example :

# Initialize Variables
name = "Python"
number = 786
marks = 78.44

# Print Variables
print("Name : ",name)
print("Number : ",786)
print("Marks : ",marks)

Output :

Name :  Python
Number :  786
Marks :  78.44

Assigning a single value to multiple variables.

We can assign a single value to multiple variables using a single statement in python.


Example :

# Creating Variable
a = b = c = 120

# Print Variables
print("a : ",a)
print("b : ",b)
print("c : ",c)

Output :

a :  120
b :  120
c :  120

Assigning multiple values to multiple variables

We can also assign multiple values to multiple variables using a single statement in python.


Example :

# Creating Variable
a,b,c=10,20,30

# Print Variables
print("a : ",a)
print("b : ",b)
print("c : ",c)

Output :

a :  10
b :  20
c :  30


Local Variable

A variable declared inside the body of the function is called a local variable.

A local variable can be used only inside that function in which it is defined.


Example :

# Define Function
def add():
    # Creating variables
    a = 10
    b = 20
    c = a + b
    print("Addition of a and b is",c)

# Call Funtion
add()

# This Line raise an Error Cause c is a local variable.
print(c)

Output : 

Addition of a and b is 30
Traceback (most recent call last):
  File "codextreame.py", line 12, in <module>
    print(c)
NameError: name 'c' is not defined


Global Variable

A variable that is created outside a function is called a global variable.

It can be used anywhere in the program.


Example :

a = 10
b = 20

# Define Function
def add():
    c = a + b
    print("Inside of the Function, Addition of a and b is",a+b)

# Call Funtion
add()
# Global Variable
print("Outside of the Function, Addition of a and b is",a+b)

Output :

Inside of the Function, Addition of a and b is 30
Outside of the Function, Addition of a and b is 30


Local and Global variable with the same name


Example :

a = 30
def num():
    a=50
    # This line use inside Function Variable
    print("Inside function value of a is",a)

#Calling funtion
num()

# This line use Outside Function Variable
print("Outside function value of a is",a)

Output :

Inside function value of a is 50
Outside function value of a is 30

global keyword

Simply we can't modify the value of a global variable inside a function but by using the global keyword we can modify the value of a global variable inside a function.



Example :

a = 30
def num():
    global a
    a=50
    # This line use inside Function Variable
    print("Inside function value of a is",a)

#Calling funtion
num()

# This line use Inside Function Variable
print("Outside function value of a is",a)

Output :

Inside function value of a is 50
Outside function value of a is 50