Python Variables

variable in python

Variable is a name that is used to refer to memory location. Python Variables is also known as an identifier and used to hold value.

In Python, we don’t need to specify the type of variable because Python is a infer language and smart enough to get variable type.

Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.

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

Identifier Naming

Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.

  • The first character of the variable must be an alphabet or underscore ( _ ).
  • All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9).
  • Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
  • Identifier name must not be similar to any keyword defined in the language.
  • Identifier names are case sensitive; for example, my name, and MyName is not the same.
  • Examples of valid identifiers: a123, _n, n_9, etc.
  • Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values

Python does not bind us to declare a variable before using it in the application. It allows us to create a variable at the required time.

We don’t need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically.

The equal (=) operator is used to assign value to a variable.

Object References

It is necessary to understand how the Python interpreter works when we declare a variable. The process of treating variables is somewhat different from many other programming languages.

Python is the highly object-oriented programming language; that’s why every data item belongs to a specific type of class.





The Python object creates an integer object and displays it to the console. In the above print statement, we have created a string object. Let’s check the type of it using the Python built-in type() function.




<class 'str'>

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously.

For example −

a = b = c = 1

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables.

 For example −

a,b,c = 1,2,"john"

Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value “john” is assigned to the variable c.

Recommende Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *