实验任务1
task1.py
实验源码:
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)
实验结果:
实验任务2
task2_1.py
实验源码:
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 def func2(a,b,c,*,d,e,f): 9 ''' 10 返回参数a,b,c,d,e,f构成的列表 11 /前面的参数只能按关键字传递 12 ''' 13 return [a,b,c,d,e,f] 14 15 def func3(a,b,c,/,d,e,f): 16 ''' 17 返回参数a,b,c,d,e,f构成的列表 18 /前面的参数只能按位置传递 19 ''' 20 return [a,b,c,d,e,f] 21 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 29 # func2调用:d,e,f必须按关键字传递 30 print( func2(11, 99, 22, d=0, e=55, f=33) ) 31 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 32 33 34 # func3调用:a,b,c必须按位置传递 35 print( func3(111, 999, 222, 0, 555, 333)) 36 print( func3(111, 999, 222, d=0, e=555, f=333))
实验结果:
在line31后加一行函数调用,运行截图为:
在line36后面加一行函数调用,运行截图为:
task2_2.py
实验源码:
1 list1 = [1, 9, 8, 4] 2 3 print( sorted(list1)) 4 print( sorted(list1, reverse=True) ) 5 print( sorted(list1, True) )
实验结果:
python内置函数sorted()中,参数reverse的传递方式必须使用关键字传递
task2_3.py
实验源码:
1 def func(a, b, c, /, *, d, e, f): 2 return( [a,b,c,d,e,f] ) 3 4 print( func(1,2,3, d=4, e=5, f=6) )
实验结果:
实验任务3
task3.py
实验源码:
1 def solve(a, b, c): 2 ''' 3 求解一元二次方程,返回方程的两个根 4 5 :para: a,b,c: float 方程系数 6 :return: tuple 7 ''' 8 delta = b*b- 4*a*c 9 delta_sqrt = abs(delta)**0.5 10 p1 = -b/2/a 11 p2 = delta_sqrt/2/a 12 13 if delta >=0: 14 root1 = p1 + p2 15 root2 = p1 - p2 16 else: 17 root1 = complex(p1, p2) 18 root2 = complex(p1, -p2) 19 20 return root1, root2 21 22 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()
实验结果:
在line23前面加一行代码,运行截图为:
实验任务4
task4.py
实验源码:
1 def list_generator(start, stop, step=1): 2 3 result = [] 4 num = start 5 while num < stop: 6 result.append(num) 7 num += step 8 return result 9 10 list1 = list_generator(-5, 5) 11 print(list1) 12 list2 = list_generator(-5, 5, 2) 13 print(list2) 14 list3 = list_generator(1, 5, 0.5) 15 print(list3)
实验结果:
实验任务5
task5.py
实验源码:
1 def is_prime(num): 2 if num == 1: 3 return False 4 elif num == 2: 5 return True 6 else: 7 for i in range(2, num): 8 if num % i == 0: 9 return False 10 return True 11 12 def goldbach_conjecture(num): 13 primes = [] 14 for i in range(2, num): 15 if is_prime(i): 16 primes.append(i) 17 for prime in primes: 18 if num - prime in primes: 19 return f"{num} = {prime} + {num-prime}" 20 return None 21 22 for num in range(4, 21, 2): 23 expression = goldbach_conjecture(num) 24 if expression: 25 print(expression)
实验结果:
实验任务6
task6.py
实验源码:
1 def encoder(text): 2 result = '' 3 for c in text: 4 if c.isalpha(): 5 if c.islower(): 6 result += chr((ord(c) - 97 + 5) % 26 + 97) 7 else: 8 result += chr((ord(c) - 65 + 5) % 26 + 65) 9 else: 10 result += c 11 return result 12 def decoder(text): 13 decoded_text = "" 14 for a in text: 15 if a.isalpha(): 16 if a.islower(): 17 decoded_text += chr((ord(a) - 97 + 21) % 26 + 97) 18 else: 19 decoded_text += chr((ord(a) - 65 + 21) % 26 + 65) 20 else: 21 decoded_text += a 22 return decoded_text 23 text = input('输入英文文本: ') 24 encoded_text = encoder(text) 25 print('编码后的文本: ', encoded_text) 26 decoded_text = decoder(encoded_text) 27 print('对编码后的文本解码: ', decoded_text)
实验结果:
实验任务7
task7.py
实验源码:
1 def collatz(n): 2 if n%2==0: 3 return n//2 4 else: 5 return (n*3)+1 6 7 8 try: 9 n=eval(input('Enter a positive integer:')) 10 if type(n) is float or type(n) is str or n<=0: 11 raise ValueError('Error:must be a positive integer') 12 except ValueError as e: 13 print(repr(e)) 14 print() 15 except: 16 print('Error:must be a positive integer') 17 else: 18 num=collatz(n) 19 list=[n,num] 20 while True: 21 if num!=1: 22 num=collatz(num) 23 list.append(num) 24 else: 25 break 26 print(list)
实验结果:
实验任务8
task8.py
实验源码:
1 def func(n): 2 if n == 0: 3 return 1 4 else: 5 return 2 * func(n-1) + 1 6 while True: 7 x = input() 8 if x == '#': 9 print('计算结束') 10 break 11 n = int(x) 12 ans = func(n) 13 print(f'n = {n}, ans = {ans}')
实验结果:
标签:return,函数,text,编程,num,实验,print,def From: https://www.cnblogs.com/gaoyue124/p/17408138.html