赞
踩
【写在这里,方便右键百度搜索!】
《Python程序设计》题库
python里面and和or用法:https://blog.csdn.net/weixin_42859280/article/details/93605460
https://blog.csdn.net/weixin_42859280/article/details/93662815
代码1:
>>> try:
fp = open(r'd:\test.txt')
print('Hello world!', file=fp)
finally:
fp.close()
代码2:
>>> try:
fp = open(r'd:\test.txt', 'a+')
print('Hello world!', file=fp)
finally:
fp.close()
答:
假设文件“D:\test.txt”不存在,那么第一段代码会抛出异常,提示fp没有定义;第二段代码执行正常。原因是第二段代码使用内置函数open()打开指定文件时如果不存在则会创建该文件,从而不会抛出异常。
答:
fp = open(r’D:\test.txt’, ‘a+’)
print(‘hello world’, file=fp)
fp.close()
x = list(range(500))
for item in x:
t = 5**5
print(item+t)
答:
x = list(range(500))
t = 5**5
for item in x:
print(item+t)
答:
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)
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
def Sum(a, b=3, c=5):
return sum([a, b, c])
print(Sum(a=8, c=2))
print(Sum(8))
print(Sum(8,2))
答:
13
16
15
def Sum(*p):
return sum(p)
print(Sum(3, 5, 8))
print(Sum(8))
print(Sum(8, 2, 10))
答:
16
8
20
答:
import math
def IsPrime(v):
n = int(math.sqrt(v)+1)
for i in range(2,n):
if v%i==0:
return 'No'
else:
return 'Yes'
答:
def Sorted(v):
t = v[::]
r = []
while t:
tt = min(t)
r.append(tt)
t.remove(tt)
return r
答:
import random
x = [random.randint(0,100) for i in range(20)]
print(x)
y = x[0:10]
y.sort()
x[0:10] = y
y = x[10:20]
y.sort(reverse=True)
x[10:20] = y
print(x)
答:
x = input('Please input an integer of 4 digits meaning the year:')
x = eval(x)
if x%400==0 or (x%4==0 and not x%100==0):
print('Yes')
else:
print('No')
x | y |
x<0 | 0 |
0<=x<5 | x |
5<=x<10 | 3x-5 |
10<=x<20 | 0.5x-2 |
20<=x | 0 |
答:
x = input('Please input x:')
x = eval(x)
if x<0 or x>=20:
print(0)
elif 0<=x<5:
print(x)
elif 5<=x<10:
print(3*x-5)
elif 10<=x<20:
print(0.5*x-2)
class Test:
def __init__(self, value):
self.__value = value
@property
def value(self):
return self.__value
t = Test(3)
t.value = 5
print(t.value)
答:
不能运行。程序中定义的是只读属性,不能修改属性的值。
import random
x = [random.____________(1,20) for i in range(_______)]
r = dict()
for i in x:
r[i] = r.get(i, _____)+1
for k, v in r.items():
print(k, v)
答:
分别填写randint、50、0
def main():
if __name__ == '__main__':
print(1)
else:
print(2)
main()
将该程序文件直接运行时输出结果为_________,作为模块导入时得到结果___________-。(1、2)
s = 0
for i in range(1,101):
s += i
else:
print(1)
s = 0
for i in range(1,101):
s += i
if i == 50:
print(s)
break
else:
print(1)
from random import randint
result = set()
while True:
result.add(randint(1,10))
if len(result)==20:
break
print(result)
答:无法正确执行,因为该程序的功能是从[1,10]区间中选择20个不同的随机整数,而该区间并没有这么多整数,所以程序死循环。
>>> x = list(range(20))
>>> for i in range(len(x)):
del x[i]
答:无法正确执行,因为删除列表元素时会影响其他元素在列表中的索引,上面的代码会抛出下标越界的异常。
>>> x = list(range(20))
>>> for index, value in enumerate(x):
if value == 3:
x[index] = 5
答:将列表x中值为3的元素修改为5。
>>> x = [range(3*i, 3*i+5) for i in range(2)]
>>> x = list(map(list, x))
>>> x = list(map(list, zip(*x)))
答:首先生成一个包含列表的列表,然后模拟矩阵转置。
>>> import string
>>> x = string.ascii_letters + string.digits
>>> import random
>>> print(''.join(random.sample(x, 10)))
答:输出由英文字母大小写或数字组成的长度为10且不重复的随机字符串。
def demo(*p):
return sum(p)
print(demo(1,2,3,4,5))
print(demo(1,2,3))
答:输出结果为
15
6
def demo(a, b, c=3, d=100):
return sum((a,b,c,d))
print(demo(1, 2, 3, 4))
print(demo(1, 2, d=3))
答:输出结果为
10
9
def demo():
x = 5
x = 3
demo()
print(x)
def demo(lst, k):
if k<len(lst):
return lst[k:]+lst[:k]
def demo(n):
b_n = bin(n)
index = b_n.rfind('1') + 1
return len(b_n[index:])
def demo(n):
if n == 1:
return 0
if n == 2:
return 1
m, c = divmod(n, 2)
return m + demo(c+m)
>>> for x in range(21):
for y in range(34):
z = 100-x-y
if z%3==0 and 5*x + 3*y + z//3 == 100:
print(x,y,z)
0 25 75
4 18 78
8 11 81
12 4 84
>>> [(x, y, 100-x-y) for x in range(21) for y in range(34) if (100-x-y)%3==0 and 5*x+3*y+(100-x-y)//3==100]
[(0, 25, 75), (4, 18, 78), (8, 11, 81), (12, 4, 84)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。