Python Variables

In this tutorial you will learn about the Python Variables and its application with practical example.

What is variable?

Variables is an identifier used to refer memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. Whenever you create a variable in Python programming, this means you are allocating some space in the memory for that variable. The size of memory block allocated and type of the value it holds is completely dependent upon the type of variable. Naming a variable in Python Programming is one of the very important task.

Rules for naming a variable –

  • Variable name can consist of letter, alphabets and start with underscore character.
  • First character of variable name cannot be digit.
  • Keywords are not allowed to use as a variable name.
  • Blank spaces are not allowed in variable name.
  • Python is case sensitive i.e. UPPER and lower case are significant.
  • Special characters like #, $ are not allowed except the underscore.

Recommendation :- Variable name must be readable and should be relative to its purpose.

Variable assignment

In Python it not mandatory too declare a variables beforehand to use it in program. As you assign a value to a variable, the variable is declared automatically. The assignment operator (=) is used to assign values to a variable, any type of value can be assigned to any valid variable.

The operand in the left side of the assignment operator (=) indicates the name of the variable and the operand in the right side of the assignment operator (=) indicates the value stored in that variable.

Example:-

Output:-

Here, we have three assignment statements. In first statement variable “a” is assigned an integer value 5.
Similarly, in the second statement a floating point value 5.5 is assigned to variable “b” and “Hello World” is a string assigned to the variables “c”.

Multiple assignments

In Python, it is possible to do multiple assignments in a single statement as follows –

Output:-

If you want to assign the same value to multiple variables at once, can be done as follows –

Output:-

 

In this tutorial we have learn about the Python Variables and its application with practical example. I hope you will like this tutorial.