task1
line1 B68ZZZ33344uilt-in
line3Global
line7Local
line11Enclosing
task2.1
def func1(a, b, c, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 默认,参数按位置传递; 也支持关键字传递 ''' return [a,b,c,d,e,f] def func2(a, b, c,*, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 *后面的参数只能按关键字传递 ''' return [a,b,c,d,e,f] def func3(a, b, c, /, d, e, f): ''' 返回参数a,b,c,d,e,f构成的列表 /前面的参数只能按位置传递 ''' return [a,b,c,d,e,f] # func1调用:按位置传递、按参数传递都可以 print( func1(1,9,2,0,5,3) ) print( func1(a=1, b=9, c=2, d=0, e=5, f=3) ) print( func1(1,9,2, f=3, d=0, e=5)) # func2调用:d,e,f必须按关键字传递 print( func2(11, 99, 22, d=0, e=55, f=33) ) print( func2(a=11, b=99, c=22, d=0, e=55, f=33) ) # func3调用:a,b,c必须按位置传递 print( func3(111, 999, 222, 0, 555, 333)) print( func3(111, 999, 222, d=0, e=555, f=333) )
task2.2
list1 = [1, 9, 8, 4] print(sorted(list1)) print(sorted(list1, reverse=True)) print(sorted(list1, True))
task2.3
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
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
def list_generator(a,b,c=1): t=[] if type(c)==int: for i in range(a-1,b,c): t.append(i) else: j=a while j<=b: t.append(j) j=j+c return(t) 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
import math def is_prime(m): result = True if m <= 1: if True: result = False for i in range(2, int(math.sqrt(m) + 1)): if m % i == 0: result = False elif m > 1: if m == 2: result = True elif m % 2 == 0: result = False elif m > 2: a = int(math.sqrt(m)) + 1 for j in range(3, a, 2): if m % j == 0: result = False else: result = True else: result = False return result t=[] for i in range(1,21): if is_prime(i)==True: t.append(i) else: continue print(t) for i in t: for j in t: if ((i + j)%2==0)and((i+j)<=20): print(f'{i+j}={i}+{j}')
def encoder(m): t=[] for i in m: j=ord(i) if (65<=j<=85)or(97<=j<=117): j = j+5 elif(86<=j<=90)or(118<=j<=122): j=j+5-26 else: j=j i = chr(j) t.append(i) n=''.join(t) return n def decoder(m): t1=[] for i in m: j=ord(i) if (122>=j >=102)or(90>=j>=70): j = j-5 elif(101>=j>=97)or(69>=j>=65): j = j-5+26 else: j=j i =chr(j) t1.append(i) n = ''.join(t1) return n text = input('输入英文文本: ') encoded_text = encoder(text) print('编码后的文本: ', encoded_text) decoded_text = decoder(encoded_text) print('对编码后的文本解码: ', decoded_text)
task7
def collatz(i): t=[i] while i!=1: if i%2==0: i=i/2 t.append(int(i)) elif i%2==1: i=i*3+1 t.append(int(i)) return t try: i=int(input('Enter a positive integer:')) if i<=0: raise ValueError except ValueError: print('Error:must be a positive integer') else: print(collatz(i))
task8
def func(n): if n == 1: return 1 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}')
标签:return,text,else,实验,print,True,def From: https://www.cnblogs.com/yuanlinuist/p/17408170.html