您现在的位置是:首页 > 数码 > 

python getvalue函数

2025-07-16 14:01:12
python getvalue函数 1 ##---------------------- 函数 ----------------------2 ##1 定义函数4 defhello_py():5 print( hello,python! )67 hello_py()8 ##1.1向函数传递消息9 defget_message(username):10 print( He

python getvalue函数

1 ##---------------------- 函数 ----------------------

2

##1 定义函数

4 defhello_py():5 print( hello,python! )6

7 hello_py()8 ##1.1向函数传递消息

9 defget_message(username):10 print( Hello, () ! )11 name=input( 请输入你的姓名: )12 get_message(name)1 ##1.2实参和形参

14

15 前面定义函数 get_message() 时,要求给变量 username 指定一个值。调用这个函数并提供这种16 信息(人名)时,它将打印相应的问候语。17 在函数 get_message() 的定义中,变量 username 是一个形参——函数完成其工作所需的一项信18 息。在代码 get_message(name) 中,值 name 是一个实参。实参是调用函数时传递给函数的信19 息。我们调用函数时,将要让函数使用的信息放在括号内。在 get_message(name) 中,将实参20 name的值 传递给了函数 get_message() ,这个值被存储在形参 username 中。21

22 形参和实参位置顺序要一一对应2 函数也可以多次调用24 基本上与C语言一样25

26 ##1.默认值

27

28 编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,Python将使用29 指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数调用中省略0 相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。1

2 def describe_pet(pet_name,animal_type= 哈士奇 ): #显示宠物的信息

4 print( 我在家养过宠物,我给它起名叫做: pet_name)5 print(pet_name 是一只非常好的 animal_type)6 describe_pet( 神奇 )##第二个参数使用默认值

7

8 ###1.4各种调用方法

9 describe_pet( 传奇 )40 describe_pet(pet_name= 传奇 )41 describe_pet( 可可 , 猫 )42 name= 巧巧

4 an_type= 猫

44 describe_pet(name,an_type)##利用别的变量传递

45 ##1.5返回简单值

46 defget_formatted_name(first_name,last_name):47 full_name=last_name first_name48 returnfull_()49 musician=get_formatted_name( 复 , 慕容 )50 print(musician)51 ##1.5让实参变成可选的

52

5 有时候,需要让实参变成可选的,这样使用函数的人就只需在必要时才提供额外的信息。可54 使用默认值为空来让实参变成可选的。55

56 ##返回字典

57 def build_person(first_name,last_name,age= ):##此处利用age默认值为空实现了参数的可选择性

58 ##返回一个在字典,其中包括一个人信息

59 person={ first :first_name, last :last_name}60 ifage:61 person[ age ]=age62 returnperson6 musician=build_person( 锋 , 欧阳 )64 print(musician)65 print( --------------------------------- )66 ##函数和while循环的结合

67 circulation=True##循环变量

68 whilecirculation:69 print( 输入你的名字和姓氏: )70 f_name=input( first_name: )71 l_name=input( last_name: )72 full_name=build_person(f_name,l_name)7 print( 你好 str(full_name[ first ]str(full_name[ last ])))74 print( 后面,还有小朋友吗?(yes/no) )75 answer=input()76 if answer== no or answer== O or answer== n :77 circulation=False78

79 ##传递列表

80 print( ----------------------------------------- )81 defgreet_user(names):82 for name innames:8 msg= hello () !

84 print(msg)85

86 user_list=[ 小明 , 小华 , 杰克 ]87 greet_user(user_list)88 ##在函数中修改列表

89 print( --------------------------------------------- )90

91

92 ##函数后序内容

9

94 将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永95 久性的,这让你能够高效地处理大量的数据。96 来看一家为用户提交的设计制作D打印模型的公司。需要打印的设计存储在一个列表中,97 打印后移到另一个列表中。98

99 defprint_models(unprinted_designs,completed_models):100 ##打印每个设计后,都将其移到列表completed_models中

101 whileunprinted_designs:102 current_design=unprinted_designs.pop()10

104 ##模拟根据要求的打印过程

105 print( 正在打印中的模型是: current_design)106 completed_models.append(current_design)107 defshow_completed_models(completed_models):108 ##显示所有打印好的模型

109 print( 打印完成的模型列表:\n )110 for print_over_models incompleted_models:111 print( \t print_over_models)112

11 unprinted_designs=[ 160_轰炸机 , 米格——k拦截机 , 苏——57战斗机 , F22战斗机 ]114 completed_models=[]115 print_models(unprinted_designs,completed_models)116 print( \n\n )117 show_completed_models(completed_models)118

119 ##禁止函数修改列表(利用列表切片实现)

120 ##例:print_models(unprinted_designs[:],completed_models[:])

121

122 ##传递任意数量的参数

12 def make_pizza(*topping):124 ##其中实参*topping是一个元组,所以可以传递任意数量的参数

125 打印顾客点的所有配料

126 print(topping)127

128 make_pizza( 洋葱 )129 make_pizza( 洋葱 , 鸡蛋 , 面粉 , 培根 )10

11 ##使用任意数量的关键字实参

12 ##其中,下面的**user_info可以理解为一个字典

1 def build_profile(first,last,**user_info):14 创建一个字典,其中包含我们知道的有关用户的一切

15 profile={}16 profile[ first_name ]=first17 profile[ last_name ]=last18 for key,value inuser_info.items():19 profile[key]=value140 returnprofile141

142 user_profile=build_profile( 赵 , 飞燕 ,性别= 女 ,职业= 舞蹈艺术家 )14 print(user_profile)

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/shuma/845581.html

相关标签:无
上传时间: 2024-02-05 11:46:11
留言与评论(共有 5 条评论)
本站网友 水煮毛豆
2分钟前 发表
8 msg= hello () ! 84 print(msg)8586 user_list=[ 小明
本站网友 牛蒡子的功效与作用
18分钟前 发表
19 profile[key]=value140 returnprofile141142 user_profile=build_profile( 赵
本站网友 湖南省地图下载
14分钟前 发表
鸡蛋
本站网友 就让你走
23分钟前 发表
使用默认值可简化函数调用,还可清楚地指出函数的典型用法