Python基础100题打卡Day19

题目七十五

请编写一个程序,随机打印7到15之间的整数。

提示:
使用random.randrange()函数

用法:
random.randrange ([start,] stop [,step])是返回从开始到结束数字集合的一个随机值,步长为step

代码实现

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

运行结果

14

题目七十六

请编写一个程序来压缩和解压字符串“hello world!hello world!hello world!hello world!”。

提示:
使用zlib.compress()进行压缩字符串
使用zlib.decompress()进行解压字符串

代码实现

import zlib

t = zlib.compress(b'hello world!hello world!hello world!hello world!')
print(t)
print(zlib.decompress(t))

运行结果

b'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaIQ\xcc \x82\r\x00\xbd[\x11\xf5'
b'hello world!hello world!hello world!hello world!'

题目七十七

请编写一个程序来打印“1+1”的运行时间100次。

提示:
使用timeit()函数

代码实现

方法一

import datetime

before = datetime.datetime.now()
for i in range(100):
    x = 1 + 1
after = datetime.datetime.now()
execution_time = after - before
print(execution_time.microseconds)

方法二

import time

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

运行结果

0.0

题目七十八

请写一个程序洗牌和打印名单[3,6,7,8]。

提示:
使用shuffle()函数

用法:
shuffle(lst) ,lst可以是序列或者元组,返回的是一个随机排列的序列

代码实现

方法一:

import random
lst = [1,2,3,4,5]
random.shuffle(lst)
print(lst)

方法二:

import random

# 添加随机种子,确定随机排序的顺序
lst = [3, 6, 7, 8]
seed = 7
random.Random(seed).shuffle(lst)
print(lst)

运行结果

[8, 6, 3, 7]

题目七十九

请编写一个程序,生成主语在[“i”、“you”]和动词在[“play”、“Love”中,宾语在[“Hockey”,“Football”]中的所有句子。

提示:
使用.format函数输出

代码实现

lst1 = ["I", "You"]
lst2 = ["Play", "Love"]
lst3 = ["Hockey", "Football"]
for i in lst1:
    for j in lst2:
        for k in lst3:
            print("{} {} {}".format(i, j, k))

运行结果

I Play Hockey
I Play Football
I Love Hockey
I Love Football
You Play Hockey
You Play Football
You Love Hockey
You Love Football
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值