# test1
def greet_user():print("hello")
greet_user()# test2
def greet_user(username):#形参print(f"hello,{username.title()}")
greet_user("name")#实参# test3
def display(text):print(f"我在本章学的是{text}")
display("函数")# test4
def favorite_book(book):print(f"one of my favorite book is {book}")
favorite_book("alice in wonderland")# test5传递多个位置实参
def discribe_name(type,name):print(f"type is {type}")print(f"name is {name}")
discribe_name("dog","pet")# test6传递多个关键字实参,指定各个实参对应的形参
def discribe_name(type,name):print(f"type is {type}")print(f"name is {name}")
discribe_name(type="dog",name="pet")# test7:给每个形参指定默认值,必须先给出没有默认值的形参
def discribe_name(name,type='dog'):print(f"type is {type}")print(f"name is {name}")
discribe_name(name="pet")#test8
def make_t(size,text):print(f"size is {size},name is {text}")
make_t("m","fish")# test9
def make_lt(size,text = "I love T"):print(f"size is {size},text is {text}")
make_lt("s")
make_lt("m")
make_lt("l")# test10
def get_formated_name(first_name,last_name):full_name = f"{first_name}{last_name}"return full_name.title()#首字母大写格式
musican = get_formated_name('jimi','hendrix')
print(musican)# test11,让参数变成可选的,处理中间名为空的情况
def get_formated_name(first_name,middle_name,last_name):if middle_name:full_name = f"{first_name}{middle_name}{last_name}"else:full_name = f"{first_name}{last_name}"return full_name.title()#首字母大写格式
musican = get_formated_name('jimi','hendrix','end_name')
print(musican)musican = get_formated_name('jimi','end_name')
print(musican)# test12 返回字典
def build_person(first_name,last_name):person  = {'first':first_name,'last':last_name}return person
musican1 = build_person('yu','fish')
print(musican1)test13 结合函数使用while循环,设置截至输入的条件
def get_formated_name(first_name,last_name):full_name = f"{first_name}{last_name}"return full_name.title()#首字母大写格式
while True:print("\nplace tell me you name:")print("enter q at any time to quit")f_name = input("first_name")if f_name == 'q':breakl_name = input("last_name")if l_name == 'q':breakformated_name = get_formated_name(f_name,l_name)print(f"hello,{formated_name}")# test14
def city_country(country,city):city_name = f"{country}{city}"return city_name
name1 = city_country("cn","kunming")
print(name1)
name2 = city_country("UN","CO")
print(name2)# test15,数量是可选的,因此把设置成空字符串,放在最末尾
def make_album(name1,name2,num = ''):if num :#判断是否有值是这么写的album = f"song name is {name1},album name is {name2},mumber is {num}"else:album = f"song name is {name1},album name is {name2}"return album
album1 = make_album(name1='周杰伦',name2 = '烟花易冷')
print(album1)# test16  注意字符串一定要用引号
def make_album(name1,name2):album = f"song name is {name1},album name is {name2}"return album
while True:print("input q at any time to quit")song_name = input("请输歌手名:")if song_name == 'q':breakalbum_name = input("请输入歌曲名:")if album_name== 'q':breakname = make_album(song_name,album_name)print({name})# test17 传递列表
def greet_user(names):for user in names:print(f"hello,{user}")
names = ['张三','李四','王五']
greet_user(names)# test18在函数中修改列表
unprinted_designs = ['phone_case','robot pendant','dodecachedron']
completed_models =[]while unprinted_designs:curent_design = unprinted_designs.pop()print(f"printing model:{curent_design}")completed_models.append(curent_design)
print("\nthe following modul have been printed:")
for complated_model in completed_models:print(complated_model)# test19传递任意数量的实参,让topping创建一个名为toppings的空元组
# 并将收到的值都封装到这个元组中
def make_pizza(*toppings):"""打印客户点的所有配料:param toppings::return:"""for top in toppings:print(top)
make_pizza('pep')
make_pizza('mushrooms','green pep','extra chees')# test20 结合使用位置实参和任意数量的实参
# python先匹配位置实参和关键字实参,再将余下的实参都收集到一个形参中
def make_pizza(size,*topping):print(f"making a {size}-inch pizza with the following toppings:")for top in topping:print(f"{top}")
make_pizza(16,'pop')
make_pizza(20,'pep','pop')# test21,使用任意数量的关键字形参,*是元组,**是字典
# 输出:{'loc': 'c', 'age': 'd', 'first_name': 'a', 'last_name': 'b'}
def biuld_profile(first,last,**user_info):user_info['first_name'] = firstuser_info['last_name'] = lastreturn user_info
user_profile = biuld_profile('a','b',loc = 'c',age = 'd')
print(user_profile)# test22
def make_san(*san):for s in san:print(s)
make_san('草莓味','香蕉味','榴莲味')
make_san('橘子味')#test23
# 输出:{'loc': 'dali', 'money': '1000000000', 'name': 'fish', 'age': '15'}
def my_info(name,age,**my_info):my_info['name'] = namemy_info['age'] = agereturn my_info
my = my_info('fish','15',loc = 'dali',money = '1000000000')
print(my)# test24
# 输出:{'money': '20w', 'price': '100w', 'maker': 'CN', 'size': '20W'}
def car_info(maker,size,**car):car['maker']  = makercar['size'] = sizereturn car
my_car = car_info('CN','20W',money = '20w',price = '100w')
print(my_car)

