A variable is created the moment you first assign a value to it. Variables are nothing but reserved memory locations to store values. It is the basic unit of storage in a program. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
Python variable is also known as an identifier and used to hold value.
Rules for Creating Variables
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (name, Name and NAME are three different variables).
- The reserved words(keywords) cannot be used naming the variable.
Declaring Variable and Assigning Values
The declaration happens automatically when we assign a value to a variable. The equal sign (=) is used to assign values to variables.
Example:
age = 10
name = 'abc'
salary = '10000.50'
print(age)
print(name)
print(salary)
Output:
10 abc 10000.50
Multiple Assignment for Variables
Python allows assigning a single value to several variables simultaneously with “=” operators.
a = b = c = 20
print('a=',a)
print('b=',b)
print('c=',c)