当前位置: 移动技术网 > IT编程>脚本编程>Python > 第8章 函数 4-8节

第8章 函数 4-8节

2020年01月13日  | 移动技术网IT编程  | 我要评论

答辩ppt下载,武汉经济,德州一中招聘

# 8.4.1 在函数中修改列表
def greet_user(names):
  '''向列表中的每位用户都发出简单的问候'''
  for name in names:
    msg = "hello, " + name.title() + "!"
    print(msg)

usernames = ['star','james','wendy']
greet_user(usernames)

# 不使用函数修改列表
# 首先创建一个列表,其中要包含一些打印的设计
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中
  '''
  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 hvae 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)

# 8.4.2 禁止函数修改列表——function_name(lsit_name[:]),切片表示法[:]创建列表的副本
def print_models(unprinted_designs, completed_models):
  '''
  模拟打印每个设计,直到没有未打印的设计为止
  打印每个设计后,都将其移到列表completed中
  '''
  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 hvae 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)

 

# 8-9 魔术师
def show_magicians(names):
  '''呈现列表中的名字'''
  for name in names:
  msg = "welcome international magician, " + name.title() + "!"
  print(msg)

usernames = ['james','wendy','star']
show_magicians(usernames)

# 8-10 了不起的魔术师
def show_magicians(list_name, modify_infor):
  '''
  修改列表中的每个信息,直到修改完成
  修改后的信息,都将其移到modify_infor中
  '''
while list_name:
  current_names = list_name.pop()
# 修改信息的过程
  print("\nmodify information: " + current_names)
  modify_infor.append(current_names)

def make_great(modify_info):
  '''显示修改好的信息'''
  print("\nthe following information have benn modified:")
  for modify_info in modify_infor:
    print("the great magician is " + modify_info)

list_name = ['james','star','wendy']
modify_infor = []

show_magicians(list_name,modify_infor)
make_great(modify_infor)
print(list_name) # 原列表信息已经彻底变空

# 8-11 不变的魔术师
def show_magicians(list_name, modify_infor):
  '''
  修改列表中的每个信息,直到修改完成
  修改后的信息,都将其移到modify_infor中
  '''
  while list_name:
    current_names = list_name.pop()

# 修改信息的过程
  print("\nmodify information: " + current_names)
  modify_infor.append(current_names)

def make_great(modify_info):
  '''显示修改好的信息'''
  print("\nthe following information have benn modified:")
  for modify_info in modify_infor:
    print("the great magician is " + modify_info)

list_name = ['james','star','wendy']
modify_infor = []

show_magicians(list_name[:],modify_infor)
make_great(modify_infor)
print(list_name) # 原列表信息没有改变

 

# 任意数量实参
def make_pizza(*toppings):
  '''打印顾客点的所有配料'''
  print(toppings)

make_pizza('pepperoni')
make_pizza('msshrooms','green peppers','extra cheese')

# 8.5.1 结合使用位置实参和任意数量实参
def make_pizza(size, *toppings):
'''概述要制作的披萨'''
  print("\nmaking a " + str(size) +
  "-inch pizza with the following toppings:")
  for topping in toppings:
    print("- " + topping)

make_pizza(16,'pepperoni')
make_pizza(12,'msshrooms','green peppers','extra cheese')

# 8.5.2 使用任意数量的关键字实参
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 # 定义profile键_值对
  return profile

user_profile = build_profile('albert','einstin',
             location='princeton',
             filed='pyhsics')
print(user_profile)


# 8-12 三明治
def make_sandwich(*toppings):
  '''打印客户要点的材料'''
  print(toppings)

make_sandwich('tomato')
make_sandwich('cheese', 'meat', 'boun')

# 8-13 用户简介:
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('sun','star',
             location='suzhou',
             age='27',
             filed='planning')
print(user_profile)

# 8-14 汽车
def build_profile(first, last, **car_info):
  '''创建一个空字典,包含我们知道的有关车的信息'''
  profile = {}
  profile['manufacturer:'] = first
  profile['model:'] = last
  for key, value in car_info.items():
    profile[key] = value
  return profile

car_profile = build_profile('sabaru','outback',
            color='bule',
            two_package=true)
print(car_profile)

 

# 8.6.1 导入整个模块
'''子文件需要新建_init_.py空文件'''
from function_library import pizza

'''模块名称pizza和函数名make_pizza(),并用句点分隔它们'''
pizza.make_pizza(16,'peppertoni')
pizza.make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.2 导入特定函数
'''from function_library.pizza import:导入特定函数'''
from function_library.pizza import make_pizza
'''显式地导入函数make_pizza(),无须句点分隔,只需指定名称'''
make_pizza(16,'peppertoni')
make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.4 使用as给模块指定别名
'''from function_library.pizza import make_=pizza as mp: 指定别名语法'''
from function_library.pizza import make_pizza as mp

'''无须复杂输入make_pizza,只需输入mp即可'''
mp(16,'peppertoni')
mp(12,'mushrooms', 'green peppers', 'extra cheese')

# 8.6.5 导入模块中的所有函数
'''使用(*)号运算符可让python导入模块中的所有函数:'''
from function_library.pizza import *

make_pizza(16,'peppertoni')
make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')

# 指定函数描述性名称帮助你和别人明白代码
# 每个函数都有包含简要地阐述其功能的注释,紧跟函数定义后面

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网