User Tools

Site Tools


python3_unpacking

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python3_unpacking [2018/08/06 09:48]
jguerin
— (current)
Line 1: Line 1:
-====== Unpacking Sequences ====== 
- 
-Sequences can be explicitly/implicitly //unpacked// into discrete, ordered units of some type. Unpacking happens automatically using the '','' (comma). (The syntax is also used for [[python3_tuples|Tuple]] construction.) 
- 
-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. 
- 
-=====Lists/Tuples/Strings=====  
-Unpacking a [[python3_lists|List]] 
-<code python> 
->>> seq = [1, 2, 3] 
->>> (a, b, c) = seq  # a=1, b=2, c=3 
-</code> 
- 
-=====In-Place Swap/Mutual Replacement===== 
-Swapping objects. 
-<code python> 
->>> x = 1 
->>> y = 2 
->>> (x, y) = (y, x) # x=2, y=1 
-</code> 
- 
- 
  
python3_unpacking.1533566891.txt.gz ยท Last modified: 2018/08/06 09:48 by jguerin