Bài tập Args & Kwargs - Cơ bản
Phần 1: *args
Phần tiêu đề “Phần 1: *args”- Viết hàm
sum_all(*numbers)tính tổng tất cả các số.
def sum_all(*numbers): pass
# Testprint(sum_all(1, 2, 3)) # 6print(sum_all(1, 2, 3, 4, 5)) # 15- Viết hàm
multiply_all(*numbers)nhân tất cả các số.
def multiply_all(*numbers): pass
# Testprint(multiply_all(2, 3, 4)) # 24- Viết hàm
find_max(*numbers)tìm số lớn nhất.
def find_max(*numbers): pass
# Testprint(find_max(1, 5, 3, 9, 2)) # 9- Viết hàm
concat_strings(*strings)nối các chuỗi.
def concat_strings(*strings): pass
# Testprint(concat_strings("Hello", " ", "World")) # "Hello World"- Viết hàm
average(*numbers)tính trung bình cộng.
def average(*numbers): pass
# Testprint(average(10, 20, 30)) # 20.0Phần 2: **kwargs
Phần tiêu đề “Phần 2: **kwargs”- Viết hàm
print_info(**kwargs)in thông tin dạng key: value.
def print_info(**kwargs): pass
# Testprint_info(name="Alice", age=25, city="Hanoi")- Viết hàm
create_user(**user_data)tạo dictionary user.
def create_user(**user_data): pass
# Testprint(user)- Viết hàm
count_kwargs(**kwargs)đếm số keyword arguments.
def count_kwargs(**kwargs): pass
# Testprint(count_kwargs(a=1, b=2, c=3)) # 3- Viết hàm
get_value(key, **kwargs)lấy giá trị theo key.
def get_value(key, **kwargs): pass
# Testprint(get_value("name", name="Alice", age=25)) # "Alice"- Viết hàm
filter_kwargs(**kwargs)chỉ giữ values > 0.
def filter_kwargs(**kwargs): pass
# Testresult = filter_kwargs(a=5, b=-3, c=10, d=0)print(result) # {'a': 5, 'c': 10}Phần 3: Kết hợp *args và **kwargs
Phần tiêu đề “Phần 3: Kết hợp *args và **kwargs”- Viết hàm
flexible_function(*args, **kwargs)in cả args và kwargs.
def flexible_function(*args, **kwargs): pass
# Testflexible_function(1, 2, 3, name="Alice", age=25)- Viết hàm
calculate(operation, *numbers, **options)thực hiện phép tính.
def calculate(operation, *numbers, **options): # operation: "add", "multiply", etc. # options có thể chứa "round": True pass
# Testprint(calculate("add", 1, 2, 3, 4)) # 10print(calculate("multiply", 2, 3, 4)) # 24- Viết hàm
format_message(template, *args, **kwargs)format chuỗi.
def format_message(template, *args, **kwargs): pass
# Testmsg = format_message("Hello {name}, you are {age} years old", name="Alice", age=25)print(msg)- Viết hàm
build_url(base, *paths, **params)tạo URL.
def build_url(base, *paths, **params): # base: "https://example.com" # paths: ["api", "users", "123"] # params: {"limit": 10, "page": 2} # Result: "https://example.com/api/users/123?limit=10&page=2" pass
# Testurl = build_url("https://api.com", "users", "123", limit=10, page=2)print(url)- Viết hàm
log_message(level, *messages, **config)ghi log.
def log_message(level, *messages, **config): # level: "INFO", "ERROR", etc. # messages: các thông báo # config: timestamp=True, file="app.log" pass
# Testlog_message("INFO", "User", "logged in", timestamp=True)Phần 4: Unpacking
Phần tiêu đề “Phần 4: Unpacking”- Dùng
*unpack list để gọi hàmadd(a, b, c).
def add(a, b, c): return a + b + c
numbers = [1, 2, 3]# Code của bạn ở đây- Dùng
**unpack dictionary để gọi hàmgreet(name, age).
def greet(name, age): return f"Hello {name}, you are {age}"
person = {"name": "Alice", "age": 25}# Code của bạn ở đây- Viết hàm
merge_dicts(*dicts)merge nhiều dictionaries.
def merge_dicts(*dicts): pass
# Testdict1 = {"a": 1, "b": 2}dict2 = {"c": 3}dict3 = {"d": 4}result = merge_dicts(dict1, dict2, dict3)print(result) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}- Viết hàm
combine_lists(*lists)kết hợp nhiều lists.
def combine_lists(*lists): pass
# Testlist1 = [1, 2, 3]list2 = [4, 5]list3 = [6, 7, 8, 9]result = combine_lists(list1, list2, list3)print(result) # [1, 2, 3, 4, 5, 6, 7, 8, 9]- Viết hàm
call_function(func, *args, **kwargs)gọi function với args và kwargs.
def call_function(func, *args, **kwargs): # Gọi func với args và kwargs pass
# Testdef sample_func(a, b, c=10): return a + b + c
result = call_function(sample_func, 1, 2, c=20)print(result) # 23