常用标准库 random,python 入门教程之每日 5 or 6 道题 | Python技能树征题
Posted 梦想橡皮擦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用标准库 random,python 入门教程之每日 5 or 6 道题 | Python技能树征题相关的知识,希望对你有一定的参考价值。
本篇博客主要为 https://bbs.csdn.net/skill/python 频道练习题模块补充题目,暂定每天提供 5 or 6 道测试题,后面可能会更多哦~。
本篇博客对【进阶语法】→**【常用标准库】**进行出题。
以下题目,默认将正确答案,放置在选项 A 位置
知识点:python random 库
第 1 题:
题目难度:1 星
题干(问题描述):
下述哪个选项能在 1~100 中的随机选择 10 个数字
选项 A:
import random
for i in range(1, 6):
rand_num = random.randint(1, 100)
print(rand_num)
选项 B:
import random
for i in range(1, 6):
rand_num = random.random(1, 100)
print(rand_num)
选项 C:
import random
for i in range(1, 6):
rand_num = random.sample(1, 100)
print(rand_num)
选项 D:
import random
for i in range(1, 6):
rand_num = random.randint(1, 101)
print(rand_num)
正确答案:A
第 2 题:
题目难度:1 星
题干(问题描述):
编写程序,在程序中随机为用户生成 6 位数短信验证码,包含大写字母。
选项 A:
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))
选项 B:
import random
password = []
for i in range(6):
num = random.randint(0, 9)
password.append(str(num))
char_num = random.randint(65, 90)
password.append(chr(char_num))
print("".join(password))
选项 C:
import random
password = []
for i in range(3):
num = random.randint(0, 9)
password.append(str(num))
char_num = random.randint(97,122)
password.append(chr(char_num))
print("".join(password))
选项 D:
import random
password = []
for i in range(3):
num = random.randint(0, 9)
password.append(str(num))
char_num = random.randint(97,122)
password.append(chr(num ))
print("".join(password))
正确答案:A
第 3 题:
题目难度:2 星
题干(问题描述):
爬虫代码编写中,会随机获取用户代理值,即 User-Agent
,编写函数,实现随机从列表中获取 User-Agent
。
选项 A:
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())
选项 B:
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",
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
"Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
"Sosospider+(+http://help.soso.com/webspider.htm)",
"Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
]
ua = random.random(uas)
headers = {
"user-agent": ua
}
return headers
if __name__ == '__main__':
print(get_headers())
选项 C:
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",
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
"Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
"Sosospider+(+http://help.soso.com/webspider.htm)",
"Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
]
ua = random.sample(uas)
headers = {
"user-agent": ua
}
return headers
if __name__ == '__main__':
print(get_headers())
选项 D:
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",
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"Mozilla/5.0 (compatible; Googlebot-Image/1.0; +http://www.google.com/bot.html)",
"Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Sogou News Spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);",
"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
"Sosospider+(+http://help.soso.com/webspider.htm)",
"Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html)"
]
ua = random.shuffle(uas)
headers = {
"user-agent": ua
}
return headers
if __name__ == '__main__':
print(get_headers())
正确答案:A
第 4 题:
题目难度:2 星
题干(问题描述):
在 1000 内,随机获取 6 个整数,要求各个数字互不重复。
选项 A:
from random import sample
ret = sample(range(1000), 6)
print(ret)
选项 B:
from random import sample
ret = random.sample(range(1000), 6)
print(ret)
选项 C:
import random
ret = random.shuffle(range(1000), 6)
print(ret)
选项 D:
import random
for i in range(1, 6):
rand_num = random.randint(1, 1000)
print(rand_num)
正确答案:A
第 5 题:
题目难度:3 星
题干(问题描述):
随机生成一个由 1
和 0
组成的列表(长度要求为 20),其中 0
的个数不能超过 5 个。
选项 A:
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)
选项 B:
import random
zero_max = 5
my_list = [1] * 20
zero_list = random.randint(0, zero_max)
zero_pos = random.sample(range(10), zero_list)
for pos in zero_pos:
my_list[pos] = 0
print(my_list)
选项 C:
import random
zero_max = 5
my_list = [0] * 20
zero_list = random.randint(0, zero_max)
zero_pos = random.sample(range(10), zero_list)
for pos in zero_pos:
my_list[pos] = 0
print(my_list)
选项 D:
import random
zero_max = 5
my_list = [0] * 20
zero_list = random.randint(0, zero_max)
zero_pos = random.sample(range(10), zero_list)
for pos in zero_list:
my_list[pos] = 0
print(my_list)
正确答案:A
试题仓库地址如下:
以上是关于常用标准库 random,python 入门教程之每日 5 or 6 道题 | Python技能树征题的主要内容,如果未能解决你的问题,请参考以下文章
Python学习笔记17:标准库之数学相关(math包,random包)