上一页 1 ··· 309 310 311 312 313 314 315 316 317 ··· 367 下一页
摘要: 有时候, 预先不知道函数需要接受多少个实参,好在python允许函数从调用语句中收集任意数量的实参。 1、 >>> def a(*x): ## 在形参的前面加上*号,表示收集参数,可实现函数接受任意数量的实参 print(x) >>> a("aaa") ('aaa',) >>> a("aaa","b 阅读全文
posted @ 2021-03-10 20:52 小鲨鱼2018 阅读(289) 评论(0) 推荐(0) 编辑
摘要: 1、原始函数 def a(x,y): while x: temp = x.pop() print(f"printing model:{temp}") y.append(temp) def b(x): print("\nThe following model had been printed!") f 阅读全文
posted @ 2021-03-10 17:38 小鲨鱼2018 阅读(246) 评论(0) 推荐(0) 编辑
摘要: 1、不使用函数: list1 = ["aaaa","bbbb","cccc"] list2 = [] while list1: temp = list1.pop() print(f"printing model {temp}") list2.append(temp) print("\nThe fol 阅读全文
posted @ 2021-03-10 16:52 小鲨鱼2018 阅读(233) 评论(0) 推荐(0) 编辑
摘要: 1、 python中向函数传递列表,提高处理列表的效率。 >>> def a(x): ## 定义函数的形参x为可迭代对象 for i in x: print(f"hello,{i.title()}") >>> b = ["aaa","bbb","ccc","ddd"] ## 实参定义为列表,实现向函 阅读全文
posted @ 2021-03-10 15:22 小鲨鱼2018 阅读(472) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(first,meddle,last): b = f"{first} {meddle} {last}" return b.title() >>> a("aaa","bbb","ccc") 'Aaa Bbb Ccc' >>> a("aaa","bbb") ## 少一个实参报错 阅读全文
posted @ 2021-03-10 11:26 小鲨鱼2018 阅读(63) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y): b = {"key1":x,"key2":y} return b >>> a(111,222) {'key1': 111, 'key2': 222} >>> a(111,222,333) ## 多家实参报错 Traceback (most recent call 阅读全文
posted @ 2021-03-10 11:12 小鲨鱼2018 阅读(365) 评论(0) 推荐(0) 编辑
摘要: 1、 def a(first,last): full = f"{first} {last}" return full.title() while True: ## 条件为真的话,一直循环 print("\n\nplease tell me your name:") ## 两个\n,每执行一次循环,空 阅读全文
posted @ 2021-03-10 10:33 小鲨鱼2018 阅读(346) 评论(0) 推荐(0) 编辑
摘要: python中函数可以返回任何类型的值,包含列表和字典等较复杂的数据结构。 1、 >>> def fun(first,last,age): full = {"first":first,"last":last,"age":age} ## 函数返回列表 return full >>> infor = f 阅读全文
posted @ 2021-03-09 22:46 小鲨鱼2018 阅读(2302) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(first,middle,last): ## 函数定义了三个形参 full = f"{first} {middle} {last}" return full.title() >>> name = a("aaa","bbb","ccc") >>> print(name) Aa 阅读全文
posted @ 2021-03-09 22:11 小鲨鱼2018 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y,z): print(x + y + z) >>> a(2,3,4) 9 >>> a(2,3) ## 缺少对应的实参 Traceback (most recent call last): File "<pyshell#317>", line 1, in <module 阅读全文
posted @ 2021-03-09 11:40 小鲨鱼2018 阅读(83) 评论(0) 推荐(0) 编辑
上一页 1 ··· 309 310 311 312 313 314 315 316 317 ··· 367 下一页