培训鉴定资源库

Python程序编写总复习参考答案

【文章来源:】 【作者:】 【发布时间:2022-06-08】 【点击量:

参考答案:

第一单元

选择题

1~5 DBACB  6~7 BC

判断题

1~5 错错对对对

填空题

1. 机器语言、汇编语言、高级语言

2. CC++C#JavaPython

3. 跨平台、面向对象

4. 数据分析、组件集成、网络服务、图像处理、科学计算

5. 更易理解、简化设计、易于学习、开源免费、可移植性、“胶水语言”

6. WindowsLinuxMac OS

7. 注释

8. 01

9. #””””””

程序设计题

10. print(“Hello world!”)

11. print(“1+2=3”)

第二单元

填空题

1. 赋值、类型

2. 内存地址、print(变量名)

3. 大写

4. 大写

5. 空格

6. 标识元组

7. 整型

8. 省略

9. \n

10. 不可变、不到、整个

11. 有序的、嵌套

12. 可变

选择题

1~5 DBCDB 6~10 DCCAB

判断题

1~5错对错错错 6~10 对错对对对

编程题

1

import random
M = []
for x in range(10):
      M.append(random.randint(1,100))
print(sorted(M))

2

:

Footballset = {'a', 'b', 'c', 'd'}
Basketballset = {'a', 'b', 'e', 'g'}
print(Footballset&Basketballset)

:

Footballset = {'a', 'b', 'c', 'd'}
Basketballset = {'a', 'b', 'e', 'g'}
print(Footballset^Basketballset)

 

第三单元

填空题

1. 123

123

2. 

3. break

4. continue

5. 6

6. else if

7. True

8. False

9. False

10. 换行

11. generator object

12. 2

13. 1,2,3,4,5,结束

14. 2

15. 'n \n

16. 1:2

选择题

1~5 ABCCC 6~10 DBDAD   11~15 CAABA

16~20 ADDBC  21~25 DACCA

程序题

1

for x in range(2000, 3201):
       if x % 7 == 0 and x % 5 != 0:
           print(x, end=',')

2.

n = eval(input('输入:'))
b= [z**2 for z in range(1,n+1)]
d = {}
for x,y in zip(range(1,n+1),b):
   d.update({x:y})
print(d)

3

str1 = input('请输入')
print(str1.upper())

4

for x in range(1000, 3001):
     a = x // 1000
     b = x % 1000 // 100
     c = x % 100 // 10
     d = x % 10
     if a % 2 and b % 2 and c % 2 and d % 2:
        print(a, b, c, d, sep='')

5

a = input('输入:').split()
b = [int(x) for x in a if eval(x) % 2]
print(*b, sep=',')

6

x = eval(input('输入第一个数:'))
y = eval(input('输入第二个数:'))
a, b = x, y
if x < y:
   x, y = y, x
while x % y != 0:
   x, y = y, x % y
print(y, a * b / y)

 

 

第四单元

 

填空题

1. [111, 33, 2]

2. len()

3. 逗号、冒号

4. 9

5. (3, 3, 3)

6. rR

7. [1, 4, 2, 3]

8. 6

9. choice()

10. 不重复

11. shuffle()

12. (1, 2)

13. 3

14. 位置参数、不定长参数

15. 函数

16. 修改

17. kwlist()

18. 模块

19. datetime

20. os

 

选择题

1~5 DCBDC 6~10 DDAAD 11~15 DCABD  16~20 DBCDC

判断题

1~5对错对错对  6~10对错对对错  11~15对错对对错

16~20对对对对对  21~25错对对对对

编程题

 

1

def cacluate(*args):  # 接收任意多个数,输出为元组

   avg = sum(args) / len(args)  # 计算平均数
   up_avg = []  # 定义一个空列表,存放比平均值大的所有数
   for item in args:  # 遍历所有数
      if item > avg:
         up_avg.append(item)  # 把比平均值大的加入到列表中
   return avg, up_avg  # 返回平均值,以及列表


