Python Variables
In this tutorial you will learn, about variables and variable assignments in Python programming.
Variables are used to store data, like string, numbers, date-time etc. When you create a variable, the interpreter will reserve some space in the memory to store value of variable. Value of a variable can change during the execution of a python program.
Creating Variables
In Python, a variable does not need to be declared while creating or before adding a value to it. Python variables are usually dynamically typed, that is, the type of the variable is interpreted during run-time and you don't need to specify the type of a variable.
x = 10 # Integer variable y = "Hello World" # String variable # Displaying variables value print(x) print(y)
Get and Check Variable Type
The type() method is used to get the type of a created variable.
var1 = 'Hello World' print(type(var1)) var2 = 100 if type(var2) == int: print('Integer')
<class 'str'> Integer
Check if Variable Exist
The locals() usually used to check the existence of a local variable.
var1 = 200 if 'var1' in locals(): print("Variable Exist")
Variable Exist