Variables in Python


What are the variables?

Variables is a named location in memory that stores a value.
They used to store data values in a program.
Variables can be created by assigning a value to a new or existing variable name using the assignment operator (=).
The value assigned in a variable can be changed during program execution.

Structure to create variable in Python

 variable_name = value

For example:

 x = 5
 y = "Hello World"
From the upside example we can see there are two variables x and y which contain specific type of data.
In the simple language variable is containers which stores values.

There are few rules to follow when naming variables in Python:

  1. Variable names can only contain letters, numbers and underscores (A-z, 0-9, and _).
  2. A variable name must start with a letter or the underscore character.
  3. Variable name cannot start with number.
  4. Variables names should not be the same as keywords in Python.
  5. Variables names should be descriptive and meaningful.

Examples of valid and invalid variables:

Valid variables:

 var = 57
 _name = "Hello World"

Invalid variables:

 1var = 5
 $name = "Hello World"