print(cacluate(1, 2.3, 4, 5))  # 调用函数

2

def fun(str1):  # 定义函数,接受一个字符串参数

   upper = lower = 0  # 定义计数器
   for i in str1:  # 遍历字符串
      if i.isupper():  # 如果为大写字母,upper的值加一
         upper += 1
      elif i.islower():  # 如果为小写,lower值加一
         lower += 1
      else:
         continue  # 既不是大写又不是小写,继续执行
   return upper, lower  # 返回大写和小写的数值


print(fun('HELLO westos!'))  # 调用丞数,其中!不包括在upperlower的值中

3

import random

list1 = []
for i in range(30):
   list1.append(random.randint(1, 100))  # 生成一个有301~100随机数的列表
print(list1)


def Fun(list1, k):  # 传入两个参数
   if type(k) is not int or k > len(list1):  # 判断传入的参数k值是否合法
      return 'error'  # 不合法返回error
   else:
      return list1[:k][::-1] + list1[k:][::-1]  # 合法进行反转


print(Fun(list1, 9))  # 调用函数

4

def f(n):
   n = str(n)  # 将数字转换为字符串
   sum1 = 0  # 定义计数器
   for item in n:  # 遍历字符串
      sum1 += int(item) ** 2  # 计算各位数字的平方和
   return sum1  # 返回sum


s = input()  # 接受变量k,a,b
li = []  # 存储变量k,a,b

for item in s.split():
   li.append(int(item))
k, a, b = li

count = 0
# 判断是否满足条件
for i in range(a, b + 1):
   if k * f(i) == i:
      count += 1
print(count)

5

def printkeys(x):
   '''该函数可以生成一个字典,其中键是1x之间的数字(都包括在内),值是键的平方,并只打印键'''
   diction = {i: i ** 2 for i in range(1, 21)}
   print(diction.values())


# return是有返回值的画数没有return就不返回借
x = input("请输入一个整数:")
printkeys(x)

6

num = int(input('n:'))
dict = {}
for i in range(num):
   m, n = map(int, input().split())
   if m in dict:
      dict[m] += n
   else:
      dict[m] = n
dic = sorted(dict.keys())
for i in dic:
   print(i, dict[i])

7

s = input('s:')
print(" ".join(s.split()[::-1]))

8

import random


def rewordfun():
   # 定义用来记录各个中奖等级人数的变量
   onecount = 0
   twocount = 0
   threecont = 0
   # 计算中奖等级人数
   for i in range(1000):
      # random.random():生成0-1的随机数
      # format:格式转换,保留两位小数
      # print(random.random())
      num = float(format(random.random(), '.2f'))
      # print(num)
      if 0 <= num < 0.08:
         onecount += 1
      elif 0.08 <= num < 0.3:
         twocount += 1
      else:
         threecont += 1
   # 返回各个中奖等级人数
   return onecount, twocount, threecont


# 将函数返回值赋给变量
count01, count02, count03 = rewordfun()  # 定义字典
rewordDict = {'一等奖': count01, '二等奖': count02, '三等奖': count03}

# 遍历输出字典的key-value
for k, v in rewordDict.items():
   print(k, '--->', v)

9

def replace_dict(dict1):
   dict2 = {}
   for x in dict1:
      dict2[dict1[x]] = x
   return dict2


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1 = replace_dict(dict1)
print(dict1)

10

def isdigit(s1):
   flag = True
   for x in s1:
      try:
         int(x)
      except ValueError:
         flag = False
   return flag


str1 = input('请输入字符串:')
print(isdigit(str1))

 

第五单元

1

arr = []
for i in range(1, 5):
   for j in range(1, 5):
      for k in range(1, 5):
         num = 100 * i + 10 * j + k
         if i != j and j != k and i != k and num not in arr:
            # 互不相同且无重复数字的三位数
            arr.append(num)

print(len(arr), arr)

2

