当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
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)
边栏推荐
- Pytoch implements tf Functions of the gather() function
- [Nacos cloud native] the first step of reading the source code is to start Nacos locally
- Yyds dry inventory JS intercept file suffix
- Comprehensive upgrade of Taobao short video photosynthetic platform
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
- How can labels/legends be added for all chart types in chart. js (chartjs.org)?
- mysql拆分字符串做条件查询
- Common commands and basic operations of Apache Phoenix
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
猜你喜欢
2021-12-22 transaction record
SAP UI5 ObjectPageLayout 控件使用方法分享
RHCSA3
Alipay transfer system background or API interface to avoid pitfalls
JDBC -- use JDBC connection to operate MySQL database
国内市场上的BI软件,到底有啥区别
Compilation principle reading notes (1/12)
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
Storage Basics
Tips and tricks of image segmentation summarized from 39 Kabul competitions
随机推荐
Redis cluster configuration
RHCSA4
奔跑,开路
Introduction to relational model theory
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
RHCAS6
I'm doing open source in Didi
自然语言处理系列(一)入门概述
我在滴滴做开源
Didi open source Delta: AI developers can easily train natural language models
Halcon 模板匹配实战代码(一)
非技术部门,如何参与 DevOps?
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
谈谈我写作生涯的画图技巧
《信息系统项目管理师》备考笔记---信息化知识
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
RHCSA2
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
前几年外包干了四年,秋招感觉人生就这样了..
10 minute fitness method reading notes (2/5)