Python 函数传递任意数量的实参
函数传递任意数量的实参
- *形参名,形参名中的星号让python创建了一个空元组,并将收到的所有值都封装到这个元组中
# 案例 *toppings 形参名中的星号让python创建了一个空元组,并将收到的所有值都封装到这个元组中
def make_pizza(*toppings):
'''打印顾客点的所有配料'''
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms','green peopers','extra cheese')
('pepperoni',)
('mushrooms', 'green peopers', 'extra cheese')
def make_pizza(*toppings):
"""概述要做的比萨"""
print("\nMaking a pizza with the following toppings: ")
for topping in toppings:
print("- " + topping)
make_pizza('pepperoni')
make_pizza('mushrooms','green peppers','extra cheese')
Making a pizza with the following toppings:
- pepperoni
Making a pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
结合使用位置实参和任意数量实参
- 让函数接收不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后,python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中
def make_pizza(size, *toppings):
print("\nMaking a " + str(size) + "-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
使用任意数量的关键字实参
- 两个星号创建了一个空字典,将收到的所有名称-值对都封装到这个字典中
# 案例
def build_profile(first,last, **user_info):
'''创建一个字典,其中包含我们知道的有关用户的一切'''
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert','einstein',
location = 'princeton',
field = 'physics')
print(user_profile)
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
练习
# 1.三明治
# 编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客
# 点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参
def add_sandwich(*foods):
print("\n I'll make you a great sandwich: ")
for food in foods:
print("....adding " + food + " to your sandwich.")
print("Your sandwich is ready!")
add_sandwich('roast beef', 'cheddar cheese', 'lettuce', 'honey dijon')
add_sandwich('turkey', 'apple slices', 'honey mustard')
add_sandwich('peanut butter', 'strawberry jam')
I'll make you a great sandwich:
....adding roast beef to your sandwich.
....adding cheddar cheese to your sandwich.
....adding lettuce to your sandwich.
....adding honey dijon to your sandwich.
Your sandwich is ready!
I'll make you a great sandwich:
....adding turkey to your sandwich.
....adding apple slices to your sandwich.
....adding honey mustard to your sandwich.
Your sandwich is ready!
I'll make you a great sandwich:
....adding peanut butter to your sandwich.
....adding strawberry jam to your sandwich.
Your sandwich is ready!
# 2.用户简介
# 在其中调用 build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键 - 值对。
def build_profile(first,last, **user_info):
'''创建一个字典,其中包含我们知道的有关用户的一切'''
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('li','yege',
height = '170cm',
weight = '70kg',
hobby = 'basketball',
)
print(user_profile)
{'first_name': 'li', 'last_name': 'yege', 'height': '170cm', 'weight': '70kg', 'hobby': 'basketball'}
# 3.汽车
# 编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可
# 少的信息,以及两个名称 — 值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用
def build_cars(made_address, model, **user_info):
cars = {}
cars['address'] = made_address
cars['model'] = model
for key,value in user_info.items():
cars[key] = value
print(cars)
build_cars('china', 'big',color = 'blue', name = 'changcheng')
{'address': 'china', 'model': 'big', 'color': 'blue', 'name': 'changcheng'}
Python 函数传递任意数量的实参的更多相关文章
- 【Python】向函数传递任意数量的实参
传递任意数量的实参 有时候,你预先不知道函数需要接受多少个实参,好在Python允许函数从调用语句中收集任意数量的实参 def get_letter(*letters): for i in lette ...
- 函数的学习3——传递任意数量的实参&将函数存储在模块——参考Python编程从入门到实践
传递任意数量的实参 形参前加一个 * ,Python会创建一个已形参为名的空元组,将所有收到的值都放到这个元组中: def make_pizza(*toppings): print("\nM ...
- Python 传递任意数量的实参
在定义函数的时候如果你不知道该函数在使用的时候要接收多少的实参怎么办? 好在python提供了可以接收任意数量的实参的操作. # def sandwitch(*ingredents): # print ...
- 传递任意数量的实参*parameter&使用任意数量的关键字实参**parameter
1.*形参名(*parameter) 有时候我们不知道知道函数需要接受多少个实参,所以我们可以在形参名前加一个*,是让python创建一个名为parameter的空元组,并将收到的所有值都封装到这个元 ...
- python_传递任意数量的实参
'''def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中 """打印所有姓名""" for i ...
- python传递任意数量的实参
1.传递任意的实参 def make(*test):#带*号 print(test) make("one")#传递一个实参 make("one","t ...
- Python函数返回不定数量的值
Python的函数是可以return多个值的,但其本质上还是返回单个值,只是利用了tuple的自动打包,将多个值打包成单个tuple返回. 使用代码验证: def func_a(): return 1 ...
- python 函数传递参数的多种方法
python中函数根据是否有返回值可以分为四种:无参数无返回值,无参数有返回值,有参数无返回值,有参数有返回值. Python中函数传递参数的形式主要有以下五种,分别为位置传递,关键字传递,默认值传递 ...
- 小知识:Python函数传递变长
先来实践一把: def f1(*args): print(args) f1(1,2,3,4) 得出:(1, 2, 3, 4) ----- 是一个元祖 Part 1: *args可以传递任意多的数,ar ...
- python 函数传递可变参数的用法
可变参数 在Python函数中,还可以定义可变参数.顾名思义,可变参数就是传入的参数个数是可变的,可以是1个.2个到任意个,还可以是0个. 我们以数学题为例子,给定一组数字a,b,c……,请计算a2 ...
随机推荐
- Realtek 平台一些乱七八糟的编译环境设置
1. 129x 系列之后的就推荐用ubuntu 16.04了 省去一些GCC ,tar,quilt 等问题 Android 平台: sudo apt-get install u-boot-tools ...
- 【前端样式】关于Element-plus 菜单 unique-opened不生效的问题
需要保证el-sub-menu 的唯一性,即index得写在el-sub-menu上,否则 unique-opened 属性不生效.
- 二、pycharm的安装
1.python安装教程在上一篇已描述,详情查看: 2.安装pycharm 首先从网站下载pycharm:链接为:http://www.jetbrains.com/pycharm/download/# ...
- Java反射机制知识
modifier:修饰语 名词 JAVA 反射机制中,Field的getModifiers()方法返回int类型值表示该字段的修饰符. 其中,该修饰符是java.lang.reflect.Modifi ...
- python和java语法对比
python java 不同的关键字 except,nolocal,as,assert,async,pass ,await,from,raise,global,in,del,with,lambda ...
- 整合Swagger2
整合Swagger2 1.Swagger介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web ...
- 👋 和我一起学【Three.js】「初级篇」:0. 总论
「和我一起学 XXX」是我 2023 年的一个新企划,目的是向读者(也包括未来的自己)介绍我正在学习的某项新技术.文章会通过长期反复迭代的方式保持其内容的新鲜度.文章有较大内容更新时,会在文章开头进行 ...
- 开学考--MIS系统(javaweb的开学练习--网络新闻发布系统)
关于本次考试的相关理解 看到题目的时候,第一反应是这道题不难,之前已经做过十分类似的题目了,然后对于难度是很有自信的(当然,对于用户的权限管理部分,还是很懵): 而第二反应就是,题量挺大的,我在这有限 ...
- MySQL查询练习 (转载)
转载 @香草味的橙子 侵删 Evernote Export body, td { font-family: 微软雅黑; font-size: 10pt } mysql查询练习 新建一个查询用的数据库: ...
- Android和adb命令
一.名词解释 1.SDK:是软件开发工具包 2.activity(活动):驱使软件运行的一段程序,软件系统和用户进行交互的界面叫一个活动 二.adb命令 1.查看连接的设备:adb devices 2 ...