1.掌握函数的定义和调用方法。
2.掌握函数参数传递原理和方法。
3.掌握匿名函数、嵌套函数、递归函数的创建和调用方法。
4. 掌握变量类型和作用域。
def func(n):
???? num = []
???? while n != 1 and n != 2:
???????? res = n % 2
???????? num.insert(0, res)
???????? n = int(n / 2)
???? num.insert(0, 1)
???? num = map(str, num)
???? return ''.join(num)
if __name__ == '__main__':
??? n = int(input("请输入一个十进制数:"))
??? print("返回的二进制数为",func(n))
def numb(i):
??? if i == 1:
??????? return 1
??? elif i == 2:
??????? return 1
??? else:
??????? return numb(i - 2) + numb(i - 1)
def sumn(n):
??? sum = 2
??? num1 = num2 = 1
??? for i in range(3,n + 1):
??????? num1, num2 = num2, num1
??????? num2 = num1 + num2
??????? sum += num2
??? return sum
if __name__ == '__main__':
??? i = int(input("请输入一个正整数:"))
??? print("斐波那契数列第%d项为%d"%(i,numb(i)) )
??? print("斐波那契数列前10项的和为",sumn(10) )
def func(str):
??? num = ch = other = 0
??? for i in str:
??????? if i <= '9' and i >= '0':
??????????? num += 1
??????? elif i >= 'a' and i <= 'z':
??????????? ch += 1
??????? else:
??????????? other += 1
??? return num,ch,other
if __name__ == '__main__':
??? str = input("请输入一串字符串:")
??? print("字符串中的数字,字母及其他类型的个数为:",func(str))
def func(str1, str2):
??? list3 = []
??? inde = str1.find(str2)
??? for i in range(0, len(str1)):
??????? if i < inde or i > inde + len(str2) - 1 :
??????????? list3.append(str1[i])
??? return ''.join(list3)
if __name__ == '__main__':
??? str1 = input("请输入字符串:")
??? str2 = input("请输入要删除的字符串:")
??? print("删除后的字符串为:",func(str1,str2))
def func(n):
??? sum = 0
??? if n % 2 == 0:
??????? for i in range(2,2*n+2,2):
??????????? sum += 1/i
??? else:
??????? for i in range(1,2*n+3,2):
??????????? sum += 1/i
??? return sum
if __name__ == '__main__':
??? n = int(input("请输入一个整数:"))
??? print("结果为:",func(n))
def nofunc(n):
??? old = 10
??? for i in range(1,`n):
??????? old += 2
??? return old
def func(n):
??? if n == 1:
??????? return 10
??? else:
??????? return func(n - 1) + 2
if __name__ == '__main__':
??? print("第5个人%d岁。"%nofunc(5))
??? print("第5个人%d岁。" % func(5))
def isWs(n):
??? sum = 0;
??? for i in range(1, n):
??????? if n % i == 0:
??????????? sum += i;
??? if sum == n:
??????? return True
??? else:
??????? return False
if __name__ == '__main__':
??? n = int(input("请输入一个正整数:"))
??? flag =? isWs(n)
??? if flag == True:
??????? print(n,"是一个完数。")
??? else:
??????? print(n, "不是一个完数。")
import math
def isGDBH(n):
??? list1 = []
??? def isprimenum(m):
??????? for i in range(2, int(math.sqrt(m) + 1), 1):
??????????? if m % i == 0:
??????????????? return -1
??????? return 1
??? for j in range(1, int(n / 2) + 1):
??????? if isprimenum(j) == 1 and isprimenum(n - j) == 1:
??????????? list1.append("{0}={1}+{2}".format(str(n),str(j),str(n - j)) )
??? return list1
if __name__ == '__main__':
??? n = int(input("请输入一个6~100间的偶数:"))
??? print(isGDBH(n))
import math
def decorator(func):
??? def check_call_square(x, y, z):
??????? if x > 0 and y > 0 and z > 0 and z < x + y and z > math.fabs(x - y):
??????????? return func(x, y, z)
??????? else:
??????????? return "提示:函数参数" + str(x) + "," + str(y) + "和" + str(z) + "必须为非负数且能够组成三角形!"
??? return check_call_square
@decorator
def square(x, y, z):
??? return 2 * (x + y + z)
def decorator2(func):
??? def check_call_rec(x, y):
??????? if x > 0 and y > 0 :
??????????? return func(x, y)
??????? else:
??????????? return "提示:函数参数" + str(x) +? "和" + str(y) + "必须为非负数!"
??? return check_call_rec
@decorator2
def rec(x, y):
??? return 2 * (x + y)
def decorator3(func):
??? def check_call_cir(x):
??????? if x > 0 and y > 0 :
??????????? return func(x)
??????? else:
??????????? return "提示:函数参数" + str(x) + "必须为非负数!"
??? return check_call_cir
@decorator3
def cir(x):
??? return 2 * 3.14 * x
if __name__ == '__main__':
??? x,y,z = map(int,input("请输入三角形三边长度:").split())
??? print(square(x, y, z))
??? x, y = map(int, input("请输入矩形的长宽:").split())
??? print(rec(x, y) )
??? x = int(input("请输入圆形的半径:"))
??? print(cir(x) )
? 通过这次实验,我有了更深入的理解和掌握了Python中函数的定义和调用方法。我学会了如何编写具有不同功能的函数,如转换数字、计算数列和处理字符串等。这些技能对我在将来解决实际问题时是非常有价值的。在函数参数传递方面,我理解了Python中的按值传递和按引用传递的区别,以及如何通过这些方式在函数间传递数据。我还学会了如何使用默认参数、关键字参数和可变参数,这让函数调用更加灵活。此外,我掌握了匿名函数、嵌套函数和递归函数的创建和使用。通过递归函数编写斐波那契数列和计算人的年龄问题,我深刻体会到了递归在解决某些类型问题时的优雅和效率。在变量类型和作用域方面,我了解了局部变量和全局变量的区别,以及如何在函数内部正确地引用和修改它们。我学会了利用作用域规则来保护代码不受外界不必要的干扰。对于实验内容的每一部分,我都进行了详尽的测试,以确保我的函数按预期工作。调试和测试不仅帮助我发现并修复了代码中的错误,也让我对编写的程序有了更深的理解。