-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack-unpack-operators-asterisk-stars.py
44 lines (32 loc) · 1.28 KB
/
pack-unpack-operators-asterisk-stars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Demonstrating the powers of the prefix operators * and **
to pack and unpack iterables/arguments.
Reference: https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/#Asterisks_in_list_literals
"""
import sys
if sys.version_info[0] < 3:
raise Exception("Please use Python 3 or later.")
fruits = ['apple', 'banana', 'coconut', 'durian']
numbers = [1, 2, 3, 4, 5]
print(numbers, fruits, '\n') # [1, 2, 3, ...] ['apple', 'banana', ...]
print(*numbers, *fruits, '\n') # 1, 2, 3, ..., 'apple', 'banana', ...
print([*numbers, *fruits], '\n') # [1, 2, 3, ..., 'apple', 'banana', ...]
def sillyPrint(*items): # "packs" parameters; variable number of arguments!
for item in items:
print(item)
sillyPrint('hi', '1', 'hello', '\n')
# hi
# 1
# hello
a = { 'a': 1 }
b = { 'b': 2 }
c = { **a, **b }
print(c, '\n') # {'a': 1, 'b': 2}
def tag(tag_name, **attributes): # attributes unpacks values from keys
attribute_list = [
f'{name}="{value}"'
for name, value in attributes.items()
]
return f"<{tag_name} {' '.join(attribute_list)}>"
print(tag('a', href="https://www.google.com"), '\n') # '<a href="https://www.google.com">'
print(tag('img', height=20, width=40, src="face.jpg"), '\n') # '<img height="20" width="40" src="face.jpg">'