Variables are used to store values in Python. These variables can be used later on in the program for computing purpose.
Variables have a name, value and a memory location in computer.
X = 56
Here X is the Variable Name and 56 is its value.
= is known as assignment operator.
The meaning of this statement is – A value 56 of integer datatype has been assigned to a variable named X.
Another Example :
>>> name = “Rohit”
Here variable name is name
Here value of variable is “Rohit”
<class – str>
How to Print a Variable
Output : Rohit
We can use variables to perform mathematical operations.
>>> x = 56
>>> y = 67
>>> print(x + y)
Output : 123
Variable values can also be changed.
>>> name = “Mohan”
>>> name = “Sohan” # We have changed the previously assigned value.
You can call variables – value-holder.
These values can be of any datatype.
>>> # Valid Variable Names
>>> first_name = “Mohan”
>>> FirstName = “Mohan”
>>> Firstname = “Mohan”
>>> firstName = “Mohan”
>>> # Invalid Variable Names
>>> first name = “Mohan” # using spaces is not allowed in variable names
SyntaxError: invalid syntax
>>> first-name = “Mohan” # using – sign means subtraction so it is not allowed
# No symbol is allowed except _ (underscore)
>>> We cannot start a variable name with number
>>> 1name = “Mohan” # invalid
>>> name1 = “Mohan” # valid