Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数
#coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 #2.形参、实参概念和其余语言的概念相同 def greet_user(username): """显示简单的问候语""" print("Hello!"+username) greet_user("Mike") #8.2传递实参 #1.位置实参(注意顺序) def describe_user_info(username,password): """得到用户名和密码""" print("\nUsername:"+username) print("Password:"+password) describe_user_info("admin","123456") describe_user_info("meng","123456") #2.关键字实参(注意形参名) describe_user_info(username="admin",password="123456") #3.默认值 def describe_user_info_1(username,password="123456"): """得到用户名和密码""" print("\nUsername:"+username) print("Password:"+password) describe_user_info_1(username="admin") #4.等效的函数调用 describe_user_info_1('john') describe_user_info_1('john','123456') describe_user_info_1('john','123456') describe_user_info_1(username='john',password='123456') describe_user_info_1(password='123456',username='john') #5.避免实参错误 #8.3返回值 #1.返回简单值 def get_user_info(username,password): """返回用户名和密码""" user_info = username + ' ' +password return user_info xiaoming = get_user_info('xiaoming','999') print(xiaoming) #2.让实参变成可选的 def get_formatted_name(first_name, last_name, middle_name=''): """返回整洁的姓名""" if middle_name: full_name = first_name + ' ' + middle_name + ' ' + last_name else: full_name = first_name + ' ' + last_name return full_name.title() musician = get_formatted_name('jimi', 'hendrix') print(musician) musician = get_formatted_name('john', 'hooker', 'lee') print(musician) #3.返回字典 def build_person(first_name, last_name, age=''): """返回一个字典, 其中包含有关一个人的信息""" person = {'first': first_name, 'last': last_name} if age: person['age'] = age return person player = build_person('Kylian', 'Mbappé', age=19) print(player) #4.结合使用函数和while循环 #8.4传递列表 def greet_users(names): """向列表中的每位用户发出问候""" for name in names: msg = "Hello,"+ name.title() + "!" print(msg) usernames=['kevin','wayne','robert'] greet_users(usernames) #1.在函数中修改列表 #第一个版本 # 首先创建一个列表, 其中包含一些要打印的设计 unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron'] completed_models = [] # 模拟打印每个设计, 直到没有未打印的设计为止 # 打印每个设计后, 都将其移到列表completed_models中 while unprinted_designs: current_design = unprinted_designs.pop() #模拟根据设计制作3D打印模型的过程 print("Printing model: " + current_design) completed_models.append(current_design) # 显示打印好的所有模型 print("\nThe following models have been printed:") for completed_model in completed_models: print(completed_model) #定义函数进行改版 def print_models(unprinted_designs, completed_models): """ 模拟打印每个设计, 直到没有未打印的设计为止 打印每个设计后, 都将其移到列表completed_models中 """ while unprinted_designs: current_design = unprinted_designs.pop() # 模拟根据设计制作3D打印模型的过程 print("Printing model: " + current_design) completed_models.append(current_design) def show_completed_models(completed_models): """显示打印好的所有模型""" print("\nThe following models have been printed:") for completed_model in completed_models: print(completed_model) #主函数看这里,功能一目了然 unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron'] completed_models = [] print_models(unprinted_designs, completed_models) show_completed_models(completed_models) #2.禁止函数修改列表 #有时候需要禁止修改列表。可以将列表的副本传递给函数 function_name(list_name[:]) #在上例中,如果不想清空未打印的设计列表,可以这样调用:print_modules(unprinted_designs[:],complete_modules) #8.5传递任意数量的实参 def greet_magicians(*magicians_names): print(magicians_names) print("*****************greet_magicians*******************") greet_magicians('liu') greet_magicians('liu','qian') #1.结合使用位置实参的任意数量实参 * #2.使用任意数量的关键字实参 ** #编写函数的时候,可以以各种方式混合使用位置实参、关键字实参和任意数量的实参。 #8.6将函数存储在模块中 #1.导入整个模块 #模块是拓展名为.py的文件,包含要导入到程序中的代码。 #import module_name #2.导入特定的函数 #from module_name import function_name #3.使用as给函数指定别名(跟SQL语言中as挺像) #from module_name import function_name as fn #4.使用as给模块指定别名 #import module_name as mn #5.导入模块中的所有函数 #from module_name import * #使用非自己编写的大型模块时,最后不要采用这种导入方法 #8.7函数编写指南 #给函数指定描述性名称,做到见名知意,且只在其中使用小写字母和下划线。 #给形参指定默认值时, 等号两边不要有空格: def function_name(parameter_0, parameter_1='default value')
由于博主也是在攀登的路上,文中可能存在不当之处,欢迎各位多指教! 如果文章对您有用,那么请点个”推荐“,以资鼓励!
欢迎各位加我主页weixin,备注“博客园”,进入技术交流群,和我一起讨论和交流,共同进步!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具