首页 > 其他分享 >实验3

实验3

时间:2023-04-25 19:58:42浏览次数:28  
标签:count pro cart products print 实验 id

1.实验任务1

task1.py

实验代码:

import random
print('用列表储存随机整数:')
lst = [random.randint(0, 100) for i in range(5)]
print(lst)

print('\n用集合储存随机整数:')
s1 = {random.randint(0, 100) for i in range(5)}
print(s1)

print('\n用集合储存随机整数:')
s2 = set()
while len(s2) < 5:
    s2.add(random.randint(0, 100))
print(s2)

 

实验结果截图:

 

 实验结论:

1.随机整数的范围是[0,100),不能取到100

2.有序序列的范围是[0,5),不包括5

   有序序列的范围是[1,5), 不包括5

3.一定是5

4.不一定是5,len(s2) < 5 有好几个数, 不一定是5

 

2.实验任务2

task2_1.py

实验代码:

#列表遍历
lst = [55, 92, 88, 79, 96]

#遍历方式1:使用while + 索引
i = 0
while i < len(lst):
    print(lst[i], end = ' ')
    i += 1
print()

#遍历方式2:使用for + 索引
for i in range(len(lst)):
    print(lst[i], end = ' ')
print()

#遍历方式3:使用for...in
for i in lst:
    print(i , end = ' ')
print()

 

实验结果截图:

 

task2_2.py

实验代码:

#字典遍历
book_info = {'isbn': '978-7-5356-8297-0',
             '书名': '白鲸记',
             '作者': '克里斯多夫.夏布特',
             '译者': '高文婧',
             '出版社': '湖南美术出版社',
             '售价': 82
            }
#遍历key-value对:实现方式1
for key, value in book_info.items():
    print(f'{key}: {value}')
print()

#遍历key-value对:实现方式2
for item in book_info.items():
    print(f'{item[0]}: {item[1]}')
print()

#遍历值: 实现方式1
for value in book_info.values():
    print(value, end = ' ')
print()

#遍历值: 实现方式2
for key in book_info.keys():
    print(book_info[key], end = ' ')

 

实验结果截图:

 

task2_3.py

实验代码:

 

book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
             {'书名': '局外人', '作者': '阿尔贝.加缪'},
             {'书名': '设计中的设计', '作者': '原研哉'},
             {'书名': '万历十五年', '作者': '黄仁宇'},
             {'书名': '刀锋', '作者': '毛姆'}
             ]


i = 0
while i < len(book_infos):
    j = book_infos[i]
    b = str(j['书名'])
    j['书名'] = b.join('《》')
    print(str(i+1)+'.'+j['书名']+','+j['作者'])
    i += 1

 

实验结果截图:

 

 

 

3.实验任务3

task3.py

实验代码:

 

text1='''
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
text=text1.lower()

lst=[text.count(chr(i)) for i in range(97,97+26)]
d={}
for i in range(26):
    d.update({chr(i+97):lst[i]})

lst1=list(d.items())
lst2=[(i,j) for j,i in lst1]
for i,j in sorted(lst2,reverse=True):
    print(f'{j}:{i}')

 

实验结果截图:

 

 

 

4.实验任务4

 

实验代码:

code_majors={8326:'地信类',
             8329:'计算机类',
             8330:'气科类',
             8336:'防灾工程',
             8345:'海洋科学',
             8382:'气象工程'}

print('专业代号信息'.center(50, '-'))
for key,value in code_majors.items():
    print(f'{key}:{value}')

print('学生专业查询'.center(50, '-'))
code=list(code_majors.keys())
number = input('请输入学号:')
while number !='#':
    number_code = int(number[4:8])
    if code_majors.get(number_code)==None:
        print('不在这些专业中...')
    else:
        major=code_majors.get(number_code)
        print('专业是:'+major)
    number = input('请输入学号:')

print('查询结束...')

 

实验结果截图:

 

5.实验任务5

 

实验代码:

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

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

相关文章

  • 实验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(......
  • 实验三
    实验任务一:实验源码:1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\......
  • 实验三
    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......