Variables in Python

Variables in python are container used to store data values in memory. In python there is no command to declare a varibale. A variable is created in memory when you assign a value to it. Equal sign is used to assign values to variable. The operand left to operator is the variable and the operand right to operator is value of the variable.

Example:--

>>> a = 20
>>> name = 'Rahul Garg'
>>> age = 30
>>> print(name, '----name----')
Rahul Garg ----name----
>>> print('Age- ', age)
Age-  30
>>> 

Python allow to assign single value to multiple valriables. For eg-

>>> a = b = c = 2

It also allow to assain multiple values to multiple variables. For eg-

>>> a, b, c = 2, 7, 8

Variable Naming Conventions:--

1. Variable name cannot start with a numeric digit.

2. Variable name must start with a letter or the underscore character.

3. Variable name can only contain alpha-numeric characters and underscores.

4. Variable names are case-sensitive.

Example:-

Correct way to create variables

>>> my_name = 'Rahul'
>>> Code_base = 'Python'
>>> abc_34 = 'hello'
>>> _code = 'jave'

Incorerct way-

>>> 9_as = 23
  File "<stdin>", line 1
    9_as = 23
     ^
SyntaxError: invalid token

 

Total Views: 414