当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
2022-07-05 12:40:00 【南湖渔歌】
''' 编写如下函数: 函数名字,叫做computer 意思是计算 参数分别是x,y,method,代表数字x,数字y,字符串method 如果method==add字符串,返回x+y 如果method==sub字符串,返回x-y 如果method==mul字符串,返回x*y 如果method==div字符串,返回x/y method设置成带默认值的参数,默认为默认加法 分别用如下方式调用调用参数 位置参数 关键词参数和不带默认值参数调用 '''
def compute(x,y,method = 'add'):
if method == 'add':
print(f'{
x}+{
y}',x+y)
elif method == 'sub':
print(f'{
x}-{
y}',x-y)
elif method == 'mul':
print(f'{
x}*{
y}', x * y)
elif method == 'div':
print(f'{
x}/{
y}', x / y)
compute(3,4,'add')
compute(3,4,'sub')
compute(3,4,'mul')
# 如果想添加关键词参数,必须放在最后
compute(3,y=4,method='div')
# compute(3,y=4,'sub') # error:位置参数位于关键字实参后面会报错
compute(3,4)
compute(x=3,y=4)
边栏推荐
- jxl笔记
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- RHCSA1
- Kotlin variable
- 单独编译内核模块
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- Compilation principle reading notes (1/12)
- Rasa Chat Robot Tutorial (translation) (1)
- Pytorch two-layer loop to realize the segmentation of large pictures
- Taobao flag insertion remarks | logistics delivery interface
猜你喜欢
UNIX socket advanced learning diary - advanced i/o functions
HiEngine:可媲美本地的云原生内存数据库引擎
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
JDBC exercise - query data encapsulated into object return & simple login demo
What is the difference between Bi software in the domestic market
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
激动人心!2022开放原子全球开源峰会报名火热开启!
Transactions from January 6 to October 2022
Kotlin variable
随机推荐
单独编译内核模块
你的下一台电脑何必是电脑,探索不一样的远程操作
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
DNS的原理介绍
Simply take stock reading notes (1/8)
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
在家庭智能照明中应用的测距传感芯片4530A
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
RHCSA4
How do e-commerce sellers refund in batches?
SAP SEGW 事物码里的 Association 建模方式
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
Common commands and basic operations of Apache Phoenix
SAP self-development records user login logs and other information
Using docker for MySQL 8.0 master-slave configuration
Tips and tricks of image segmentation summarized from 39 Kabul competitions
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
View and terminate the executing thread in MySQL