Skip to content

Commit 2967361

Browse files
updated
1 parent 9616df4 commit 2967361

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

34-args-kwargs/another_way.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
def show_kwargs_details(**kwargs):
3+
# Show all keys
4+
print("Keys:")
5+
for key in kwargs.keys():
6+
print(f" {key}")
7+
8+
# Show all values
9+
print("\nValues:")
10+
for value in kwargs.values():
11+
print(f" {value}")
12+
13+
# Show key-value pairs
14+
print("\nKey-Value Pairs:")
15+
for key, value in kwargs.items():
16+
print(f" {key} -> {value}")
17+
18+
19+
# Call the function
20+
show_kwargs_details(name="Alice", age=30, city="Toronto", country="Canada")

34-args-kwargs/args.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# *args = allows you to pass multiple non-key arguments
2+
# **kwargs = allows you to pass multiple keyword-arguments
3+
# * unpacking operator
4+
# 1. positional 2. default 3.keyword 4.ARBITRARY
5+
6+
7+
# def add(a,b):
8+
# return a + b
9+
10+
def add(*args):
11+
total = 0
12+
for arg in args:
13+
total += arg
14+
return sum(args)
15+
16+
# def add(*args):
17+
# print(type(args))
18+
# for arg in args:
19+
# total += arg
20+
# return total
21+
22+
23+
print(add(1,2))
24+
25+
26+

34-args-kwargs/kwargs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# *args = allows you to pass multiple non-keyword arguments
3+
# **kwargs = allows you to pass multiple keyword arguments
4+
# * and ** are unpacking operators
5+
# Argument types:
6+
# 1. positional
7+
# 2. default
8+
# 3. keyword
9+
# 4. arbitrary
10+
11+
def print_address(**kwargs):
12+
print(type(kwargs)) # kwargs is a dict
13+
for key, value in kwargs.items(): # use .items() to get key-value pairs
14+
print(f'{key}: {value}')
15+
16+
print_address(street="Front Street", city="Toronto", state="Ontario", zip="M5V3A4")
17+
18+

34-args-kwargs/main.py

Whitespace-only changes.

0 commit comments

Comments
 (0)