当前位置:Gxlcms > Python > Guibs的Python学习_函数

Guibs的Python学习_函数

时间:2021-07-01 10:21:17 帮助过:12人阅读

Guibs 的 Python学习_ 函数

  1. # 函数# 函数是带有名字的代码块, 用于完成具体的工作# 定义函数 greet_userdef greet_user():
  2. # 函数体
  3. print("Hello")# 调用函数 greet_usergreet_user()# 向函数传递信息def greet_user(username): # username 是一个形参
  4. print("Hello " + username)
  5. greet_user(username='Guibs') # Guibs 是一个实参greet_user('Guibs')# 带关键字传递实参# 可以不用考虑实参传递的顺序def greet_user(username1, username2):
  6. print("Hello " + username1 + " and " + username2)
  7. greet_user(username2='Guibs', username1='Guibs82')# 参数默认值# 若不传递参数则调用默认值# 含有默认值的参数必须放在后面
  8. def greet_user_with_default_name(username1, username2='Guibs82'):
  9. print("Hello " + username1 + " and " + username2)# 若未传递足够参数, 则按位置进行参数传递greet_user_with_default_name('G')# 返回值
  10. def get_formatted_name(first_name, last_name, middle_name = ""):
  11. if middle_name:
  12. full_name = first_name + " " + middle_name + " " + last_name else:
  13. full_name = first_name + " " + last_name return full_name.title()
  14. my_name = get_formatted_name("guibs", "g")
  15. print("My name is " + my_name)
  16. my_name = get_formatted_name("guibs", 'g', '82')
  17. print("My name is " + my_name)# 返回字典def build_person(first_name, last_name, age=''):
  18. '''返回一个字典, 包含有关一个人的信息'''
  19. person = { 'first': first_name.title(), 'last': last_name.title(),
  20. } if age:
  21. person['age'] = age return person
  22. print(build_person('g', 'ghost', 22))# 传递列表names = ['guibs', 'ghostg', 'rio_G']def greet_users(names):
  23. for name in names:
  24. print("Hello " + name.title())
  25. greet_users(names=names)# 在函数中修改列表# 将列表传递给函数后, 函数可以对其进行永久性修改unprinted_designs = ['iphone case', 'ipad case', 'mac case']
  26. printed_designs = []def print_designs(unprinted_designs, printed_designs):
  27. while unprinted_designs:
  28. current_design = unprinted_designs.pop()
  29. print("准备打印: " + current_design)
  30. printed_designs.append(current_design)
  31. print("打印完毕: " + current_design)
  32. print("全部作品打印完毕")
  33. print_designs(unprinted_designs=unprinted_designs, printed_designs=printed_designs)def show_printed_designs(printed_designs):
  34. print("打印完毕的作品: ") for printed_design in printed_designs:
  35. print(printed_design)
  36. show_printed_designs(printed_designs)# 禁止函数修改列表# 采用切片的形式, 复制传入函数的列表
  37. unprinted_designs = ['iphone case', 'ipad case', 'mac case', 'apple watch case']
  38. printed_designs = []
  39. print_designs(unprinted_designs[:], printed_designs)
  40. print(unprinted_designs) # 此时, 原列表未被修改# 传递任意数量的参数# [*param_name]# 此时, Python 将参数封装至一个元组def print_username(*username):
  41. print(username)
  42. print_username("Guibs")
  43. print_username("Guibs", 'GhostG')# 使用任意数量的关键字形参# [**param_names]def set_hobbies(name, **hobbies):
  44. my_hobbies = {}
  45. my_hobbies['name'] = name for key, value in hobbies.items():
  46. my_hobbies[key] = value return my_hobbies
  47. print(set_hobbies(name="Guibs", hobby_1='Swift', hobby_2='Python'))# 注意: 在 import 时, 若不使用系统中的解释器, 而是用自己创建的, 则报错
  48. # 导入存储在模块中的函数
  49. # 导入整个模块import pizza
  50. pizza.make_pizza(12, 'mushrooms', 'extra cheese')# 使用 as 给模块指定别名import pizza as p
  51. p.make_pizza(12, 'mushrooms', 'lots of cheese')# 导入特定的函数# from module_name import function_name_0, function_name_1, ...
  52. # 这种语法可以无需使用 .from pizza import make_pizza
  53. make_pizza(12, 'mushrooms', 'more cheese')# 使用 as 给函数指定别名from pizza import make_pizza as buy_pizza
  54. buy_pizza(12, 'mushrooms', 'a lot of cheese')# 导入模块中的所有函数# [*]from pizza import *
  55. get_price()

运行上方代码另需新建 pizza.py

  1. def make_pizza(size, *toppings):
  2. '''概述要制作的披萨'''
  3. print("做一个尺寸为: " + str(size) + ", 包含: ") for topping in toppings:
  4. print("- " + topping)
  5. print("的披萨")def get_price():
  6. print("The price is 20")


以上就是Guibs 的 Python学习_ 函数的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行