for i in range(1, 10):
   for j in range(1, 10):
      if j <= i:
         string = '%d*%d=%d' % (j, i, j * i)
         print('%-7s' % string, end='')
   print('')

3

l = [0,1]
for i in range(10):
   arr = l[-2:]
   l.append(arr[0]+arr[1])
print(l)

4

m1 = 1  # 满月
m2 = 0  # 满俩月(下个月生兔子)
mm = 0  # 可以月月生兔子了
for i in range(1, 13):
   # 过了1个月后
   mm = mm + m2  # 加入新增成年的兔子
   m2 = m1  # 满月的变成满俩月
   m1 = mm  # 这个月新出生兔子
print(i, mm + m1 + m2)  # 每个月有多少对兔子

5

arr = []
for i in range(101, 201):
   k = 0
   for j in range(2, i - 1):
      if i % j == 0:
         k = 1
         break
   if k == 0:
      arr.append(i)
print('共有素数{}个,满足条件的素数为:{}'.format(len(arr), arr))

6

for i in range(100, 1000):
   b = i // 100  # 百位
   s = i % 100 // 10  # 十位
   g = i % 10  # 个位
   if b ** 3 + s ** 3 + g ** 3 == i:
      print(i)
      # b,s,g=map(int,str(i)  利用map()函数更方便,也可以使用divmod()

7

n = n1 = int(input('输入一个大于1的正整数:'))
arr = []
k = 2
while k <= n:
   if n % k == 0:
      arr.append(k)
      n = n // k
      continue
   else:
      k = k + 1

arr = [str(i) for i in arr]
t = "*".join(arr)
print("{}=1*{}".format(n1, t))

8

def tm017():
   s = input('input a string:\n')
   letters, space, digit, others = 0, 0, 0, 0
   for c in s:
      if c.isalpha():
         letters += 1
      elif c.isspace():
         space += 1
      elif c.isdigit():
         digit += 1
      else:
         others += 1
   print('char = %d,space = %d,digit = %d,others = %d' % (letters, space, digit, others))


tm017()

9

for x in range(1, 1000):
   l = []
   for y in range(1, x):
      if x % y == 0:
         l.append(y)
      if sum(l) == x:
         l1 = [str(i) for i in l]
         b = '+'.join(l1)
         print(x, b, sep='=')
         break

10

