Python Variables

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 basic parts of a program like a letter or a number.

The examples of such values can be:

Python Values Example
ValueData Type
5, 9integers
Hello, Okstring (combination of letters)

Assigning Values to Variables

Python interpreter can determine that 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 right side operand to left side operand.

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

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

print (name)
print (height)
print (width)
Output:
Packing box
10 
20.5

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

  • 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 variable name.

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.