![]() When we put tuples on both sides of an assignment operator, a tuple unpacking operation takes place. Check out the following example: > (a, b, c) = ( 1, 2, 3) ![]() This is commonly known as tuple unpacking in Python. The values on the right will be automatically assigned to the variables on the left according to their position in the tuple. In Python, we can put a tuple of variables on the left side of an assignment operator ( =) and a tuple of values on the right side. Let's take a closer look to unpacking in Python and see how this feature can improve our code. Unpacking operations have been quite popular among Python developers because they can make our code more readable, and elegant. However, since this feature has been generalized to all kind of iterable, a more accurate term would be iterable unpacking and that's what we'll call it in this tutorial. Each variable in the tuple can receive one value (or more, if we use the * operator) from an iterable on the right side of the assignment.įor historical reasons, Python developers used to call this tuple unpacking. Python allows a tuple (or list) of variables to appear on the left side of an assignment operation. ![]() In this tutorial, we'll learn what iterable unpacking is and how we can take advantage of this Python feature to make our code more readable, maintainable, and pythonic.Īdditionally, we'll also cover some practical examples of how to use the iterable unpacking feature in the context of assignments operations, for loops, function definitions, and function calls. Nowadays, a more modern and accurate term would be iterable unpacking. However, since this Python feature has turned out to be quite useful and popular, it's been generalized to all kinds of iterables. Historically, Python developers have generically referred to this kind of operation as tuple unpacking. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, *. ![]() Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list) of variables in a single assignment statement. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |