Let’s see what happens when the number of variables exceeds the number of elements in the tuple: coords = 1, 2, 3 Make sure you always have the same number of elements on each side of the assignment operator when unpacking a tuple. The Number of Variables Must Equal to the Number of Elements This assigns the value 1 to x, 2 to y, and 3 to z. To make this work, the number of variables must match the number of items in the tuple.įor example, let’s pick all the 3D coordinate values from a tuple into separate variables x, y, and z: coords = 1, 2, 3 To do this, declare a comma-separated group of variable names and assign it to the tuple. If you need to pick all the values from a tuple into variables, use tuple unpacking. ![]() Here is the syntax for accessing elements from a tuple: tuple_valuesįor example, let’s get the first coordinate of the tuple that represents a 3D point: coords = 1, 2, 3
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |