What is Assignment and Initialization in Python?
Difference beween Assignment and Initialization in Python
In programming assignment and initialization are two distinct tasks. Wel, for Python Interpreter the Assignment and Initialization is quite the same. Initialization means storing a value in a variable for the first time when it is being forward declared (Forward declaration means declaring a variable – which is essential in many languages).
While Assignment simply means storing a value in a variable, need not be first time.
Assignment and Initialization in Python
Like so many other programming languages Python allows you to create variables and then store and manipulate the values of these variables. Python is dynamic typed language, that makes it quite deifferent from other languages. In Python it is not required to forward declare a variable. But it does distinguish between assigning a value first time or subsequently assigning a value there after.
When you assign a value for the first time, the resources are allocated (memory is allocated and reference is assigned to a variable). The only way python evaluates the type of a variabe is by the value stored in it.
Python is Object Oriented Programming Language
Since Python is an Object Oriented Language, all its components are implemented as objects and thus have associated properties and functionality (behavior).
So during the Assignment (or initialization) in Python what value is assigned to Python variable, largely impacts what all functionality is available that can be exploited programmatically.
Look at the examples below:
X=90 Y=20 print(X+Y) X="Hello" Y="World" print(X+Y)
The output of the program would be:
110
HelloWorld
First two statements are known as an Assignment (or initialization does not matters) statements in Python. Observe the code above, on line number 3 and 6 we are performing the same operation (i.e. X+Y in the print statement) but the outcome is different.
Fourth and Fifth statements are also known as Assignment (or initialization does not matters) statements in Python language. This is simply because first time when integer values were assigned to the variables X and Y it inherited functionality from integer class and performed arithmatic operation (sum of two numbers X and Y).
Second time when String values were assigned to the variables X and Y it inherited functionality from String class and performed String operation (concatination or joining of two strings X and Y).