提交 d7fe9a50 编写于 作者: 梦想橡皮擦's avatar 梦想橡皮擦 💬

模块 random

上级 df7373ba
# python 题库 # python 题库
主要用于 python 基础知识点题库备份 主要用于 python 基础知识点题库备份.
\ No newline at end of file
相关博客
- [python 入门教程之每日 5 or 6 道题,列表推导式篇 | Python技能树征题](https://dream.blog.csdn.net/article/details/120476685)
- [三元表达式篇,python 入门教程之每日 5 or 6 道题 | Python技能树征题](https://dream.blog.csdn.net/article/details/120498213)
- [断言、with-as 篇,python 入门教程之每日 5 or 6 道题 | Python 技能树题库](https://dream.blog.csdn.net/article/details/120553317)
- [常用标准库 random,python 入门教程之每日 5 or 6 道题 | Python 技能树题库](https://dream.blog.csdn.net/article/details/120743071)
"""
题干(问题描述):
下述哪个选项能在 1~100 中的随机选择 10 个数字
"""
import random
for i in range(1, 6):
rand_num = random.randint(1, 100)
print(rand_num)
"""
题干(问题描述):
编写程序,在程序中随机为用户生成 6 位数短信验证码,包含大写字母。
"""
import random
password = []
for i in range(3):
num = random.randint(0, 9)
password.append(str(num))
char_num = random.randint(65, 90)
password.append(chr(char_num))
print("".join(password))
"""
题干(问题描述):
爬虫代码编写中,会随机获取用户代理值,即 User-Agent,编写函数,实现随机从列表中获取 User-Agent。
"""
import random
def get_headers():
uas = [
"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
"Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)",
"Baiduspider-image+(+http://www.baidu.com/search/spider.htm)",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36",
]
ua = random.choice(uas)
headers = {
"user-agent": ua
}
return headers
if __name__ == '__main__':
print(get_headers())
"""
题干(问题描述):
在 1000 内,随机获取 6 个整数,要求各个数字互不重复。
"""
from random import sample
ret = sample(range(1000), 6)
print(ret)
"""
题干(问题描述):
随机生成一个由 1 和 0 组成的列表(长度要求为 20),其中 0 的个数不能超过 5 个。
"""
import random
zero_max = 5
my_list = [1] * 20
zero_list = random.randint(0, zero_max)
zero_pos = random.sample(range(20), zero_list)
for pos in zero_pos:
my_list[pos] = 0
print(my_list)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册