Python学习笔记-函数

1、定义函数
  • 简单函数
#定义函数
def greet_user():
	print("Hello!")
	
#调用函数
greet_user()
2、传递参数
  1. 简单传递参数
def greet_user(username):	#username这是形参
	'''显示简单的问候语'''
	print("Hello, " + username.title() + "!")

greet_user("feven")			#"feven"这是实参
  1. 位置实参和关键字实参
def describe_pet(animal_type, pet_name):
	"""显示宠物信息"""
	print("I have a " + animal_type + ".")
	print("My " + animal_type + "'s name is " + pet_name.title() + ".")

#位置实参:函数调用中实参的顺序与函数定义中形参的顺序一致
describe_pet('cat','penny')
describe_pet('dog','williw')
#
#关键字实参:显示指出形参-实参对应的内容
describe_pet(animal_type='cat', pet_name='happy')
describe_pet(pet_name='blue', animal_type='dog')
  1. 形参默认值
#形参animal_type的默认值为dog
def describe_pet(pet_name, animal_type = 'dog'):
	print("\nI have a " + animal_type + ".")
	print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet('yoki')
describe_pet('mmmi', 'cat')
#形参默认值的应用—让实参变成可选的
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()
	
name_1 = get_formatted_name('jimi', 'hendrix')
name_2 = get_formatted_name('jimi', 'hendrix', 'DC')
print(name_1)
print(name_2)
  1. 修改/禁止修改参数中的列表
def print_models(unprinted_designs, completed_models):
	'''
	打印设计,将已打印的设计移动到completed_models列表中
	'''
	while unprinted_designs:
		current_design = unprinted_designs.pop()
		
		print("....打印:" + 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的原件,修改了列表的内容	
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

#传递列表unprinted_desings的副本
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs[:], completed_models)
print(unprinted_designs, completed_models)
3、返回值
  1. 返回简单的值
def get_formatted_name(first_name, last_name):
	full_name = first_name + ' ' + last_name
	return full_name.title()
	
name = get_formatted_name('jimi', 'hendrix')
print(name)

4、传递列表
def greet_users(names):
	for name in names:
		msg = "Hello, " + name.title() + "!"
		print(msg)

usernames = ['hhhh', 'heiheihei', 'gogogo']
greet_users(usernames)
5、传递任意数量的实参
  1. 传递任意数量的实参
#*toppings中的*号使python创建一个名为toppings的空元组
def make_pizza(*toppings):
	'''打印顾客点的所有配料'''
	print(toppings)

make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#提取任意数量实参中元组的元素
def print_pizza(*toppings):
	'''概述要制作的披萨'''
	print("\nMaking a pizza with the following toppings: ")
	for topping in toppings:
		print("- " + topping)

print_pizza('pepperoni')
print_pizza('mushrooms', 'green peppers', 'extra cheese')
  1. 传递任意数量的关键字实参
#**user_info表示任意数量的实参是 键值对 形式
def build_profile(first, last, **user_info):
	'''创建一个字典,字典中包含用户的信息'''
	profile = {}
	profile['first_name'] = first
	profile['last_name'] = last
	
	for key, value in user_info.items():
		profile[key] = value
	
	return profile
	
user_profile = build_profile('albert', 'einstein',
							location = 'princeton',
							filed = 'physics')

print(user_profile)
6、将函数存储在模块中
  1. 导入整个模块(调用函数需要用 模块名.函数名 的形式)
#moudle_pizza.py
def make_pizza(size, *toppings):
	print("\nMaking a pizza with the following toppings: ")
	for topping in toppings:
		print("- " + topping)

#moudle_make.py
#导入整个模块,可以使用该模块中的所有函数
import module_pizza

module_pizza.make_pizza(18, 'pepperoni')
module_pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
  1. 导入模块中的特定函数
#moudle_pizza.py
def make_pizza(size, *toppings):
	print("\nMaking a pizza with the following toppings: ")
	for topping in toppings:
		print("- " + topping)

#moudle_make2.py
#从模块module_pizza中导入函数make_pizza
from module_pizza import make_pizza

make_pizza(18, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
  1. 用 as 给从模块中导入的函数指定别名
from module_pizza import make_pizza as mp
mp(18, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
  1. 用 as 给模块指定别名
import module_pizza as pizza

pizza.make_pizza(16, 'mushrooms')
  1. 导入模块中的所有函数(可直接使用函数名)-不推荐
from module_pizza import * 

make_pizza(18, 'mushrooms')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值