Python Variables


A python variable is a named location used to store data in the memory. Each variable should have a specific name that is called identifier.

In python, the declaration is not required to reserve memory space for the variable. The “variable declaration” or “variable initialisation” occurs when we assign values to a variable.

A python constant is a type of variable whose value cannot be changed. It can be considered as containers that hold information which can’t be changed later.

Python is a case sensitive language means TutorialsDuniya and tutorialsduniya are two different variables.

It is recommended to use lowercase letters for variable names and uppercase letters for constant names.


Naming Conventions

The naming conventions for variables and constants in python are as follows:

  1. First character of the variable must be either an alphabet or an underscore (_) but it cannot be a digit (0-9).
  2. All characters except the first character can be an alphabet, underscore or a digit.
  3. Variable name should NOT contain any white-space or special character (!, @, #, %, ^, &, *).
  4. Variable name must be different from the reserved keywords.
  5. Variable names are case sensitive means Abc and abc are different.
  6. Constants are put into Python modules and meant not be changed.


Examples of valid variable names:

ab21, _td, t_38, tuto65duniya, etc.

Examples of Invalid variable names:

1abc, td%8, ab cd, [email protected], etc.

Python is a dynamically typed language means we are not bound to declare a variable before using that instead it allows us to create a variable at the required time.

The equal (=) operator is used to assign the value to a variable. Name of variable will always be on left side and its value will be on right side.

Example:

    a = 17				        #stores integer
    b = 195.23			        #stores floating point
    c = “tutorialsduniya”		#stores string    


Python allows us to assign a single value to multiple variables in a single statement.

Example:

    a=b=c = 135
    print a                 #prints 135
    print b                 #prints 135
    print c                 #prints 135    


Python allows us to assign a multiple values to multiple variables in a single statement.

Example:

    a,b,c = 70,80,90
    print a			            #prints 70
    print b                             #prints 80
    print c			            #prints 90