python-函数模块基础语法复习相关推荐

  1. python导入模块的语法结构_python学习第五讲,python基础语法之函数语法,与Import导入模块....

    python学习第五讲,python基础语法之函数语法,与Import导入模块. 一丶函数简介 函数,就是一个代码块,这个代码块是别人写好的.我们调用就可以. 函数也可以称为方法. 1.函数语法定义 ...

  2. python基础语法复习[二] 函数、类

    python基础语法复习[二] 前言 一.函数 1.基本概念 2.一般实例 3.参数传递 (1)传不可变对象 (2)传可变对象 4.函数递归调用实例 (1)eg:实现字符串的反转: (2)递归思想画树 ...

  3. python编程语法大全-Python编程入门——基础语法详解

    今天小编给大家带来Python编程入门--基础语法详解. 关于怎么快速学python,可以加下小编的python学习群:611+530+101,不管你是小白还是大牛,小编我都欢迎,不定期分享干货 每天 ...

  4. python编程语法-Python编程入门——基础语法详解

    今天小编给大家带来Python编程入门--基础语法详解. 一.基本概念 1.内置的变量类型: Python是有变量类型的,而且会强制检查变量类型.内置的变量类型有如下几种: #浮点 float_num ...

  5. python编程if语法-Python编程入门基础语法详解经典

    原标题:Python编程入门基础语法详解经典 一.基本概念 1.内置的变量类型: Python是有变量类型的,而且会强制检查变量类型.内置的变量类型有如下几种: #浮点 float_number = ...

  6. python编程语法-Python编程入门——基础语法详解(经典)

    今天小编给大家带来Python编程入门--基础语法详解.温馨提示: 亮点在最后! 在这里还是要推荐下我自己建的Python开发学习群:301056051,群里都是学Python开发的,如果你正在学习P ...

  7. python基础语法及知识总结-Python 学习完基础语法知识后,如何进一步提高?

    ---4.30更新---感谢大家支持,点赞都破两千了.给大家整理出来新的资料,需要的小伙伴来自取: Python练手项目视频如下: Python自学基础知识如下: 以下为原文内容: Python 学习 ...

  8. python基础编程语法-Python编程入门——基础语法详解

    今天小编给大家带来Python编程入门--基础语法详解. 一.基本概念 1.内置的变量类型: Python是有变量类型的,而且会强制检查变量类型.内置的变量类型有如下几种: #浮点 float_num ...

  9. python基础编程语法-Python编程入门——基础语法详解(经典)

    今天小编给大家带来Python编程入门--基础语法详解.温馨提示: 亮点在最后! 在这里还是要推荐下我自己建的Python开发学习群:301056051,群里都是学Python开发的,如果你正在学习P ...

最新文章

  1. JackJson 使用记录
  2. Centos6.9安装Oracle11G(静默方式)
  3. GridView列值绑定
  4. Python小游戏(俄罗斯方块)
  5. 参加浙江中医药大学第十一届程序设计竞赛(ACM赛制)的总结
  6. JNI开发笔记(六)--一种更规范的so库生成方法
  7. raspberry pi_Raspberry Pi在单板计算机,新的符合FCC规则的路由器芯片等众多清单上排名第一
  8. nodefs模块的使用demo
  9. Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)
  10. SSH客户端:Termius for Mac
  11. 视频播功能及画面协同操作注意事项
  12. 2013年计算机运算速度慢,win7电脑运行速度很慢怎么提速|三个win7提速的技巧
  13. 游戏设计分析——魔塔
  14. 通俗易懂告诉你:何为95%置信区间?
  15. 天津插画师培训机构 ,0基础可以学吗?
  16. Comet实现的新选择
  17. java-net-php-python-springtboot校园信息交流互助系统计算机毕业设计程序
  18. 最全的脱壳,反编译 ,汇编工具
  19. 什么是码率控制? 在视频编码中,码率控制的概念是什么,它是通过什么实现的?
  20. # Python3 面试试题--Python语言特性

热门文章

  1. 【论文阅读】超分辨率——Towards Real-World Blind Face Restoration with Generative Facial Prior
  2. [ 英语 - 特别收录系列 ] 语法重塑专栏 之 动词分类 —— 英语兔学习笔记(2)
  3. 刘芳计算机学院,刘芳,女,1983年生,天津工业大学计算机与软件学院讲师.PDF
  4. SpringBoot--->>>原理解析-->>自定义事件监听组件
  5. CSS速成手册(5)
  6. springboot整合ftp
  7. 母(父)爱是一把双刃剑
  8. LED灯内常见驱动电路
  9. 【从零开始学Skynet】基础篇(五):简易聊天室
  10. Java中带图片的数据导出到excel