首页 > 编程语言 >实验3 控制语句与组合数据类型应用编程

实验3 控制语句与组合数据类型应用编程

时间:2023-04-25 22:24:31浏览次数:34  
标签:语句 count product 编程 数据类型 cart products print id

task1

程序源码:

 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?  [0,100],能取到100。  

问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5?  [0,1,2,3,4],不包括5。

             利用 list(range(1,5)) 生成的有序序列范围是?是否包括5?  [1,2,3,4],不包括5。

问题3:使用line8代码生成的集合s1,len(s1)一定是5吗?如果不是,请解释原因。  len(s1)不一定是5,如果生成的随机数重复了,集合会去掉重复的随机数,len(s1)就会小于5。

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

 

task2_1

程序源码:

 1 #列表遍历
 2 lst = [55,92,88,79,96]
 3 
 4 #遍历方式1: 使用while + 索引
 5 i = 0
 6 while i < len(lst):
 7     print(lst[i],end = ' ')
 8     i += 1
 9 
10 print()
11 
12 #遍历方式2:使用for + 索引
13 for i in range(len(lst)):
14     print(lst[i],end = ' ')
15 print()
16 
17 #遍历方式3:使用for...in
18 for i in lst:
19     print(i,end = ' ')
20 print()

运行截图:

 

task2_2

程序源码:

 1 #字典遍历
 2 book_info = {'isbn': '978-7-5356-8297-0',
 3             '书名': '白鲸记',
 4             '作者': '克里斯多夫.夏布特',
 5             '译者': '高文婧',
 6             '出版社': '湖南美术出版社',
 7             '售价': 82
 8             }
 9 #遍历key-value对: 实现方式1
10 for key, value in book_info.items():
11     print(f'{key}: {value}')
12 print()
13 
14 #遍历key-value对: 实现方式2
15 for item in book_info.items():
16     print(f'{item[0]}: {item[1]}')
17 print()
18 
19 #遍历值:实现方式1
20 for value in book_info.values():
21     print(value, end = ' ')
22 print()
23 
24 #遍历值:实现方式2
25 for key in book_info.keys():
26     print(book_info[key], end = ' ')

运行截图:

 

task2_3

程序源码:

1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
2              {'书名': '局外人', '作者': '阿尔贝.加缪'},
3              {'书名': '设计中的设计', '作者': '原研哉'},
4              {'书名': '万历十五年', '作者': '黄仁宇'},
5              {'书名': '刀锋', '作者': '毛姆'}
6              ]
7 for i in range(len(book_infos)):
8     l=[v for v in book_infos[i].values()]
9     print(f'{i+1}.《{l[0]}》,{l[1]}')

程序截图:

 

task_3

程序源码:

 1 text = '''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 
23 l = [text.lower().count(chr(i)) for i in range(97,97+26)]
24 d = {}
25 for j in range(26):
26     d.update({chr(j+97):l[j]})
27 
28 l1 = list(d.items())
29 l2 = [(v,k) for k,v in l1]
30 for v,k in sorted(l2,reverse=True):
31     print(f'{k}:{v}')

运行截图:

 

task_4

程序源码:

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

运行截图:

 

task_5

程序源码:

 1 import random
 2 a = random.randint(1,31)
 3 print('猜猜2023年5月哪天是你的lucky day:

标签:语句,count,product,编程,数据类型,cart,products,print,id
From: https://www.cnblogs.com/p-s-y-0309/p/17332611.html

相关文章

  • 实验3 控制语句与组合数据类型应用编程
    task1.pyimportrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whi......
  • Java 编程问题:三、使用日期和时间
    本章包括20个涉及日期和时间的问题。这些问题通过Date、Calendar、LocalDate、LocalTime、LocalDateTime、ZoneDateTime、OffsetDateTime、OffsetTime、Instant等涵盖了广泛的主题(转换、格式化、加减、定义时段/持续时间、计算等)。到本章结束时,您将在确定日期和时间方面没有问题,......
  • 快速掌握并发编程---深入学习ThreadLocal
    生活中的ThreadLocal考试题只有一套,老师把考试题打印出多份,发给每位考生,然后考生各自写各自的试卷。考生之间不能相互交头接耳(会当做作弊)。各自写出来的答案不会影响他人的分数。注意:考试题、考生、试卷。用代码来实现:publicclassThreadLocalDemo{//线程共享变量localVar......
  • 面试官:Java 中有几种基本数据类型是什么?各自占用多少字节?
    认识基本数据类型在学习基本数据类型之前,我们先认识一下这两个单词:1、bit--位:位是计算机中存储数据的最小单位,指二进制数中的一个位数,其值为“0”或“1”。2、byte--字节:字节是计算机存储容量的基本单位,一个字节由8位二进制数组成。在计算机内部,一个字节可以表示一个数据,也可以表......
  • 2023.4.25编程一小时打卡
    一、问题描述:格式输出:输入一个整数,以八进制形式输入,分别以十进制和十六进制显示;输出字符串“Iamastudent!”,设置输出位宽为20,使用符号“*”填充;输出浮点数3.1415926,分别以浮点数和二进制形式进行输出,并分别设置小数点后的位数为8,6,4位。 二、解题思路:首先,根据题意定......
  • Python面向切面编程-语法层面和functools模块
    1,Python语法层面对面向切面编程的支持(方法名装饰后改变为log)__author__='Administrator'importtimedeflog(func):defwrapper(*args):start=time.time()func(args)end=time.time()print'funcusedtimeis:',end-st......
  • 编程实现可靠数据传输原理 Go-Back-N
    1.编写接收端代码接收端模拟网络环境较差时情况,每次生成一个随机数,小于0.8时不丢包,大于0.8时丢包。接收数据格式:编号+空格+内容返回数据格式:丢包:Loss+空格+编号未丢包:ACK+空格+编号接收包非累计计数时不做处理。2.编写发送端代码发送端较为复杂,分为两个线程:发送线程:设......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1实验源码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('......
  • 实验3 控制语句与组合数据类型应用编程
    一.实验目的:1.知道Python中组合数据类型字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的表示、特性2.能够正确、熟练使用字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的常用操作3.针对具体问题场景,能够灵活、组合使用多种数据类型,应用或设计算法,使用......
  • 实验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用集合存储随机整数......