PYTHON VARIABLES
WHAT IS A PYTHON VARIABLE
- A Python variable is a container to store values.
- It is a name given to a memory location
- We do not need to declare a variable before using them
- It is automatically created when we assign first value to it
NAMING CONVENTION FOR PYTHON VARIABLES
- A variable can contain letters([A-Z] and [a-z]), digits([0-9]) and underscores(_). These are the legal characters while defining python variables.
- Variable name must be starts with a letter. It can not be starts with underscore(_) or digits.
- Variable names are case sensitive
- It should not contain any special characters like +, -, !, @, #, $, %
- It should not contain any reserved python keyword
- Maximum variable name length should not exceed 79 characters
LIST OF RESERVED PYTHON KEYWORD
There are 33 reserved keyword in PYTHON.
In the above list except Ture, False and None all are in lowercase.
VARIABLE ASSIGNMENTS
SINGLE VARIABLE AND SINGLE VALUE
# Variable example
a = 9
b = 9
c = 9
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> 9
Value of b -> 9
Value of c -> 9
MULTIPLE VARIABLES AND SINGLE VALUE
# Variable example
a = b = c = 9
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> 9
Value of b -> 9
Value of c -> 9
MULTIPLE VARIABLES AND MULTIPLE VALUES
Example 1
# Variable example
a, b, c = 9, 10, 12
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> 9
Value of b -> 10
Value of c -> 12
Example 2
# Variable example
a, b, c = 9, "Python", 12.5
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
print("\nData Type of a -> ", type(a))
print("\nData Type of b -> ", type(b))
print("\nData Type of c -> ", type(c))
PYTHON VARIABLES : Output
Value of a -> 9
Value of b -> Python
Value of c -> 12.5
Data Type of a -> <class 'int'>
Data Type of b -> <class 'str'>
Data Type of c -> <class 'float'>
OTHER COMPLEX VARIABLE ASSIGNMENTS
Example 1
# Variable example
a = b = "Python"
c = a + b
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> Python
Value of b -> Python
Value of c -> PythonPython
Example 2
# Variable example
a, b = "Python", "World"
c = a + b
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> Python
Value of b -> World
Value of c -> PythonWorld
Example 3
# Variable example
a, b = "Python", "World"
c = a == b
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> Python
Value of b -> World
Value of c -> False
Example 4
# Variable example
a = b = "Python"
c = a == b
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> Python
Value of b -> Python
Value of c -> True
Example 5
# Variable example
a = "Welcome to"
b = "Python"
print("\nValue of a, b -> ", a, b)
PYTHON VARIABLES : Output
Value of a, b -> Welcome to Python
UPDATING PYTHON VARIABLES
If we use the same variable multiple times for any assignment then the latest assignment will replace the previous assignment.
# Variable example
a = "Python"
b = "Python"
a = "Welcome to"
c = a == b
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\nValue of c -> ", c)
PYTHON VARIABLES : Output
Value of a -> Welcome to
Value of b -> Python
Value of c -> False
Initially a was containing Python but later we have updated the value to Welcome to.
INCREMENTING BEFORE UPDATING
Example 1
# Variable example
a = 10
b = 12
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a += b
print("\nValue of a += b -> ", a)
PYTHON VARIABLES : Output
Value of a -> 10
Value of b -> 12
Value of a += b -> 22
Example 2
# Variable example
a = 12
print("\nValue of a -> ", a)
a += 5
print("\nValue of a += 5 -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of a += 5 -> 17
DECREMENTING BEFORE UPDATING
Example 1
# Variable example
a = 12
b = 10
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a -= b
print("\nValue of a += b -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of b -> 10
Value of a += b -> 2
Example 2
# Variable example
a = 12
print("\nValue of a -> ", a)
a -= 5
print("\nValue of a -= 5 -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of a -= 5 -> 7
MULTIPLYING BEFORE UPDATING
Example 1
# Variable example
a = 12
b = 3
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a *= b
print("\nValue of a *= b -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of b -> 3
Value of a *= b -> 36
Example 2
# Variable example
a = 12
print("\nValue of a -> ", a)
a *= 5
print("\nValue of a *= b -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of a *= b -> 60
DIVIDING BEFORE UPDATING
Example 1
# Variable example
a = 12
b = 3
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
a /= b
print("\nValue of a /= b -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of b -> 3
Value of a /= b -> 4.0
Example 2
# Variable example
a = 12
print("\nValue of a -> ", a)
a /= 5
print("\nValue of a /= 5 -> ", a)
PYTHON VARIABLES : Output
Value of a -> 12
Value of a /= 5 -> 2.4
VARIABLE SCOPE
There are 3 scopes available in PYTHON.
- GLOBAL
- NONLOCAL
- LOCAL
GLOBAL VARIABLE
if a variable is marked as global then it can be used anywhere inside the program.
Global variable is identified by the keyword GLOBAL.
# Global Variable example
a = 12
def func_test():
a = 10
print("\nValue of a inside func_test", a)
func_test()
print("\nValue of outer a", a)
PYTHON VARIABLES : Output
Value of a inside func_test 10
Value of outer a 12
In the above example we have used same variable a both inside and outside the function. Both have different values as they are referring different variable.
# Global Variable example
a = 12
def func_test():
global a
a = 10
print("\nValue of a inside func_test", a)
func_test()
print("\nValue of outer a", a)
PYTHON VARIABLES : Output
Value of a inside func_test 10
Value of outer a 10
In the above example we have called the outer variable a inside the function by using the global keyword. Now both have the same value 10 as it is updated inside the function.
NONLOCAL VARIABLE
NONLOCAL VARIABLES are used in nested structure only. They are neither global nor local.
# Nonlocal Variable example
a = 12
def func_test():
a = 10
print("\nValue of a inside func_test", a)
def func_local():
nonlocal a
print("Value of nonlocal a inside func_local ->" , a)
a = 15
print("Value of nonlocal a inside func_local after updation ->" , a)
func_local()
print("Value of a inside fun_test but outside func_local ->" , a)
func_test()
print("\nValue of a outside func_test", a)
PYTHON VARIABLES : Output
Value of a inside func_test 10
Value of nonlocal a inside func_local -> 10
Value of nonlocal a inside func_local after updation -> 15
Value of a inside fun_test but outside func_local -> 15
Value of a outside func_test 12
In the above example we have defined a variable a outside the function func_test. Then inside function func_test we have defined another function func_local.
We have used the same variable a inside the function func_test but its local to func_test. Then again we have used the same variable a inside the function func_local. But we have marked it as a nonlocal variable. That means its local to func_test but global to func_local. Then we have updated the value of a inside func_local and its reflected outside func_local as well. Also we have printed the outer most variable a which has no impact of func_local.
LOCAL VARIABLE
Any variable defined inside a function are by default local variable until they are explicitly marked as global.
# Local Variable example
a = 12
print("\nValue of a ", a)
def func_test():
a = 10
print("\nValue of a inside func_test", a)
func_test()
print("\nValue of a outside func_test", a)
PYTHON VARIABLES : Output
Value of a 12
Value of a inside func_test 10
Value of a outside func_test 12
VARIABLE DELETION
- Variable can be deleted by using del command.
- Once a variable is deleted it can not be accessed.
- Python will raise error if you try to access a variable post deletion.
# Local Variable example
a = 12
print("\nValue of a ", a)
del a
print("\nValue of a after deletion ", a)
PYTHON VARIABLES : Output
Value of a 12
Traceback (most recent call last):
File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 12, in <module>
print("\nValue of a after deletion ", a)
^
NameError: name 'a' is not defined
RELATED TOPICS: