1、编写程序,在D盘根目录下创建一个文本文件test.txt,并向其中写入字符串hello world
答:
fp = open(r’D:\test.txt’, ‘a+’)
print(‘hello world’, file=fp)
fp.close()
2、写出下面代码的优化版本,提高运行效率
x = list(range(500))
for item in x:
t = 5**5
print(item+t)
3、编写程序,生成一个包含20个随机整数的列表,然后对其中偶数下标的元素进行降序排列,奇数下标的元素不变。(提示:使用切片)
答:
import random
x = [random.randint(0,100) for i in range(20)]
print(x)
y = x[::2]
y.sort(reverse=True)
x[::2] = y
print(x)
4、写出下面代码的执行结果
def Join(List, sep=None):
return (sep or ',').join(List)
print(Join(['a', 'b', 'c']))
print(Join(['a', 'b', 'c'],':'))
答:
a,b,c
a:b:c
5、写出下面代码的运行结果
def Sum(a, b=3, c=