在Python中,傳遞參數的方法有以下幾種:
示例:
def greet(name, age):
print("Hello", name, "you are", age, "years old")
greet("Alice", 25)
示例:
def greet(name, age):
print("Hello", name, "you are", age, "years old")
greet(age=25, name="Alice")
示例:
def greet(name, age=18):
print("Hello", name, "you are", age, "years old")
greet("Alice") # 使用默認值18
greet("Bob", 20) # 傳遞新的值20
示例:
def greet(*names):
for name in names:
print("Hello", name)
greet("Alice", "Bob", "Charlie")
示例:
def greet(**info):
for key, value in info.items():
print(key, ":", value)
greet(name="Alice", age=25, city="New York")
以上是Python中常用的傳遞參數的方法,可以根據具體的需求選擇合適的方法。