我的python学习历程

今天是关键字实参,默认值,等效函数调用,以及返回值
1.关键字实参、默认值、以及等效的函数调用
(1)关键字实参
def asd(a,s): //定义一个函数
print("\nw “+a+”.")
print(“b “+s+”.”)
asd(a=‘rt’,s=‘zx’) //对asd中的元素进行赋值

w rt.
b zx.
(2)默认值
def asd(a,s=‘zx’): //先默认asd中一个元素的值
print("\nw “+a+”.")
print(“b “+s+”.”)
asd(a=‘rt’) //在这里再对asd中的另一个元素赋值

w rt.
b zx. //输出结果

(3)等效的函数调用
def asd(a,s=‘zxc’):
asd(‘qwe’)
asd(a=‘qwe’)
asd(‘rty’,‘fgh’)
asd(a=‘rty’,s=‘fgh’)
asd=(s=‘fgh’,a=‘rty’)
不知道为什么,这些代码不能输出结果
def asd(a,s=‘zx’):
asd(‘rt’)
asd(a=‘rt’)
print("\nw “+a+”.")
print(“b “+s+”.”)
asd(a=‘rt’,s=‘zx’)
即使是这么改动了也不能输出结果,试了好几种方法,都是错误的。

2.返回值
(1)返回简单值
def asd(q,w):
z=q+’ '+w
return z.tittle() //在此处将结果返回到函数调用行

xc=asd(‘r’,‘t’) //在这里储存返回的值
print(xc)
Q W
(2)让实参变成可选的
def asd(q,w,e)
z=q+’ ‘+w+’ '+e
return z.title()//在此处将结果返回到函数调用行
rt=asd(‘f’,‘g’,‘h’) //在这里储存返回的值
print(rt)
F G H

def asd(q,w,e)
if e:
rt=q+’ ‘+w+’ ‘+e
else:
rt=q+’ '+w //使用if else来进行判断
fgh=asd(‘u’,‘i’)
print(fgh)

fgh=asd(‘u’,‘i’,‘o’)
print(fgh)
u i
u i o //输出结果

(3)返回字典
def asd(q,w):
zxc=(‘a’:q,‘s’:w)
return zxc //返回表示人的整个字典

qwe=asd(‘y’,‘u’)
print(qwe)
(‘a’:‘y’,‘s’:‘u’)

(4)结合使用函数和while循环
def get_formatted name(first_name, last_ name):
full_name =first_name +’ '+last_name
retuln full_name.title() //定义函数

while True: //使用while循环来判断
print ("\nPlease tell me your name:")
f_ name = input ( “First name:”)
l_name = input( “Last name:”)
formatted_name = get_formatted_name(f_ name, I_name)
print("\nHello,"+formatted_name +"!")
Please tell me your name:
(enter’q’ at any time to quit)
First name:eric
last name:matthes

Hello, Eric Matthes! //输出结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值