“In other languages, there are commands to assign/declare variables, but in Python, there is no command to declare variables. We created a variable by assigning a value to it. We use the equal sign (=)
to assign value.” For example:
FirstName = 'Python'
Now here we created a variable. We named it FirstName
and that stores a value Python
. Now this
is stored in Python memory as a variable, and whenever we use this variable, it will show the value it stores. For example:FirstName
FirstName = 'Python'
print(FirstName)
Python
So we get Python
in the output because this FirstName
variable stores Python
as a value. Now, if we have multiple variables.
FirstName = 'Python'
SecondName = 'Java'
ThirdName = 'PHP'
print(SecondName)
Java
Now here we have multiple variables, and every variable stores a value. So when we use print(SecondName)
, we get Java
in the output.
Now what if we update a variable value?
FirstName = 'Python'
print(FirstName)
FirstName = 'Programming'
print(FirstName)
Python
Programming
We get the latest updated value in the output. Because Python interpreter reads code from top to bottom, first it will read that FirstName
variable consists of Python
as a value. So it will print Python in the output. But in the 3rd line, the interpreter reads that now FirstName
consists Programming
as value. So it will print Programming
in the output.
So this is how we create a variable in Python; there is no command. We simply write a variable name, then an equal sign (=), and write a value. We don’t have to declare the value type.