def programer_say ():
print('\t\t\t***愿你眼里有星辰,身边有微风,心中有暖阳,指尖有代码。***')
programer_say()
'''!!!兵家大忌:切勿在函数体之内修改全局变量,因为。。。'''
def discount (price,rate):
new_price = price*rate
old_price=520
print('企图在函数内修改全局变量old_price:',old_price)
print('打折后的价格为:',new_price)
old_price = float(input('请输入单价:'))
rate = float(input('请输入折扣:'))
discount(old_price,rate)
print('打印在函数外修改全局变量old_price:',old_price)
''''....如果实在需要在函数中修改全局变量的值那么,,,,'''
def discount_1():
global old_price
old_price=520
print('函数中修改全局变量的值',old_price)
discount_1()
'''函数的嵌套、闭包'''
def outside():
print('I am outside!')
def inside():
print('I am inside!')
inside()
outside()
'''请问如何访问funIn()'''
def funOut():
def funIn():
print('1:宾果!你成功访问到我啦!')
return funIn()
'''第一个直接返回了函数的值,第二个属于闭包不可直接访问'''
#请问如何访问funIn()
def funOut():
def funIn():
print('2:宾果!你成功访问到我啦!')
return funIn
funOut()
'''目测打印什么内容?'''
def funX():
x = 5
def funY():
nonlocal x
x += 1
return x
return funY
a = funX()
print(a())
print(a())
print(a())
'''统计次数'''
str1 = '''1223abc'''
list1 = []
for each in str1:
if each not in list1:
print(each + ':'+str(str1.count(each)))
list1.append(each)
#——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————#
'''匿名函数'''
nm = lambda x :2*x
print(type(nm))
print(nm(5))
'''过滤器'''
x = range(10)
add = lambda x:x % 2
print(list(filter(add,x)))
'''map映射'''
print(list(map(lambda x:x**2,range(10)))
'''求一百以内3的倍数'''
print(list(filter(lambda x:not(x%3) ,range(101))))
'''递归:求一个数的阶乘:'''
def factiroal(n):
if n==1:
return 1
else:
return n*factiroal(n-1)
number = int(input('请输入一个整数'))
result = factiroal(number)
print(result)
'''递归有风险,使用需谨慎!'''
'''斐波那契数列递归实现'''
def fib(n):
if n==1:
return -1
n1=1
n2=1
while (n-2)>0:
n3=n2+n1
n1=n2
n2=n3
n-=1
return n3
result = fib(6)
print(result)
章后语:直到回到岗位上才知道做总结,慵懒的假期结束了,假期不注意的,现在忘了的都该拾起来了。。。
不然又忘了。。。。