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)
运行结果:
问题:不是,line1是build-in function sum,line3是指引入的sum = 42,line7是指sum = n + 1,line11是指sum = Inc(7)+ Inc(7)
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 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 17 def func3(a,b,c,/,d,e,f): 18 ''' 19 返回参数a,b,c,d,e,f构成的列表 20 /前面参数只能按位置传递 21 ''' 22 return[a,b,c,d,e,f] 23 24 25 # func1调用:按位置传递、按参数传递都可以 26 print(func1(1,9,2,0,5,3)) 27 print(func1(a=1,b=9,c=2,d=0,e=5,f=3)) 28 print(func1(1,9,2,f=3,d=0,e=5)) 29 30 31 # func2调用:d,e,f必须按照关键字传递 32 print(func2(11,99,22,d=0,e=55,f=33)) 33 print(func2(a=11,b=99,c=22,d=0,e=55,f=33)) 34 35 36 # func3调用:a,b,c必须按照位置传递 37 print(func3(111,999,222,0,555,333)) 38 print(func3(111,999,222,d=0,e=555,f=333))
运行结果:
试错结果:
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))
运行结果:
问题:需要
task2_3.py
运行代码:
1 def func(a,b,c,/,*,d,e,f): 2 return([a,b,c,d,e,f]) 3 print(func(1,2,3,d=4,e=5,f=6))
运行结果:
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()
运行结果:
task4.py
运行代码:
1 # list_generator()函数定义 2 # 待补足 3 # xxx 4 def list_generator(a,b,c = 1): 5 list = [] 6 while a <= b: 7 list.append(a) 8 a += c 9 return list 10 11 12 13 list1 = list_generator(-5,5) 14 print(list1) 15 16 list2 = list_generator(-5,5,2) 17 print(list2) 18 19 list3 = list_generator(1,5,0.5) 20 print(list3)
运行结果:
task5.py
运行代码:
1 def is_prime(n): 2 if n == 2: 3 return False 4 else: 5 for i in range(2,n): 6 if n % i == 0: 7 return False 8 break 9 else: 10 return True 11 for i in range(4,21,2): 12 for x in range(2,i): 13 if is_prime(x): 14 if (i-x)>1 and is_prime(i-x): 15 print(f'{i} = {x} + {i-x}') 16 break
运行结果:
task6.py
运行代码:
1 def encoder(text): 2 text1 = '' 3 for i in text: 4 if 'a' <= i <= 'z': 5 c = 97 + (ord(i) + 5 - 97) % 26 6 text1 += chr(c) 7 elif 'A' <= i <= 'Z': 8 c = 65 + (ord(i) + 5 - 97) % 26 9 text1 += chr(c) 10 else: 11 text1 += i 12 return text1 13 14 def decoder(text): 15 text1 = '' 16 for i in text: 17 if 'a' <= i <= 'z': 18 c = 65 + (ord(i) - 5 - 65) % 26 19 text1 += chr(c) 20 elif 'A' <= i <= 'Z': 21 c = 65 + (ord(i) - 5 - 65) % 26 22 text1 += chr(c) 23 else: 24 text1 += i 25 return text1 26 text = input('输入英文文本: ') 27 28 encoded_text = encoder(text) 29 print('编码后的文本: ', encoded_text) 30 31 decoded_text = decoder(encoded_text) 32 print('对编码后的文本解码: ', decoded_text)
运行结果:
task7.py
运行代码:
1 def collatz(n): 2 x = [] 3 x.append(int(n)) 4 while n != 1: 5 if n % 2 == 0: 6 n = n/2 7 x.append(int(n)) 8 elif n % 2 != 0: 9 n = n*3 + 1 10 x.append(int(n)) 11 else: 12 if n == 1: 13 x.append(int(n)) 14 return x 15 try: 16 n = int(input('Enter a positive integer:')) 17 if n <= 0: 18 raise ValueError 19 except ValueError: 20 print('Error: must be a positive integer') 21 else: 22 print(collatz(n))
运行结果:
task8.py
运行代码:
1 def func(n): 2 if n == 0: 3 return 0 4 else: 5 return 2*func(n-1) + 1 6 7 while True: 8 x = input() 9 if x == '#': 10 print('计算结束') 11 break 12 n = int(x) 13 ans = func(n) 14 print(f'n = {n}, ans = {ans}')
运行结果:
标签:py,return,函数,sum,编程,实验,运行,print,def From: https://www.cnblogs.com/hyy052416/p/17408106.html