在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中常用的传递参数的方法,可以根据具体的需求选择合适的方法。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python怎么设置函数注解