实验任务1
源代码:
print(sum) sum = 42 print(sum) def inc(n): sum = n+1 print(sum) return sum sum = inc(7) + inc(7) print(sum)
运行结果:
问题:task1.py源码中,共有4处有python语句 print(sum) (line1, line3, line7, line11)。
这4处使用的标识符sum是同一个对象的名称吗?如果不是,请以文字方式回答这4处中标识符 sum的作用域。 答:不是。line1为内置作用域 ,line3和line11为全局作用域,line7为局部作用域
实验任务2
task1
源代码:
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
源代码:
list1 = [1, 9, 8, 4] print( sorted(list1) ) print( sorted(list1, reverse=True) ) print( sorted(list1, True) )
实验结果:
task3
源代码:
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))
实验结果:
实验任务3
源代码:
def list_generator(a,b,c=1): t = [] i = a while i <= b: t.append(i) i += 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)
实验结果:
实验任务4
源代码:
def is_prime(n): i = 2 while i < n: if n % i == 0: break i += 1 return i == n lst = [] for t in range(2,21): if is_prime(t) == True: lst.append(t) k = 0 while k <= 20: a = 2 while a <= 20: b = 2 while b <= 20: if a in lst and b in lst and a + b == k and k%2 ==0 and a <= b: print(k,'=',a,'+',b) k += 1 b += 1 a += 1 k += 1
实验结果:
实验任务5
源代码:
def encoder(text): lst = list(text) text1 = [] for i in lst: if ord(i) in range(97,97+26) or ord(i.lower()) in range(97,97+26): if 'a' <= i <= 'u' or 'A' <= i <= 'U': t = chr(ord(i) + 5) text1.append(t) elif 'v' <= i <= 'z' or 'V' <= i <= 'Z': t = chr(ord(i) + 5 - 26) text1.append(t) else: text1.append(i) text = ''.join(text1) return text def decoder(text): lst1 = list(text) text2 = [] for k in lst1: if ord(k) in range(97,97+26) or ord(k.lower()) in range(97,97+26): if 'a' <= k <= 'e' or 'A' <= k <= 'E': t = chr(ord(k) - 5 + 26) text2.append(t) elif 'f' <= k <= 'z' or 'F' <= k <= 'Z': t = chr(ord(k) - 5) text2.append(t) else: text2.append(k) text = ''.join(text2) return text text = input('输入英文文本: ') encoded_text = encoder(text) print('编码后的文本: ', encoded_text) decoded_text = decoder(encoded_text) print('对编码后的文本解码: ', decoded_text)
实验结果:
实验任务6
源代码:
def collatz(n): if n.isdigit() and int(n) > 0: n1 = int(n) lst = [n1] while n1 != 1: if n1%2 == 0: n1 = n1/2 lst.append(int(n1)) else: n1 = n1*3 + 1 lst.append(int(n1)) return lst else: return 'Error: must be a positive integer' n = input('Enter a positive integer:') x = collatz(n) print(x)
实验结果:
实验任务7
源代码:
def func(n): if n == 1: return 1 elif n > 1: 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}')
实验结果:
over!
标签:return,sum,实验,print,n1,源代码,def From: https://www.cnblogs.com/khyyyds/p/17426806.html