Join my course at Udemy (Python Programming Bible-From beginner to advanced )

Blogger templates

Thursday 14 May 2020

Python Assignment Statement

Any variable that assigns a literals to a python variable is called assignment statement. 
Following  are some example of assignment statement in python.

               a=10

Above .statement does the following :
- Creates a python variable 'a'
- Assigns a integer object with value = 10
- Make variable 'a' point to the integer object with value = 10.

a=10

Properties

Python assignment has following properties :
1) Assignment creates references
2) Variables are created when it is first assigned. Unlike C,C++ and JAVA, dynamic typing allows python  not to declare the data type when variables are created.

Scenarios where implicit assignment is done are as follows :
     - Module imports
     - Function arguments
     - Class definition

Various types of assignment in Python 

Basic Assignment

Variable assignment deals with basic variable assignment as follows :

               a=10   ## Pointing to integer
               a="hello Python"  ##Pointing to string
               a=10.2   ## Pointing to float.

In python, variable and python object as two distinct entities, variable are just a way to keep track of the corresponding object. Because of this it is possible for a variable to just change references to point to  objects with different datatype. In the above example variable 'a' is first pointing to integer 10, then  to a string "hello Python" and then to a float number 10.2.

Tuple, List and Sequence assignment 

Tuple, List and String assignment take the advantage of positional value of elements in tuple, list and string.
               a, b = 10, 20  ##Tuple assignment
               c,d = [10,20]  ##List assignment
               e,f,g,h,i = "Hello"  ##String assignment

The above assignment syntax assigns the value based on the positional order. For tuple and list assignment, the first value(=10) in list/tuple will go to variable 'a' and second value(=20) of the tuple/list will go to variable 'b'. In the similar way 'e','f','g','h','i' will be assigned the corresponding character from "Hello" string based on position.

Extended Sequence Assignment

In the above tuple, list and string assignment, it is mandatory to match the no of elements on the left side and the right side. If there is mismatch then there will be a error.

                    a,b = "Hello"  ##String assignment ERROR

So the above assignment statement will be error as left side there are just two variables and right side there are 5 characters in the string.

To take care of these error python has provided extended sequence assignment. It allows to use '*' before the variable to assign more than one element (character in case of string) to a variable. We can correct the above example as below :

                    a,*b = "Hello"  ##String assignment ERROR

                    Output
                    >>> a
                    'H'
                    >>> b
                    ['e', 'l', 'l', 'o’]

'a' will be assigned "H" and variable 'b' will be assigned a list of remaining characters.

Multiple Target Assignment

It allows multiple variable to be assigned to a value in one line. Please note that following statement will be assigned to same integer object will value equal to 10.

                    a = b = c = 10  ##Multiple target assignment

Representation of the statement is as shown below. All three variable 'a','b' and 'c' is pointing to same python integer object with value =10. 


a=b=c=10

Augment Assignment

Augment assignment makes the developer life easy by enabling him/her to shorten the statement of addition, subtraction etc. In short a=a+10 can be replaced with augment assignment as below:


                              a += 10

The same functionality is present for other expressions as  -, /, //, %, ** etc. Augment assignment has two advantages over normal assignment :
- Lesser to type
- Augment assignment runs faster than normal assignment.

Augment assignment is faster because it does the 'in-place' changes. Following are the two ways to add new element in list :
                                              L = L + [100, 101] ##Creates new list
                          L += [100,101]  ##Does in place change in existing list.

In the above addition the augment assignment update the new list in the existing list while basic addition creates a new list and appends the list. So one need to be careful when using augment assignment because of in-place changes in augment assignment.

Conclusion 

In this article we have seen python assignment statement and four different types of assignment statement.  One needs to be careful about assignment as many times it may create references.





Share:

0 comments:

Post a Comment

Feature Top (Full Width)

Pageviews

Search This Blog