num = 7
for i in range(num):
   blank = abs(num // 2 - i)
   print(' ' * blank + '*' * (num - 2 * blank) + ' ' * blank)

11

1

a, b, num = 2, 1, 0
for i in range(20):
   num += a / b
   a = a + b
   b = a - b
print(num)

2

s, t = 0, 1
for n in range(1, 21):
   t *= n
   s += t
print(s)

12

def st(str):
   if len(str) == 1:
      return str
   else:
      return st(str[1:]) + str[0]


str1 = input('请输入一串字符:')
print('原字符串为{}, 逆序{}'.format(str1, st(str1)))

13

def age(x):
   if x > 1:
      return 2 + age(x - 1)
   else:
      return 10


print(age(5))

14

num = input('请输入一串数字:')
s = num.strip()
for i in range(len(s) // 2):
   if s[i] != s[-i - 1]:
      print(False)
      break
   else:
      print('输入的数据是回文字{}'.format(s))

15

方法一:

l1 = [12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
for i in range(10):
   for j in range(i + 1, 10):
      if l1[i] < l1[j]:
         l1[i], l1[j] = l1[j], l1[i]
print(l1)

方法二:

[12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
print(sorted(l1)[::-1])

方法三:

[12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
l1.sort(reverse=True)
print(l1)

16

l1 = [12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
for i in range(10):
   for j in range(10 - i - 1):
      if l1[j] > l1[j + 1]:
         l1[j], l1[j + 1] = l1[j + 1], l1[j]
print(l1)

L1 = [12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
print(sorted(l1))

l1 = [12, 5, 34, 26, 0, 3, 9, 74, 25, 46]
l1.sort()
print(l1)

17

aaa = [1, 5, 8, 14, 28, 39, 60, 89, 134, 324, 612, 900]
b = 555
for a in aaa:
   if b < a:
      aaa.insert(aaa.index(a), b)
      break
else:
   aaa.append(b)
print(aaa)

18

方法一:

s = 0
for i in range(1, 101):
   s += i
print(s)

方法二:

nt(sum(range(1, 101)))

19

print('1'.center(26))
while len(arr) < 10:  # [1,1]
   a = [0] + arr  # [0,1,1]
   b = arr + [0]  # [1,1,0]
   arr = [a[i] + b[i] for i in range(len(a))]  # [1,2,1]
   s = [str(i) for i in arr]
   print(' '.join(s).center(26))

20

a = [6, 3, 10, 2, 5, 1, 4, 7, 9, 8]
i = a.index(max(a))
a[0], a[i] = a[i], a[0]
i = a.index(min(a))
a[-1], a[i] = a[i], a[-1]
print(a)

21

print(bin(10))  # 十转二
print(oct(10))  # 十转八
print(hex(10))  # 十转16
print(int('10', 8))  # 八转十
print(int('10', 2))  # 二转十
print(int('10', 16))  # 16转十

22

count = 0
for i in range(76543211):  # 能组成的最大数字也就是76543210
   s = str(i)  # 转换成文本形式s
   if '8' in s or '9' in s:  # s中不包含89
      continue
   else:
      cs = set([c for c in s])  # s中的数字去重,如果去重后和去重前长度一致,说明数字没有重复使用
   if len(s) == len(cs) and s[-1] in '13579':  # 各位不重复且是奇数
      count += 1
print(count)  # 结果是46972

23

def datapw():
   x = int(input('请输入4位的正整数:'))
   c = 5
   q, b, s, g = x // 1000, x // 100 % 10, x // 10 % 10, x % 10
   s = (g + c) % 10 * 1000 + (s + c) % 10 * 100 + (b + c) % 10 * 10 + (q + c) % 10
   print('加密后输出为{}'.format(s))


datapw()

24

方法一:

l = ['ak17', 'b51', 'b52', '#64']
d = {}
for i in range(len(l)):
   d[i] = l[i]
print(d)

方法二:

l = ['ak17', 'b51', 'b52', '#64']
d1 = dict(zip(range(4), l))
print(d1)

25

def pattern(n):
   for i in range(0, n):
      for j in range(0, i + 1):
         print(' * ', end='')
      print('\r')
   for i in range(n, 0, -1):
      for j in range(0, i + 1):
         print(' * ', end='')
      print('\r')


pattern(5)
print(' * ')

26

def   pattern(n):

k=2*n-2

for i in range(0.n-1):

for j in range(0, k):

print(end=”)

k=k-2

for j in range(0,i+ 1):

       print(*,end=)

print(")

k=-1

for i in range(n-1-11):

for j in range(k-1,-1):

print(end=”)

k=k+2

for j in range(0,1 1):

print("*",end="")

print()

pattern(5)

27

def pattem(n):
   for y in range(n, 0, -1):
      print(' ' * (5 - y), end='')
      if '*' * (y * 2 - 2):
         print('*' * (y * 2 - 2))
      else:
         break
   for x in range(1, n + 1):
      if x == 1:
         print('*')
      else:
         print(' ' * (5 - x), end='')
         print('*' * (x * 2 - 2))


pattem(5)

28

from turtle import *

speed(0)
hideturtle()
color('red', 'yellow')
begin_fill()
while True:
   forward(200)
   left(170)
   if abs(pos()) < 1:
      break
   end_fill()
done()

         29.

            s,t=0,1

for n in range(1,21):

              t*=n

               s+=t

           print(s)

        30.

           def   fac(x):

               if x>1:

                 return x*fac(x-1)

              else:

                return x

  

print(fac(5))

 


上一条:Python基础知识刷题

下一条:无

Baidu
map