Variables are identifiers of a physical memory location, which is used to hold values temporarily during program execution.

Python interpreter allocates memory based on the values data type of variable, different data types like integers, decimals, characters, etc. can be stored in these variables.

What are Values?

Before learning about variables, you must know about values.

A value is one of the essential parts of a program, like a letter or a number.

The examples of such values can be:

Value Data Type
5, 9 integers
Hello, Ok string (a combination of letters)

Python Variable Declaration

In Python, like many other programming languages, there is no need to define variables in advance. As soon as a value is assigned to a variable, it is automatically declared. This is why Python is called a dynamically typed language.

The syntax for creating variables in Python is given below:

<variable-name> = <value>

Assigning Values to Variables

Python interpreter can determine what type of data is stored, so before assigning a value, variables do not need to be declared.

Usually, in all programming languages, equal sign = is used to assign values to a variable. It assigns the values of the right side operand to the left side operand.

The left side operand of = operator is the name of a variable, and the right side operand is value.

name = "Packing box" # A string
height = 10 # An integer assignment
width = 20.5 # A floating point

print (name)
print (height)
print (width)
Packing box

In the above code snippet, the variable name 'height' is storing a value 10, and since the value is of type integer, the variable is automatically assigned the type integer.
Another variable name 'width' is assigned with floating type value. Then both the values are printed or displayed using the 'print' statement.

Commons Rules for Naming Variables in Python

Python has some variable related rules that must be followed:

  • Variable names are case-sensitive.
  • Variable names must begin with a letter or underscore.
  • A variable name can only contain alphanumeric characters and underscore, such as (a-z, A-Z, 0-9, and _ ).
  • A variable name can not contents space.
  • Reserved words cannot be used as a variable name.

Python Variable Deletion

Python also provides the facility to delete a variable from memory. For this, the del command is used. The following is the general syntax for deleting a variable in Python:

del <variable-name>