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')

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James Shangguan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值