# 2.1. Unpack Assignment¶

## 2.1.1. Rationale¶

• Scalar assignment

• Vector assignment

• Multi assignment

## 2.1.2. Syntax¶

Scalar assignment:

>>> a = 1

Vector assignment:

>>> a, b = 1, 2

Multi assignment:

>>> a = b = 1

>>> a = 1
>>>
>>> print(a)
1

## 2.1.4. Vector Assignment¶

>>> a, b = 1, 2
>>>
>>> print(a)
1
>>>
>>> print(b)
2
>>> a, b = 1, 2
>>> a, b, c = 1, 2, 3
>>> a, b, c, d = 1, 2, 3, 4
>>> a, b, c, d, e = 1, 2, 3, 4, 5

## 2.1.5. Right-Side Brackets¶

Scalar assignments:

>>> a = 1, 2
>>> a = (1, 2)
>>> a = [1, 2]
>>> a = {1, 2}

Vector assignments:

>>> a, b = 1, 2
>>> a, b = (1, 2)
>>> a, b = [1, 2]
>>> a, b = {1, 2}

Rationale:

>>> a, b = 1, 2
>>> a, b = (1, 2)

## 2.1.6. Left-Side Brackets¶

>>> (a) = (1)
>>>
>>> print(a)
1
>>> (a, b) = 1, 2
>>> (a, b) = (1, 2)
>>> (a, b, c) = (1, 2, 3)
>>> (a, b, c) = (1, 2, 3)
>>> (a, b, c) = [1, 2, 3]
>>> [a, b, c] = [1, 2, 3]
>>> [a, b, c] = (1, 2, 3)

## 2.1.7. Errors¶

>>> a, b, c = 1, 2, 3, 4
Traceback (most recent call last):
ValueError: too many values to unpack (expected 3)
>>> a, b, c = 1, 2
Traceback (most recent call last):
ValueError: not enough values to unpack (expected 4, got 3)
>>> {a, b, c} = {1, 2, 3}
Traceback (most recent call last):
SyntaxError: can't assign to literal
>>> {a, b, c} = 1, 2, 3
Traceback (most recent call last):
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?

## 2.1.8. Unpacking¶

>>> data = [1, 2, 3]
>>> a, b, c = data
>>>
>>>
>>> print(a)
1
>>>
>>> print(b)
2
>>>
>>> print(c)
3
>>> line = 'Mark,Watney,44'
>>> firstname, lastname, age = line.split(',')
>>>
>>>
>>> print(firstname)
Mark
>>>
>>> print(lastname)
Watney
>>>
>>> print(age)
44

## 2.1.9. Nested¶

>>> a, (b, c) = [1, (2, 3)]
>>>
>>>
>>> print(a)
1
>>>
>>> print(b)
2
>>>
>>> print(c)
3

## 2.1.10. Skipping Values¶

• _ is regular variable name, not a special Python syntax

• _ by convention is used for data we don't want to access in future

>>> _ = 'Mark Watney'
>>>
>>> print(_)
Mark Watney
>>> line = 'Mark,Watney,1'
>>> firstname, lastname, _ = line.split(',')
>>>
>>>
>>> print(firstname)
Mark
>>>
>>> print(lastname)
Watney
>>> line = 'Mark,Watney,1,2,3'
>>> firstname, lastname, _, _, _ = line.split(',')
>>>
>>>
>>> print(firstname)
Mark
>>>
>>> print(lastname)
Watney

## 2.1.11. Use Case - 0x01¶

• Skip

>>> a, b, _ = 1, 2, 3
>>> a, _, _ = 1, 2, 3
>>> a, _, c = 1, 2, 3
>>> _, b, _ = 1, 2, 3
>>> _, _, c = 1, 2, 3

## 2.1.12. Use Case - 0x02¶

• Passwd

>>> line = 'twardowski:x:1001:1001:Jan Twardowski:/home/twardowski:/bin/bash'
>>> line = line.split(':')
>>>
>>> fullname = line[4]
>>>
>>>
twardowski
>>>
>>> print(fullname)
Jan Twardowski
>>> line = 'twardowski:x:1001:1001:Jan Twardowski:/home/twardowski:/bin/bash'
>>> username, _, _, _, fullname, _, _ = line.split(':')
>>>
>>>
twardowski
>>>
>>> print(fullname)
Jan Twardowski

## 2.1.13. Use Case - 0x03¶

• Important

>>> _, important, _ = 1, 2, 3
>>>
>>> print(important)
2
>>> _, (important, _) = [1, (2, 3)]
>>>
>>> print(important)
2
>>> _, _, important = (True, [1, 2, 3, 4], 5)
>>>
>>> print(important)
5
>>> _, _,  important = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(important)
(5, True)
>>>
>>> _, _, (important, _) = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(important)
5

Python understands this as:

>>> _ = (True, [1, 2, 3, 4], (5, True))
>>>
>>> a,b,c = (object, object, object)
>>> a,b,(c,d) = (object, object, (object,object))

## 2.1.14. Assignments¶

Code 2.11. Solution
"""
* Assignment: Unpack Assignment List
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Split input data
2. Separate ip address and host name
3. Run doctests - all must succeed

Polish:
1. Podziel dane wejściowe
2. Odseparuj adres ip i nazwę hosta
3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert ip is not Ellipsis, \
'Assign result to variable: ip'
>>> assert host is not Ellipsis, \
'Assign result to variable: hosts'
>>> assert type(ip) is str, \
'Variable ip has invalid type, should be str'
>>> assert type(host) is str, \
'Variable hosts has invalid type, should be str'

>>> ip
'10.13.37.1'

>>> host
'nasa.gov'
"""

DATA = ['10.13.37.1', 'nasa.gov']

# str: first string: 10.13.37.1
ip = ...

# str: second string: nasa.gov
host = ...

Code 2.12. Solution
"""
* Assignment: Unpack Assignment Split
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
1. Split input data
2. Separate ip address and host name
3. Run doctests - all must succeed

Polish:
1. Podziel dane wejściowe
2. Odseparuj adres ip i nazwę hosta
3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
* str.split()

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> assert ip is not Ellipsis, \
'Assign result to variable: ip'
>>> assert host is not Ellipsis, \
'Assign result to variable: hosts'
>>> assert type(ip) is str, \
'Variable ip has invalid type, should be str'
>>> assert type(host) is str, \
'Variable hosts has invalid type, should be str'

>>> ip
'10.13.37.1'

>>> host
'nasa.gov'
"""

DATA = '10.13.37.1 nasa.gov'

# str: first string: 10.13.37.1
ip = ...

# str: second string: nasa.gov
host = ...