1.实验任务1
1 print(sum) 2 sum = 42 3 print(sum) 4 5 def inc(n): 6 sum = n + 1 7 print(sum) 8 return sum 9 10 sum = inc(7) + inc(7) 11 print(sum)
问题:不是。line1中的sum是指Python的内置函数;line3中的sum指的是line2中的全局变量sum;line7中的sum指的是inc函数中的局部变量;line11中的sum指的是line10的全局变量
2.实验任务2
task1
1 def func1(a, b, c, d, e, f): 2 ''' 3 返回参数a,b,c,d,e,f构成的列表 4 默认,参数按位置传递; 也支持关键字传递 5 ''' 6 return [a,b,c,d,e,f] 7 8 9 def func2(a, b, c,*, d, e, f): 10 ''' 11 返回参数a,b,c,d,e,f构成的列表 12 *后面的参数只能按关键字传递 13 ''' 14 return [a,b,c,d,e,f] 15 16 def func3(a, b, c, /, d, e, f): 17 ''' 18 返回参数a,b,c,d,e,f构成的列表 19 /前面的参数只能按位置传递 20 ''' 21 return [a,b,c,d,e,f] 22 23 # func1调用:按位置传递、按参数传递都可以 24 print( func1(1,9,2,0,5,3) ) 25 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 26 print( func1(1,9,2, f=3, d=0, e=5)) 27 28 # func2调用:d,e,f必须按关键字传递 29 print( func2(11, 99, 22, d=0, e=55, f=33) ) 30 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 31 32 # func3调用:a,b,c必须按位置传递 33 print( func3(111, 999, 222, 0, 555, 333)) 34 print( func3(111, 999, 222, d=0, e=555, f=333) )
运行结果如图:
增加print( func2(11, 99, 22, 0, 55, 33))后:
增加print(func3(a=111, b=999, c=222, 0, 555, 333))后:
task2_2
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )
运行结果如图:
task2_2所示情况中,必须使用关键字传递reversse
task2_3
1 def func(a, b, c, /, *, d, e, f): 2 return( [a,b,c,d,e,f] ) 3 4 5 print(func(1,2,3,d = 4,e = 5,f = 6))
3.实验任务3
1 def solve(a, b, c): 2 ''' 3 求解一元二次方程, 返回方程的两个根 4 :para: a,b,c: float 方程系数 5 :return: tuple 6 ''' 7 delta = b*b - 4*a*c 8 delta_sqrt = abs(delta)**0.5 9 p1 = -b/2/a 10 p2 = delta_sqrt/2/a 11 12 if delta >= 0: 13 root1 = p1 + p2 14 root2 = p1 - p2 15 else: 16 root1 = complex(p1, p2) 17 root2 = complex(p1, -p2) 18 19 return root1, root2 20 21 while True: 22 try: 23 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 24 if t == '#': 25 print('结束计算,退出') 26 break 27 a, b, c = map(float, t.split()) 28 if a == 0: 29 raise ValueError('a = 0, 不是一元二次方程') 30 except ValueError as e: 31 print(repr(e)) 32 print() 33 except: 34 print('有其它错误发生\n') 35 else: 36 root1, root2 = solve(a, b, c) 37 print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') 38 print()
运行结果如图:
加了print(solve.__doc__)后:
4.实验任务4
1 def list_generator(start,end,step=1): 2 lst = [] 3 while start <= end: 4 lst.append(start) 5 start = start + step 6 print(lst) 7 8 list_generator(-5,5) 9 10 list_generator(-5,5,2) 11 12 list_generator(1,5,0.5)
运行结果如图:
5.实验任务5
1 def is_prime(n): 2 for i in range(2,n+1): 3 if n % i == 0 or n == 2: 4 return False 5 else: 6 return True 7 8 prime_list = [] 9 for n in range(2,21): 10 if is_prime(n) == True: 11 prime_list.append(n) 12 else: 13 pass 14 i = 2 15 while i <= 20: 16 j = 0 17 while j < len(prime_list): 18 k = 0 19 while k <len(prime_list): 20 if prime_list[j] + prime_list[k] == i: 21 print(f'{i} = {prime_list[j]} + {prime_list[k]}') 22 else: 23 pass 24 k += 1 25 break # 找到后停止该循环 26 j += 1 27 i += 2
运行结果如图:
6.实验任务6
# 编码函数encoder()定义 def encoder(text): text_list = list(text) for i in range(len(text_list)): if ord('a') <= ord(text_list[i]) <= ord('z'): add5 = ord(text_list[i]) + 5 if add5 <= ord('z'): text_list[i] = chr(add5) else: add5 = add5 % ord('z') - 1 +ord('a') text_list[i] = chr(add5) elif ord('A') <= ord(text_list[i]) <= ord('Z'): add5 = ord(text_list[i]) + 5 if add5 <= ord('z'): text_list[i] = chr(add5) else: add5 = add5 % ord('Z') - 1 +ord('A') text_list[i] = chr(add5) else: pass return ''.join(text_list) # 解码函数decoder()定义 def decoder(text): text_list = list(text) for i in range(len(text_list)): if ord('a') <= ord(text_list[i]) <= ord('z'): red5 = ord(text_list[i]) - 5 if ord('a') <= red5: text_list[i] = chr(red5) else: red5 = ord('z') - (ord('a') - red5 - 1) text_list[i] = chr(red5) elif ord('A') <= ord(text_list[i]) <= ord('Z'): red5 = ord(text_list[i]) - 5 if ord('A') <= red5: text_list[i] = chr(red5) else: red5 = ord('Z') - (ord('A') - red5 - 1) text_list[i] = chr(red5) else: pass return ''.join(text_list) # 主体代码逻辑 text = input('输入英文文本: ') encoded_text = encoder(text) print('编码后的文本: ', encoded_text) decoded_text = decoder(encoded_text) print('对编码后的文本解码: ', decoded_text)
运行结果如图:
7.实验任务7
1 # collatz函数定义 2 def collatz(n): 3 n_list.append(n) 4 while True: 5 if n % 2 == 0 and n != 0: 6 collatz(n // 2) 7 elif (n + 1) % 2 == 0 and n != 1: 8 collatz(n * 3 + 1) 9 elif n == 1: 10 print(n_list) 11 break 12 13 # 自定义异常 14 class Error(Exception): 15 def __init__(self,n): 16 self.n = n 17 18 def __str__(self): 19 print('Error: must be a positive integer') 20 21 # 主题代码逻辑 22 try: 23 n_list = [] 24 n = input('输入一个正整数:') 25 if n.isdigit() == False: 26 raise Error(n) 27 elif int(n) <= 0: 28 raise Error(n) 29 else: 30 collatz(int(n)) 31 except Error: 32 print('Error: must be a positive integer')
8.实验任务8
1 # 函数func()定义 2 def func(n): 3 if n == 1: 4 ans = 2 - 1 5 else: 6 ans = 2 * func(n-1) +1 7 n -= 1 8 return ans 9 # 10 while True: 11 x = input() 12 if x == '#': 13 print('计算结束') 14 break 15 n = int(x) 16 ans = func(n) 17 print(f'n = {n}, ans = {ans}')
运行结果如图:
实验总结
作用域:LEGB(局部-嵌套-全句-内置)
参数: * 后面的只能按关键字传递
/前面的只能按位置传递
函数的异常和处理
嵌套:找出f(n)和f(n-1)的关系
标签:11,return,函数,sum,编程,list,实验,print,def From: https://www.cnblogs.com/ciallo/p/17408180.html