首页 > 其他分享 >实验三

实验三

时间:2023-04-25 19:47:23浏览次数:30  
标签:count pro cart products print 实验 id

实验任务一:

实验源码:

 1 import random
 2 
 3 print('用列表存储随机整数: ')
 4 lst=[random.randint(0,100) for i in range(5)]
 5 print(lst)
 6 
 7 print('\n用集合存储随机整数: ')
 8 s1={random.randint(0,100) for i in range(5)}
 9 print(s1)
10 
11 print('\n用集合存储随机整数: ')
12 s2=set()
13 while len(s2) < 5:
14     s2.add(random.randint(0,100))
15 print(s2)

问题1: random.randint(0,100) 生成的随机整数范围是?能否取到100?

能。

问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5?

不包括。

问题3:使用line8代码生成的集合s1,len(s1)一定是5吗?如果不是,请解释原因。

 

不一定。原因:集合内不存在重复的元素,而随机生成的5个数可能包含重复元素。

问题4:使用line12-14生成的集合s2,len(s2)一定是5吗?如果不是,请解释原因。

一定。

实验任务二

实验源码:

 1 #task2_1.1
 2 lst=[55,92,88,79,96]
 3 i=0
 4 while i <len(lst):
 5     print(lst[i],end=' ')
 6     i+=1
 7 print()
 8 
 9 #task2_1.2
10 for  i in range(len(lst)):
11     print(lst[i],end=' ')
12 print()
13 
14 #task2_1.3
15 for i in lst:
16     print(i,end=' ')
17 print()
18 
19 #task2_2.1.1
20 book_info={'isbn':'978-7-5356-8297-0',
21              '书名':'白鲸记',
22              '作者':'克里斯多夫.夏布特',
23              '译者':'高文婧',
24              '出版社':'湖南美术出版社'}
25 for key,value in book_info.items():
26     print(f'{key}:{value}')
27 print()
28 
29 #task2_2.1.2
30 for item in book_info.items():
31     print(f'{item[0]}:{item[1]}')
32 print()
33 
34 #task2_2.2.1
35 for value in book_info.values():
36     print(value,end=' ')
37 print()
38 
39 #task2_2.2.2
40 for key in book_info.keys():
41     print(book_info[key],end=' ')
42 print()
43 
44 #task2_3
45 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
46                  {'书名': '局外人', '作者': '阿尔贝.加缪'},
47                  {'书名': '设计中的设计', '作者': '原研哉'},
48                  {'书名': '万历十五年', '作者': '黄仁宇'},
49                  {'书名': '刀锋', '作者': '毛姆'}]
50 j=0
51 for i in book_infos:
52     x,y=i['书名'],i['作者']
53     print(f'{j+1}. 《{x}》,{y}')
54     j+=1

实验结果截图:

试验任务三

实验源码:

 1 x='''The Zen of Python, by Tim Peters
 2 
 3 Beautiful is better than ugly.
 4 Explicit is better than implicit.
 5 Simple is better than complex.
 6 Complex is better than complicated.
 7 Flat is better than nested.
 8 Sparse is better than dense.
 9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!'''
22 y=x.lower()
23 z=[]
24 d={}
25 fo i in y:
26     if 'a'<= i <='z':
27         z.append(i)
28 for i in z:
29     d[i]=d.get(i,0)+1
30 for i in range(26):
31     if chr(97+i) not in d:
32         d[chr(97+i)]=0
33     else:
34         continue
35 a=[(value,key) for key,value in d.items()]
36 a=sorted(a,reverse=True)
37 for i in a:
38     print(f'{i[1]}:{i[0]}')

实验结果截图:

 实验任务四

实验源码:

 1 x='专业代号信息'
 2 print('{:-^20s}'.format(x))
 3 d={'8326':'地信类','8329':'计算机类','8330':'气科类','8336':'防灾工程','8345':'海洋科学','8382':'气象工程'}
 4 for i in d:
 5     print(f'{i}:{d[i]}')
 6 x='学生专业查询'
 7 print('{:-^20s}'.format(x))
 8 n=input('请输入学号:')
 9 if n!='#':
10     while n!='#':
11         x=n[4:8]
12         if x not in d :
13             print('不在这些专业中...')
14             n=input('请输入学号:')
15         else:
16             print(f'专业是:{d[x]}')
17             n=input('请输入学号:')
18     print('查询结束')
19 else:
20     print('查询结束')

实验结果截图:

实验任务五

 1 print('猜猜2023年5月哪一天会是你的lucky day

标签:count,pro,cart,products,print,实验,id
From: https://www.cnblogs.com/wjh857925263/p/17332720.html

相关文章

  • 实验三
    task1源代码importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(......
  • 实验3 控制语句与组合数据类型应用编程
    一.实验目的:1.知道Python中组合数据类型字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的表示、特性2.能够正确、熟练使用字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的常用操作3.针对具体问题场景,能够灵活、组合使用多种数据类型,应用或设计算法,使用......
  • 实验3
    实验任务1源代码:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whil......
  • linux操作系统分析实验五-深入理解进程切换
    Lab5:深入理解进程切换首先找到对应进程调度的代码文件Kernal/sched/core.c  找到context_switch()函数   其中包括rq,为进程的runningqueue;以及进程切换前后的进程描述符prev和next  首先调用一些函数做上下文切换的准备,与最后出现的finish_task_switch()成......
  • 实验3 控制语句和组合数据类型应用编程
    task1.py1importrandom2print('用列表存储随机整数:')3lst=[random.randint(0,100)foriinrange(5)]4print(lst)5print('\n用集合存储随机整数:')6s1={random.randint(0,100)foriinrange(5)}7print(s1)8print('\n用集合存储随机整数......
  • 实验三
    task1importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()while......
  • 实验3控制语句与组合数据类型应用编程
    实验任务一实验源码importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()......
  • 实验3
    task1:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()while......
  • 实验3 控制语句与组合数据类型
    实验任务一task1.py实验源码:1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011prin......
  • 实验3
    task1实验源码:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set......