About Lesson
A variable in python is a container that stores data values. Python is a type-infer language i.e. it detects the data type of a variable automatically in other words you don’t need to specify the data type of variable.
Note: Python supports unlimited variable length, we keep the maximum length to 79 characters.
Example:
name = 'python' # String Data Type sum = None # a variable without a value x = 23 # integer y = 6.2 # Float sum = x+y print(sum)
Multiple Assignments:
a = b = c = 1 #single value to multiple variables a,b = 1, 2 #multiple value to multiple variable a,b = b,a #value of a and b is swapped
Variable Scope and Lifetime:
- Local Variable
def func(): x=8 print(x) func() print(x) #error will be shown
- Global Variable
x = 8 def fun(): print(x) #Calling variable 'x' inside func() func() print(x) #Calling variable 'x' outside func()