task1.py
源代码
1 print(sum) 2 sum = 42 3 print(sum) 4 def inc(n): 5 sum = n+1 6 print(sum) 7 return sum 8 sum = inc(7) + inc(7) 9 print(sum)
运行截图
答:不是同一个对象的名称。line1是内置作用域, line3,line11的sum是全局作用域,line7的sum是局部作用域。
task2_1.py
源代码
1 def func1(a, b, c, d, e, f): 2 return [a,b,c,d,e,f] 3 def func2(a, b, c,*, d, e, f): 4 return [a,b,c,d,e,f] 5 def func3(a, b, c, /, d, e, f): 6 return [a,b,c,d,e,f] 7 8 print( func1(1,9,2,0,5,3) ) 9 print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) 10 print( func1(1,9,2, f=3, d=0, e=5)) 11 12 print( func2(11, 99, 22, d=0, e=55, f=33) ) 13 print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) 14 15 print( func3(111, 999, 222, 0, 555, 333)) 16 print( func3(111, 999, 222, d=0, e=555, f=333) )
运行截图
task2_2.py
源代码
1 list1 = [1, 9, 8, 4] 2 print( sorted(list1) ) 3 print( sorted(list1, reverse=True) ) 4 print( sorted(list1, True) )
运行截图
答:内置函数sorted()中参数reverse一定要用关键字传递。
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 :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 if delta >= 0: 12 root1 = p1 + p2 13 root2 = p1 - p2 14 else: 15 root1 = complex(p1, p2) 16 root2 = complex(p1, -p2) 17 return root1, root2 18 19 print(solve.__doc__) 20 while True: 21 try: 22 t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') 23 if t == '#': 24 print('结束计算,退出') 25 break 26 a, b, c = map(float, t.split()) 27 if a == 0: 28 raise ValueError('a = 0, 不是一元二次方程') 29 except ValueError as e:
运行截图
task4.py
源代码
1 def list_generator(a,b,c=1): 2 lst = [] 3 while a<=b: 4 lst.append(a) 5 a += c 6 return lst 7 8 list1 = list_generator(-5, 5) 9 print(list1) 10 list2 = list_generator(-5, 5, 2) 11 print(list2) 12 list3 = list_generator(1, 5, 0.5) 13 print(list3)
运行截图
task5.py
源代码
1 def is_prime(n): 2 if n == 2: 3 return True 4 elif n != 1: 5 for i in range(2,n): 6 if n == 2: 7 return True 8 elif n % i == 0: 9 break 10 else: 11 return True 12 13 for i in range(2,20+1,2): 14 flag = True 15 for j in range(2, i): 16 if is_prime(j) == True and is_prime(i - j) == True and flag == True: 17 print(f'{i}={j}+{i-j}') 18 flag = False
运行截图
task6.py
源代码
1 def encoder(text): 2 new_text1 = '' 3 for i in text: 4 if 'a' <= i <='z': 5 i = chr((ord(i)-97+5)%26 + 97) 6 new_text1 += i 7 elif 'A' <= i <= 'Z': 8 i = chr((ord(i)-65+5)%26 + 65) 9 new_text1 += i 10 else:new_text1 += i 11 return new_text1 12 13 def decoder(text): 14 new_text2 = '' 15 for i in text: 16 if 'a' <= i <='z': 17 i = chr((ord(i)-97-5)%26 + 97) 18 new_text2 += i 19 elif 'A' <= i <= 'Z': 20 i = chr((ord(i)-65-5)%26 + 65) 21 new_text2 += i 22 else:new_text2 += i 23 return new_text2
运行截图
task7.py
源代码
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))
运行截图
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}')
运行截图
标签:return,函数,sum,编程,实验,print,源代码,True,def From: https://www.cnblogs.com/simple77/p/17422103.html