Python之第十四天的努力--函数与迭代器练习
作者:互联网
01 内容回顾
-
可迭代对象:
-
可以更新迭代的实实在在的值
-
内部含有
'__iter__'
方法的。
-
str list tuple dict set range
-
优点:操作方法多,操作灵活,直观
-
缺点:占用内存。
-
-
迭代器:
-
可更新迭代的一个工具(数据结构)。
-
内部含有
'__iter__' '__next__'
方法的
-
文件句柄
-
优点:节省内存。惰性机制。
-
缺点:不直观,速度相对慢,操作方法单一,一条路走到黑。
-
-
格式化输出。
-
函数名的应用。
02 练习
# 1.请写出下列代码的执行结果:
# 例一:
#
# def func1():
# print('in func1' )
#
# def func2():
# print('in func2' )
#
# ret = func1
# ret()
# ret1 = func2
# ret1()
# ret2 = ret
# ret3 = ret2
# ret2()
# ret3()
# 执行结果:
# in func1
# in func2
# in func1
# in func1
# 例二:
#
# def func1():
# print('in func1' )
#
# def func2():
# print('in func2' )
#
# def func3(x, y):
# x()
#
# print('in func3' )
#
# y()
#
# print(111)
# func3(func2, func1)
# print(222)
# 执行结果:
# 111
# in func2
# in func3
# in func1
# 222
# 例三(选做题):
#
# def func1():
# print('in func1' )
#
# def func2(x):
# print('in func2' )
# return x
#
# def func3(y):
# print('in func3' )
# return y
#
# ret = func2(func1)
# ret()
# ret2 = func3(func2)
# ret3 = ret2(func1)
# ret3()
#
# 执行结果:
# in func2
# in func1
# in func3
# in func2
# in func1
# 2.看代码写结果:
#
# def func(arg):
# return arg.replace('苍老师', '***')
#
#
# def run():
# msg = "Alex的女朋友苍老师和大家都是好朋友"
# result = func(msg)
# print(result)
#
#
# run()
# Alex的女朋友***和大家都是好朋友
# def func(arg):
# return arg.replace('苍老师', '***')
#
#
# def run():
# msg = "Alex的女朋友苍老师和大家都是好朋友"
# result = func(msg)
# print(result)
#
# data = run()
# print(data)
# Alex的女朋友***和大家都是好朋友
# None
# 3.看代码写结果:
#
# DATA_LIST = []
#
#
# def func(arg):
# return DATA_LIST.insert(0, arg)
#
#
# data = func('绕不死你')
# print(data)
# print(DATA_LIST)
# None
# ['绕不死你']
# 4.看代码写结果:
#
# def func():
# print('你好呀')
# return '好你妹呀'
#
#
# func_list = [func, func, func]
#
# for item in func_list:
# val = item()
# print(val)
# 你好呀
# 好你妹呀
# 你好呀
# 好你妹呀
# 你好呀
# 好你妹呀
# 5.看代码写结果:
#
# def func():
# print('你好呀')
# return '好你妹呀'
#
#
# func_list = [func, func, func]
#
# for i in range(len(func_list)):
# val = func_list[i]()
# print(val)
# 你好呀
# 好你妹呀
# 你好呀
# 好你妹呀
# 你好呀
# 好你妹呀
# 6.看代码写结果:
# def func():
# return '烧饼'
#
#
# def bar():
# return '豆饼'
#
#
# def base(a1, a2):
# return a1() + a2()
#
#
# result = base(func, bar)
# print(result)
# 烧饼豆饼
# 7.看代码写结果:
# for item in range(10):
# print(item)
#
# print(item)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 9
# 8.看代码写结果:
# def func():
# for item in range(10):
# pass
# print(item)
# func()
# 9
# 9.看代码写结果:
# item = '老男孩'
# def func():
# item = 'alex'
# def inner():
# print(item)
# for item in range(10):
# pass
# inner()
# func()
# 9
# 10.看代码写结果:
# l1 = []
# def func(args):
# l1.append(args)
# return l1
# print(func(1))
# print(func(2))
# print(func(3))
# [1]
# [1,2]
# [1,2,3]
# 11.看代码写结果:
# name = '太白'
# def func():
# global name
# name = '男神'
# print(name)
# func()
# print(name)
# 太白
# 男神
# 12.看代码写结果:
# name = '太白'
# def func():
# print(name)
# func()
# 太白
# 13.看代码写结果:
# name = '太白'
# def func():
# print(name)
# name = 'alex'
# func()
# 报错
# 14.看代码写结果:
# def func():
# count = 1
# def inner():
# nonlocal count
# count += 1
# print(count)
# print(count)
# inner()
# print(count)
# func()
# 1
# 2
# 2
# 15.看代码写结果:
# def extendList(val,list=[]):
# list.append(val)
# return list
#
# list1 = extendList(10)
# list2 = extendList(123,[])
# list3 = extendList('a')
#
# print('list1=%s'%list1)
# print('list2=%s'%list2)
# print('list3=%s'%list3)
# list1=[10,'a']
# list1=[123]
# list1=[10,'a']
# 16.看代码写结果:
# def extendList(val,list=[]):
# list.append(val)
# return list
# print('list1=%s'% extendList(10))
# print('list2=%s'% extendList(123,[]))
# print('list3=%s'% extendList('a'))
# list1=[10]
# list1=[123]
# list1=[10,'a']
# 17.写代码:用while循环模拟for内部的循环机制
# l1 = [1,2,3,4,5,6,7,8,9,10]
#
# obj = iter(l1)
# while 1:
# try:
# print(next(obj))
# except StopIteration:
# break
# 18.写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值}
# 例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数)
# dic = {}
# def min_max(*args):
# dic['max'] = max(args)
# dic['min'] = min(args)
# print(dic)
# min_max(2,5,7,8,4)
# 19.写函数,传入一个参数n,返回n的阶乘
# 例如:cal(7) 计算7654321
# def factorial(num):
# result = 1
# while 1:
# if num > 0:
# result = result * num
# else:
# break
# num = num - 1
# return result
# print(factorial(3))
# def factorial(num):
# result = 1
# for i in range(num,0,-1):
# result = result * i
# return result
# print(factorial(3))
# 20.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组(选做题)
# 例如:[(‘红心’,2),(‘草花’,2), …(‘黑桃’,‘A’)]
# l1 = []
# def poker():
# tu1 = ()
# tu2 = ()
# tu3 = ()
# tu4 = ()
# s1 = 'JQKA'
# for i in range(2,11):
# tu1 = ('红心',i)
# tu2 = ('草花',i)
# tu3 = ('方片',i)
# tu4 = ('黑桃',i)
# l1.append(tu1)
# l1.append(tu2)
# l1.append(tu3)
# l1.append(tu4)
# for j in s1:
# tu1 = ('红心', j)
# tu2 = ('草花', j)
# tu3 = ('方片', j)
# tu4 = ('黑桃', j)
# l1.append(tu1)
# l1.append(tu2)
# l1.append(tu3)
# l1.append(tu4)
# poker()
# print(l1)
# def poker():
# num = list(range(2, 11))
# num.extend('JQKA')
# color = ('红心', '草花', '方片', '黑桃')
# card = [(m, n) for m in color for n in num]
# return card
# print(poker())
标签:func1,return,迭代,Python,第十四天,func,l1,print,def 来源: https://www.cnblogs.com/szheng/p/13126869.html