实验任务1
task1
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 11 sum = inc(7) + inc(7) 12 print(sum)View Code
不是同一个对象名称
1.Built-in。2和3 Global。4.Local
实验任务2
task2-1
1 def func1(a, b, c, d, e, f): 2 return [a,b,c,d,e,f] 3 4 5 def func2(a, b, c,*, d, e, f): 6 return [a,b,c,d,e,f] 7 8 9 def func3(a, b, c, /, d, e, f): 10 return [a,b,c,d,e,f] 11 12 13 # func1调用:按位置传递、按参数传递都可以 14 print( func1(1,9,2,0,5,3) ) 15 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 16 print( func1(1,9,2, f=3, d=0, e=5)) 17 18 19 # func2调用:d,e,f必须按关键字传递 20 print( func2(11, 99, 22, d=0, e=55, f=33) ) 21 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 22 23 24 # func3调用:a,b,c必须按位置传递 25 print( func3(111, 999, 222, 0, 555, 333)) 26 print( func3(111, 999, 222, d=0, e=555, f=333) )View Code
task2-2
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )View Code
注:reverse的传递方式必须使用关键字。
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))View Code
实验任务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 22 print(solve.__doc__) 23 while True: 24 try: 25 t = input('输入一元二次方程系数a b c, 或者,输入#结束:') 26 if t == '#': 27 print('结束计算,退出') 28 break 29 a, b, c = map(float, t.split()) 30 if a == 0: 31 raise ValueError('a = 0, 不是一元二次方程') 32 except ValueError as e: 33 print(repr(e)) 34 print() 35 except: 36 print('有其它错误发生\n') 37 else: 38 root1, root2 = solve(a, b, c) 39 print(f'root1 ={root1:.2f},root2 = {root2:.2f}') 40 print()View Code
实验任务4
task4
1 # list_generator()函数定义 2 def list_generator(start, stop, step=1): 3 result = [] 4 i = start 5 while i <= stop: 6 result.append(i) 7 i += step 8 return result 9 10 11 list1 = list_generator(-5, 5) 12 print(list1) 13 list2 = list_generator(-5, 5, 2) 14 print(list2) 15 list3 = list_generator(1, 5, 0.5) 16 print(list3)View Code
实验任务5
task5
1 def is_prime(x): 2 for i in range(2,int(x**0.5+2)): 3 a=x%i 4 if a==0: 5 break 6 if a==0 and x!=2: 7 return False 8 elif a!=0 or x==2: 9 return True 10 11 12 for i in range(4,21,2): 13 for x in range(2,i): 14 y = i - x 15 if is_prime(x) and is_prime(y): 16 print(f'{i}={x}+{y}') 17 breakView Code
实验任务6
task6
1 def encoder(text): 2 x1 = '' 3 for i in text: 4 if 'a' <= i <= 'z': 5 i = chr((ord(i)-97+5) % 26 + 97) 6 x1 += i 7 elif 'A' <= i <= 'Z': 8 i = chr((ord(i)-65+5) % 26 + 65) 9 x1 += i 10 else:x1 += i 11 return x1 12 13 14 def decoder(text): 15 x2 = '' 16 for i in text: 17 if 'a' <= i <= 'z': 18 i = chr((ord(i)-97-5) % 26 + 97) 19 x2 += i 20 elif 'A' <= i <= 'Z': 21 i = chr((ord(i)-65-5) % 26 + 65) 22 x2 += i 23 else: 24 x2 += i 25 return x2 26 27 28 text = input('输入英文文本: ') 29 encoded_text = encoder(text) 30 print('编码后的文本: ', encoded_text) 31 decoded_text = decoder(encoded_text) 32 print('对编码后的文本解码: ', decoded_text)View Code
实验任务7
task7
1 def collatz(n): 2 lst = [] 3 lst.append(n) 4 while n != 1: 5 if n %2 == 0: 6 n = n/2 7 lst.append(int(n)) 8 else: 9 n = n*3 +1 10 lst.append(int(n)) 11 return lst 12 try: 13 n = int(input('Enter a positive integer:')) 14 if n <= 0: 15 raise ValueError 16 except ValueError: 17 print('Error: must be a positive integer') 18 else: 19 print(collatz(n))View Code
实验任务8
task8
1 def func(a): 2 if a == 0: 3 return 0 4 else: 5 return 2*func(a-1)+1 6 7 8 while True: 9 x = input() 10 if x == '#': 11 print('计算结束') 12 break 13 n = int(x) 14 ans = func(n) 15 print(f'n = {n}, ans = {ans}')View Code
标签:Code,return,函数,sum,编程,实验,print,def,View From: https://www.cnblogs.com/202280060019x/p/17407903.html