Python ProgrammingPython Programming

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.

The following program shows how to use string and integer type variables:
x = 10  # Integer variable
y = "Hello World"  # String variable
# Displaying variables value

Get and Check Variable Type

The type() method is used to get the type of a created variable.

This program shows how to check type of a variable:
var1 = 'Hello World'
var2 = 100
if type(var2) == int:
<class 'str'>

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