This blog is mainly about https://bbs.csdn.net/skill/python Supplementary questions for the channel exercise module , It is tentatively provided every day 5 or 6 Road test questions , There may be more in the back ~.
This blog is right 【 Advanced Grammar 】→**【 Common standard library 】** Make a question .
The following questions , The default will be the correct answer , Place in options A Location
questions :1 star
stem ( Problem description ):
Which of the following options can be used in 1~100 Random selection in 10 A digital
Options A:
import random
for i in range(1, 6):
rand_num = random.randint(1, 100)
print(rand_num)
Options B:
import random
for i in range(1, 6):
rand_num = random.random(1, 100)
print(rand_num)
Options C:
import random
for i in range(1, 6):
rand_num = random.sample(1, 100)
print(rand_num)
Options D:
import random
for i in range(1, 6):
rand_num = random.randint(1, 101)
print(rand_num)
right key :A
questions :1 star
stem ( Problem description ):
Programming , Randomly generate... For users in the program 6 Digit SMS verification code , Include capital letters .
Options 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))
Options 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))
Options 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))
Options 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))
right key :A
questions :2 star
stem ( Problem description ):
Crawler code writing , Will randomly get the user agent value , namely User-Agent
, Write function , Achieve random access from the list User-Agent
.
Options 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())
Options 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())
Options 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())
Options 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())
right key :A
questions :2 star
stem ( Problem description ):
stay 1000 Inside , Random access 6 It's an integer , It is required that the numbers do not repeat each other .
Options A:
from random import sample
ret = sample(range(1000), 6)
print(ret)
Options B:
from random import sample
ret = random.sample(range(1000), 6)
print(ret)
Options C:
import random
ret = random.shuffle(range(1000), 6)
print(ret)
Options D:
import random
for i in range(1, 6):
rand_num = random.randint(1, 1000)
print(rand_num)
right key :A
questions :3 star
stem ( Problem description ):
Randomly generated by 1
and 0
A list of components ( The length requirement is 20), among 0
You can't have more than 5 individual .
Options 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)
Options 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)
Options 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)
Options 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)
right key :A