This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
python3_unpacking [2018/08/06 10:11] jguerin |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Unpacking Sequences ====== | ||
| - | |||
| - | Sequences can be explicitly/ | ||
| - | |||
| - | Unpacking works //only// in instances where the number of variables used //exactly// matches the number of items in the sequence. | ||
| - | |||
| - | In the below examples parenthesis are not required, and are included for clarity. | ||
| - | |||
| - | =====Implicit Unpacking: Member Assignment===== | ||
| - | The most common application of unpacking is to (shallow) copy items into named variables. | ||
| - | |||
| - | ====Lists==== | ||
| - | Unpacking a [[python3_lists|List]]. | ||
| - | <code python> | ||
| - | >>> | ||
| - | >>> | ||
| - | </ | ||
| - | |||
| - | ====Tuples==== | ||
| - | Unpacking a [[python3_tuples|Tuple]]. | ||
| - | <code python> | ||
| - | >>> | ||
| - | >>> | ||
| - | </ | ||
| - | |||
| - | ====Strings==== | ||
| - | Unpacking the characters of a [[python3_strings|String]]. | ||
| - | <code python> | ||
| - | >>> | ||
| - | </ | ||
| - | |||
| - | =====In-Place Swap/Mutual Replacement===== | ||
| - | Swapping objects. | ||
| - | <code python> | ||
| - | >>> | ||
| - | >>> | ||
| - | >>> | ||
| - | </ | ||
| - | |||
| - | |||
| - | =====Explicit Unpacking: The * (Star) Operator===== | ||
| - | The unary '' | ||
| - | |||
| - | This is commonly used when elements of a sequence are intended to be used as the parameters to a function. | ||
| - | |||
| - | E.g., (Assume that '' | ||
| - | |||
| - | ====Unpacking Arguments==== | ||
| - | <code python> | ||
| - | >>> | ||
| - | [1, 2, 3] | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | >>> | ||
| - | 1 2 3 | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | </ | ||
| - | |||
| - | ====Extended Iterable Unpacking==== | ||
| - | |||