4 Dec 2020

Tuple assingment

* An assignment to all of the elements in a tuple using a single assignment statement. 

* Python has a very powerful tuple assignment feature that allows a tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment. The left side is a tuple of variables; the right side is a tuple of values.

* Each value is assigned to its respective variable.

* All the expressions on the right side are evaluated before any of the assignments.

* This feature makes tuple assignment quite versatile. 

* Naturally, the number of variables on the left and the number of values on the right have to be the same. 

Example : swap of two variables


Tuple packing & Tuple unpacking :

  * In tuple packing, the values on the left side are packed together in a tuple. In tuple unpacking, the values on the right are unpacked in the variables/names.

Example :
 >>b = ("Ben",18,"30000") #tuple packing
>>(name,age,salary) = b #tuple unpacking
>>name
"Ben"
>>age
18
>>salary
"30000"

    




No comments:

Post a Comment

Operator precedence

           When an expression contains more than one operator, the order of evaluation depends on the order of operations.            The ac...