Variable in python is a name given to a value or it can be considered as a container for storing data values assigned to it.
eg: a=3
The value 3
is assigned to the variable a
. Thus when this statement gets executed, the variable a
is associated with the value 3
.
In python, variables are often called names.
Rules for naming a variable
- A variable must begin with an alphabet or an underscore ( _ ).
- A variable name cannot have any other character except for alphabets, digits or an underscore.
Python is case sensitive. Thus, age
and AGE
are different variables.
Some valid variable names are:
- name
- marks
- _age
- a1
- temp_1
Some invalid variable names are:
- total. : ends with a dot
- 1age : starts with a digit
- amount$ : use of $ character
- new file : space between two words
- file : starts with character
Assignment statement
An assignment statement binds or associates a variable to an object (value) and is often called association.
SYNTAX
variable = expression
Here expression can be either a value like age=20
or a string like name = 'Kanika'
.
We can have multiple assignments in a single statement, each assignment separated by a comma ( , ).
date = 08
month = 'Nov'
year = '2020'
can be written as
date , month , year = 08 , 'Nov' , '2020'
Moreover, same values can be assigned to multiple variable in a single statement.
count = 0
mark = 0
can be written as
count = mark = 0
These notations are called Shorthand Notations and are used to enhance the readability of the program.
Another form of writing this notation is
a = a <operator> b => a <operator> = b
for example
a = a + b => a + = b
Keywords
Keywords in python are those reserved words that are already defined and have a special meaning. They cannot be used as identifiers or for naming any other objects in a program. The following is a list of python keywords.
Stay tuned for more python notes!!