What is a variable?
a variable is a storage container which can store values, variables are present in every language .we can access, modify and use the values present in the variables. each variable has its own memory location. Variables are classified into a different type, this will be discussed on the data types section.
to create a variable in python ,we should use assign operator(=),syntax is given below
just think x is a box, so now we should store a value in that box, and the value is 66. when python interrupter encounters the syntax, as shown above, a memory block/container is created with name x and 66 is assigned to it as a value
we always write variable name to the left of the assignment operator(=) and value to the right
if we need to change the value of the variable x, we need to declare the same statement with the above syntax with different value
the above example has a value integer, to assign a string to the same variable x, we need to use single quotes or double qoutes, python automatically detects data type, whether it is an integer or string because python is a dynamic programming language
there are some rules for creating variables in python
- name of the python variable should begin with alphabet or _(underscore)
- Only letters (
A-Z) ,numbers (
0-9) ,underscore( _ )are allowed to create variable names, nothing else.
- reserved keywords cant be used
- variables dosen't have finite length
below are some valid variables