A variable is a name that is used to refer to a memory location. Variables in Python are used to store data and are also referred to as "identifiers."

Since Python is an inferring language and intelligent enough to determine the type of a variable, we don't need to specify it.

Variable names must start with a letter or an underscore and can contain both letters and numbers.

It is recommended to use lowercase letters for the variable name. Orange and orange are both two different variables.

In a Python program, values are stored in variables, which are designated areas of memory. This implies that you set aside some memory when you create a variable.

The Python interpreter decides how much memory needs to be allocated and what can be stored in the allocated memory based on the data type of a variable. Therefore, you can store integers, decimals, or characters in Python variables by giving them different data types.

Identifier Naming

Variables are an example of identifiers. The literals used in the program are designated by an Identifier. Below are the guidelines for naming an identifier.

    • The variable's first character must be an alphabetic character or an underscore (_).
    • All the characters—aside from the first one—can be either a digit (0–9), an alphabet of lowercase letters (a–z), uppercase letters (A–Z), or underscores.
    • No white space or special characters (!, @, #,%,, &, *) are allowed in the identifier name.
    • The name of the identifier cannot be similar to any language-defined keyword.
    • Names used as identifiers are case-sensitive; for instance, my name and MyName are not the same.

    Examples of acceptable identifiers include a123, _n, n_9, etc.

    Invalid identifiers include 1a, n%4, n 9, etc.

    Creating Python Variables

    When you want to create a variable in Python, you don't need to explicitly declare it first. When you assign a value to a Python variable, it automatically creates one. Assigning values to variables is done with the equal sign (=).

    The name of the variable is the operand to the left of the = operator, and the value that is stored in the variable is the operand to the right of the = operator. For illustration,

    range = 100          # Creates an integer variable
    volume = 1000.0       # Creates a floating point variable
    staff = "John Doe"  # Creates a string variable

    Printing Python Variables

    The print() function can be used to output a Python variable after we've created it and given it a value. The example that follows continues the previous one and demonstrates how to print various variables in Python:

    range = 100          # Creates an integer variable
    volume   = 1000.0       # Creates a floating point variable
    staff= "John Doe"   # Creates a string variable
    print (range)
    print (volume)
    print (staff)

    Here, the range, volume, and staff variables are each given the values 100, 1000.0, and "John Doe," respectively. Running the Python program mentioned above results in the output shown below:

    100
    1000.0
    John Doe

    Delete a Variable

    Using the del statement, you can remove the reference to a number object. The del statement has the following syntax:

    del var1[,var2[,var3[....,varN]]]]

    The del statement allows you to delete one or more objects at once. For example:

    del var
    del var_a, var_b

    Example

    The examples below demonstrate how to delete a variable, and the Python interpreter will throw an error if you attempt to use a deleted variable:

    counter = 100
    print (counter)
    del counter

    print (counter) # printing the deleted variable

    This will produce the following result:

    100
    Traceback (most recent call last):
      File "main.py", line 7, in <module>
        print (counter)
    NameError: name 'counter' is not defined

    Multiple Assignment

    You can create multiple variables at once in Python because it allows you to assign a single value to many variables at once. Consider this:

    a = b = c = 100
    print (a)
    print (b)
    print (c)

    The outcome is as follows:

    100
    100
    100

    Here, the three variables are all assigned to the same memory location along with the creation of an integer object with the value 1. Additionally, you can link several objects to various variables. For instance,

    a,b,c = 1,2,"Jane Doe"
    print (a)
    print (b)
    print (c)

    This produces the following result:

    1
    2
    Jane Doe

    Here, variables a and b are each given two integer objects with the values 1 and 2, and the string object "Jane Doe" is given to variable c.

    Local Variable in Python

    Local variables in Python are defined within a function. Outside of the function, we are unable to access a variable.

    Here is an illustration of how to use local variables:

    def sum(x,y):
       sum = x + y
       return sum
    print(sum(5, 10))
    15

    Global Variable in Python

    Any variable created outside of a function has global scope because it is possible to access it from inside of any other function. Here is an illustration of a global variable:

    x = 5
    y = 10
    def sum():
       sum = x + y
       return sum
    print(sum())

    This will produce the following result:

    15

    Related Articles and Resources

    How To Install Python

    In order to become Python developer, the first step is to learn how to install or update Python on a local machine or computer. In …

    Python Features

    Python differs from other programming languages in terms of popularity and value because it offers a number of practical features. It supports procedural and object-oriented …

    Python - Basic Syntax

    The Python syntax defines a set of rules that are used to create Python statements while writing a Python program. The Python Programming Language Syntax …

    Python - Comments

    Python comments are programmer-readable explanations or annotations in the Python source code. They are added with the purpose of making the source code easier for …

    Plot Correlation Matrix In Python

    Given two variables, if the value of one variable is dependent on the value of the other variables, we say the variables are related. The …

    Python Literals

    Python Literals can be defined as data that is given in a variable or constant.Python supports the following literals:String literals:String literals can be formed by …

    Python Data Types

    Python data types are used to specify a variable's type. It specifies the kind of information that will be kept in a variable. Different kinds …

    Python Tutorial

    This Python tutorial explains both basic and advanced Python concepts. This Python tutorial is intended for both beginners and experts. You'll have a strong foundation …

    Trusted by digital leaders and practitioners from 100+ International Organizations