Python Variables

Python Variables
Vriables are the names that are used to refer to a specific memory location. Variables are also known as identifiers and the variabls hold the values assignes to them. Generally a variable cannot hold more than one value at the same time.

In python programming language, we need not to specify the data type of the variable with it as python is a type infer and smart enough language that itself get the data type of the variable on tha basis of the value assigneed to the variable.

We can name a variable as we want such as a group of letters or alphabets and/of groups of letters and digits but, it should always be kept in mind that the name of variable should always start with the letter or underscore.
It should also be kept in mind that the uppercase and the lowercase letter represent diffrenet varialbes i.e. 'A' and 'a' represent two different variables.

Identifier Naming
A variable is an example of an identifier. An identifier is used to identify the literals that are used in the program. The rules that are used to name an identifier are as follows:

  • The first character of the variable name 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 : abc, a123, _a, a_9, etc.
  • Examples of invalid identifiers: 5a, a@6, a 9, etc.

Declaring Variable and Assigning Values
If we are using python programming language then there is no need to declare the variable before using that in the application. We can create the variables at the time when required.

In the programming laguage python we do not need to declare the variables explicitely i.e. when we assign any value to to any variable then that variable is automatically declared.

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

>>>a=10

>>>print(a)

10



Multiple Assignment
In python , we can assign the values to multiple variables withion a single statement and this is known as multiple assignment. So, there is no need to distictily assign the values to different variables.

Multiple assignment in python can be done in two ways, first is that multiple variables can be assignes a single value and the second way is to assign multiple values to multiple variables. Let's see the following example:

1. Assigning single value to multiple variables:

>>>a=b=c=500

>>>print(a,b,c)

500 500 500

2.Assigning multiple values to multiple variables:

>>>a,b,c=100,200,300

>>>print(a,b,c)

100 200 300