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/08 10:14] jguerin Added the word "Note". |
— (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. | ||
- | |||
- | **Note:** 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> | ||
- | >>> | ||
- | </ | ||
- | |||
- | =====Input (Console, File)===== | ||
- | Since '' | ||
- | |||
- | The below examples use the [[python3_strings|string]] '' | ||
- | |||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | =====In-Place Swap/Mutual Replacement===== | ||
- | Swapping objects. | ||
- | <code python> | ||
- | >>> | ||
- | >>> | ||
- | >>> | ||
- | </ | ||
- | |||
- | |||
- | =====Explicit Unpacking: The * (Star) Operator===== | ||
- | The unary '' | ||
- | |||
- | E.g., (Assume that '' | ||
- | |||
- | ====Unpacking Arguments==== | ||
- | Consider printing values in a [[python3_lists|List]]. | ||
- | <code python> | ||
- | >>> | ||
- | [1, 2, 3] | ||
- | </ | ||
- | |||
- | In many instances we may wish to print a list's elements, but not formatted as a Python3 '' | ||
- | |||
- | <code python> | ||
- | >>> | ||
- | 1 2 3 | ||
- | </ | ||
- | |||
- | ====Extended Iterable Unpacking==== | ||
- | Extended Iterable Unpacking (([[https:// | ||
- | |||
- | Like regular unpacking for member assignment, extended iterable unpacking is '' | ||
- | |||
- | Note that the following examples make use of Python3 [[python3_ranges|range()]] objects. | ||
- | |||
- | ===Unpacking First and Rest=== | ||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | ===Unpacking Rest and Last=== | ||
- | Note that the '' | ||
- | |||
- | It is also valid to start with the iterable. | ||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | ===Unpacking First, Rest, and Last=== | ||
- | One or more additional named variables can be specified //before// or //after// the '' | ||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | ===Empty Iterables=== | ||
- | Note that the catch-all nature of a starred member does not mean that these values are required. In the event where additional elements are omitted they are treated as an empty sequence. | ||
- | <code python> | ||
- | >>> | ||
- | </ | ||
- | |||
- | |||
- | The number of named elements before and after '' | ||