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
list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
运行结果
答:内置函数sorted()中参数reverse一定要用关键字传递
task2_3.py
def func(a, b, c, /, *, d, e, f): return( [a,b,c,d,e,f] ) print(func(1,2,3,d=4,e=5,f=6))
运行结果
task3.py
def solve(a, b, c): ''' 求解一元二次方程, 返回方程的两个根 :para: a,b,c: float 方程系数 :return: tuple ''' delta = b*b - 4*a*c delta_sqrt = abs(delta)**0.5 p1 = -b/2/a p2 = delta_sqrt/2/a if delta >= 0: root1 = p1 + p2 root2 = p1 - p2 else: root1 = complex(p1, p2) root2 = complex(p1, -p2) return root1, root2 print(solve.__doc__) while True: try: t = input('输入一元二次方程系数a b c, 或者,输入#结束: ') if t == '#': print('结束计算,退出') break a, b, c = map(float, t.split()) if a == 0: raise ValueError('a = 0, 不是一元二次方程') except ValueError as e: print(repr(e)) print() except: print('有其它错误发生\n') else: root1, root2 = solve(a, b, c) print(f'root1 = {root1:.2f}, root2 = {root2:.2f}') print()
运行结果
task4.py
def list_generator(a,b,c=1): lst = [] while a<=b: lst.append(a) a += c return lst list1 = list_generator(-5, 5) print(list1) list2 = list_generator(-5, 5, 2) print(list2) list3 = list_generator(1, 5, 0.5) 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 ls=list(text) 3 for i in range(len(ls)): 4 if 'a'<=ls[i].lower()<='u': 5 ls[i]=chr(ord(ls[i])+5) 6 elif 'v'<=ls[i].lower()<='z': 7 ls[i]=chr(ord(ls[i])-21) 8 else: 9 pass 10 return ''.join(ls) 11 12 def decoder(text): 13 ls=list(text) 14 for i in range(len(ls)): 15 if 'f'<=ls[i].lower()<='z': 16 ls[i]=chr(ord(ls[i])-5) 17 elif 'a'<=ls[i].lower()<='e': 18 ls[i]=chr(ord(ls[i])+21) 19 else: 20 pass 21 return ''.join(ls) 22 23 24 text = input('输入英文文本: ') 25 26 encoded_text = encoder(text) 27 print('编码后的文本: ', encoded_text) 28 29 decoded_text = decoder(encoded_text) 30 print('对编码后的文本解码: ', decoded_text)
运行结果
task7.py
def collatz(n): lst = [] lst.append(n) while n != 1: if n %2 == 0: n = n/2 lst.append(int(n)) else: n = n*3 +1 lst.append(int(n)) return lst try: n = int(input('Enter a positive integer:')) if n <= 0: raise ValueError except ValueError: print('Error: must be a positive integer') else: print(collatz(n))
运行结果
task8.py
def func(n): if n == 0: return 0 else: return 2*func(n-1) + 1 while True: x = input() if x == '#': print('计算结束') break n = int(x) ans = func(n) print(f'n = {n}, ans = {ans}')
运行结果
标签:py,return,函数,sum,编程,实验,print,True,def From: https://www.cnblogs.com/xixihamugua/p/17417480.html