-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path45-Asterisk_Star_Operator.py
More file actions
42 lines (33 loc) · 953 Bytes
/
45-Asterisk_Star_Operator.py
File metadata and controls
42 lines (33 loc) · 953 Bytes
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
'''
author : Jaydatt
Asterisk Operator:
Multiplication Operator: *
Exponentiation Operator: **
Pack or Unpack Arguments : *args
Variable-Length Keyword Arguments : **kwargs
'''
print('-------Exponentiation-------')
# using asterisk
result = 5 ** 3
print(result)
print('-------Pack Arguments-------')
def print_numbers(*args):
for number in args:
print(number)
print_numbers(1, 2, 3) # Output: 1 2 3
print('-------Unpack Arguments-------')
def add(x, y):
return x + y
numbers = (5, 7)
result = add(*numbers) # result will be 12
print('-------Extended Iterable Unpacking-------')
first, *middle, last = [1, 2, 3, 4, 5]
print(first) # Output: 1
print(middle) # Output: [2, 3, 4]
print(last) # Output: 5
print('-------Variable-Length Keyword Arguments-------')
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name='Alice', age=30)
# Output: name: Alice \n age: 30