文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Python基础练习100题 ( 71

2023-01-31 08:18

关注

昨天和大家分享了61-70题,今天继续来刷71~80题

Question 71:

Please write a program to output a random number, which is divisible by 5 and 7, between 10 and 150 inclusive using random module and list comprehension.

解法一

import random
print (random.choice([i for i in range(10,151) if i%5==0 and i%7==0]))

解法二

import random
resp = [i for i in range(10,151) if i % 35 == 0 ]
print(random.choice(resp))

Question 72:

Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.

解法一

import random
resp = random.sample(range(100,201),5)
print(resp)

Question 73:

Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.

解法一

import random
numbers = random.sample(range(100,201,2),5)
print(numbers)

解法二

import random
print (random.sample([i for i in range(100,201) if i%2==0], 5))

Question 74:

Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.

解法一

import random
lst = [i for i in range(1,1001) if i%35 == 0]
resp = random.sample(lst,5)
print(resp)

Question 75:

Please write a program to randomly print a integer number between 7 and 15 inclusive.

解法一

import random
number= random.choice([x for x in range(7,16)])
print(number)

解法二

import random
print(random.randrange(7,16))

Question 76:

Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".

解法一

import zlib
s = 'hello world!hello world!hello world!hello world!'.encode()
t = zlib.compress(s)
print(t)
print(zlib.decompress(t))

Question 77:

Please write a program to print the running time of execution of "1+1" for 100 times.

解法一

from timeit import Timer
t = Timer("for i in range(100):1+1")
t.timeit()

解法二

import time
before = time.time()
for i in range(100):
    x = 1 + 1
after = time.time()
execution_time = after - before
print(execution_time)

Question 78:

Please write a program to shuffle and print the list [3,6,7,8].

解法一

import  random  
lst  =  [3,6,7,8]  
random.shuffle(lst)  
print(lst) 

解法二

import random
lst = [3,6,7,8]
seed = 7
random.Random(seed).shuffle(lst)
print(lst)

Question 79:

Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].

解法一

subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]

res = [[i, j, k] for i in subjects  
                 for j in verbs 
                 for k in objects] 
for x in res:
    print(" ".join(x))

Out:
  
    I Play Hockey 
    I Play Football 
    I Love Hockey 
    I Love Football 
    You Play Hockey 
    You Play Football 
    You Love Hockey 
    You Love Football

解法二


subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]

for sub in subjects:
    for verb in verbs:
        for obj in objects:
            print("{}  {}  {}".format(sub,verb,obj))
Out:
    I Play Hockey 
    I Play Football 
    I Love Hockey 
    I Love Football 
    You Play Hockey 
    You Play Football 
    You Love Hockey 
    You Love Football

解法三


import itertools 

subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
all_list = [subjects,verbs,objects]

res = list(itertools.product(*all_list)) 
for x in res:
    print(" ".join(x))

Out:
    I Play Hockey 
    I Play Football 
    I Love Hockey 
    I Love Football 
    You Play Hockey 
    You Play Football 
    You Love Hockey 
    You Love Football

Question 80:

Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24].

解法一

def isEven(n):
    return n%2!=0

li = [5,6,77,45,22,12,24]
lst = list(filter(isEven,li))
print(lst)

解法二

li  =  [5,6,77,45,22,12,24]  lst  =  list(filter(lambda  n:n%2!=0,li))  
print(lst)

这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

  • Python 71 - 80题

我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在Python3中是input()
  • print需要加括号
  • fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