The *
operator in Python is versatile and its meaning depends on the context in which it is used, including multiplication, sequence repetition, argument unpacking, variable-length arguments, unpacking in assignments, keyword-only arguments, merging iterables.
# Multiplying integers
int_result = 5 * 3
print(int_result) # 15
# Multiplying floats
float_result = 2.5 * 4.2
print(float_result) # 10.5
# Multiplying an integer with a float
mixed_result = 3 * 4.5
print(mixed_result) # 13.5
# Repeating a string
string_result = "hello" * 3
print(string_result) # "hellohellohello"
# Repeating a list
list_result = [1, 2, 3] * 2
print(list_result) # [1, 2, 3, 1, 2, 3]
# Repeating a tuple
tuple_result = (1, 2) * 3
print(tuple_result) # (1, 2, 1, 2, 1, 2)
# Unpacking a list into function arguments
def multiply(a, b, c):
return a * b * c
# Define a list
numbers_list = [2, 3, 4]
# Unpack the list into function arguments
result_multiply = multiply(*numbers_list)
print(result_multiply) # 24
# Unpacking a tuple into function arguments
def add(x, y, z):
return x + y + z
# Define a tuple
numbers_tuple = (1, 2, 3)
# Unpack the tuple into function arguments
result_add = add(*numbers_tuple)
print(result_add) # 6
*args
is used to pass a variable number of non-keyword arguments.def sum_all(*args):
total = 0
for number in args:
total += number
return total
# Calling the function with different numbers of arguments
result1 = sum_all(1, 2, 3)
print(result1) # 6
result2 = sum_all(4, 5, 6, 7, 8)
print(result2) # 30
result3 = sum_all(10)
print(result3) # 10
numbers = [1, 2, 3, 4, 5]
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
*
in a function definition, all parameters after *
must be provided as keyword arguments. This means that you must specify the argument names when calling the function. Omitting these keyword specifications or using positional arguments for them will result in a TypeError.def greet(name, *, greeting="Hello", punctuation="!"):
return f"{greeting}, {name}{punctuation}"
# Calling the function with keyword-only arguments
result1 = greet("Alice", greeting="Hi", punctuation="?")
print(result1) # "Hi, Alice?"
result2 = greet("Bob", greeting="Good morning")
print(result2) # "Good morning, Bob!"
result3 = greet("Charlie")
print(result3) # "Hello, Charlie!"
# Uncommenting the following lines will raise a TypeError
# result4 = greet("Dave", "Hi", "?")
# print(result4)
# Define multiple iterables for list merging
list1 = [1, 2, 3]
list2 = [4, 5, 6]
tuple1 = (7, 8, 9)
# Merge iterables into a single list
merged_list = [*list1, *list2, *tuple1]
print(merged_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Define multiple iterables for tuple merging
list3 = [10, 11, 12]
list4 = [13, 14, 15]
tuple2 = (16, 17, 18)
# Merge iterables into a single tuple
merged_tuple = (*list3, *list4, *tuple2)
print(merged_tuple) # (10, 11, 12, 13, 14, 15, 16, 17, 18)