zzy
7-python作业--极简主义

7-python作业--极简主义


        

OK ! 整活继续! 这次基本每个要求同样尽量使用较少的行数来完成,体现了作业的某种特点()

Release

  • Setup
  • 安装包下载的文件默认在桌面上.

        

作业

例 4-2

用户分别从键盘输入4个整数和3个整数组成列表list1和list2;

将列表list2合并到list1中;

在list1末尾再添加两个数字90和100;

对list1降序排列,最后输出最终的列表list1。

  1. 正常作业:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # example4_2.py
    # coding=utf-8

    print('list1:')
    list1=[]
    for i in range(4):
    list1.append(int(input(f'请输入第{i+1}个整数: ')))
    print('list2:')
    list2=[]
    for i in range(3):
    list2.append(int(input(f'请输入第{i+1}个整数: ')))
    print(list1)
    print(list2)
    list1.extend(list2)
    print(f'列表list2合并到list1中后的数据: {list1}')
    list1.extend([90,100])
    print(f'加上90,100后的1ist1的数据: {list1}')
    list1.sort(reverse=True)
    print(f'降序排列后最终列表list1中的数据: {list1}')
  2. 我的:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 4-2
    print('list1:')
    list1 = [int(input(f'请输入第{i+1}个整数: ')) for i in range(4)]
    print('list2:')
    list2 = [int(input(f'请输入第{i+1}个整数: ')) for i in range(3)]
    print(f'list1: {list1}',f'list2: {list2}',sep='\n')
    (lambda list1: print(f'列表list2合并到list1中后的数据: {list1}'))(list1+list2)
    (lambda list1: print(f'加上90,100后的1ist1的数据: {list1}'))(list1+[90,100])
    (lambda list1: (list1.sort(reverse=True), (lambda: print(f'降序排列后最终列表list1中的数据: {list1}'))()))(list1)

作业

练 7-1

从键盘依次输入多个正整数(每次输入一个)构成一个列表,输入-1表示结束,并且-1不添加到列表中。输出此列表,分别计算并输出列表中奇数和偶数的和。

  1. 正常作业

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # exercise7_1.py
    # coding=utf-8

    nums = []
    i = int(input('请输入数字,以-1结束: '))
    while i != -1:
    nums.append(i)
    i = int(input('请输入数字: '))
    print(nums)
  2. 我的

    1
    (lambda: (print('请输入数字,以-1结束:'), print([int(x) for x in iter(input, '-1')])))()

    (lambda: (print('请输入数字,以-1结束:'), print([int(x) for x in iter(input, '-1')])))()

练 7-2

已知10个学生的成绩分别为: 68, 75, 32, 99, 78, 45, 88, 72, 83, 78。请将成绩存放在列表中,并对其进行统计,输出优(100~90)、良(89~80)、中(79~60)、差(59~0)四个等级的人数。

  1. 正常作业

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # exercise7_2.py
    # coding=utf-8

    scores = [68, 75, 32, 99, 78, 45, 88, 72, 83, 78]
    grade = [0, 0, 0, 0]

    for score in scores:
    if 90 <= score <= 100:
    grade[0] += 1
    elif 80 <= score < 90:
    grade[1] += 1
    elif 60 <= score < 80:
    grade[2] += 1
    else:
    grade[3] += 1

    for grade, count in zip(['优', '良', '中', '差'] , grade):
    print(f"{grade}等级的人数:{count}")
  2. 我的

    1
    2
    3
    4
        scores = [68, 75, 32, 99, 78, 45, 88, 72, 83, 78]  
    grade_ranges = {'优': (90, 100), '良': (80, 89), '中': (60, 79), '差': (0, 59)}
    grade_counts = {grade: sum(1 for score in scores if low <= score <= high) for grade, (low, high) in grade_ranges.items()}
    print({grade: count for grade, count in grade_counts.items() if count > 0})

练 7-3

将列表list2中的偶数元素追加到列表list1的尾部

  1. 正常作业

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # exercise7_3.py
    # coding=utf-8

    list1 = [20, 99, 88, 79, 59, 43, 23, 90, 44, 89]
    list2 = [25, 91, 82, 72, 51, 46, 28, 98, 42, 87]

    for i in list2:
    if i % 2 == 0:
    list1.append(i)
    print('新的list1::', list1)
  2. 我的

    1
    (lambda list1, list2: print('新的list1::', list1+[i for i in list2 if i % 2 ==0]))([20, 99, 88, 79, 59, 43, 23, 90, 44, 89],[25, 91, 82, 72, 51, 46, 28, 98, 42, 87])

    (lambda list1, list2: print('新的list1::', list1+[i for i in list2 if i % 2 ==0]))([20, 99, 88, 79, 59, 43, 23, 90, 44, 89],[25, 91, 82, 72, 51, 46, 28, 98, 42, 87])

练 7-4

列表scores存放某小组的成绩,成绩采用百分制(0–100)均为整数,对成绩判定等级,如果成绩不在百分制范围内则输出“成绩错误”

  1. 正常作业

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # exercise7_4.py
    # coding=utf-8

    scores = [100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89]
    for score in scores:
    if score > 100 or score < 0:
    grade = '成绩错误'
    else:
    if score >= 85:
    grade = "优良"
    elif score >= 70:
    grade = "中等"
    elif score >= 60:
    grade = "及格"
    else:
    grade = "不及格"
    print('成绩:', score, '\t', grade)
  2. 我的

    1
    (lambda scores: print('\n'.join(['成绩:{} \t {}'.format(score, '成绩错误' if score > 100 or score < 0 else ('优良' if score >= 85 else ('中等' if score >= 70 else ('及格' if score >= 60 else '不及格')))) for score in scores])))([100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89])

    (lambda scores: print('\n'.join(['成绩:{} \t {}'.format(score, '成绩错误' if score > 100 or score < 0 else ('优良' if score >= 85 else ('中等' if score >= 70 else ('及格' if score >= 60 else '不及格')))) for score in scores])))([100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89])

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    (lambda scores:  

    print('\n'.join(['成绩:{} \t {}'.format(score,

    '成绩错误' if score > 100 or score < 0 else

    ​ ('优良' if score >= 85 else

    ​ ('中等' if score >= 70 else

    ​ ('及格' if score >= 60 else '不及格'))))

    for score in scores])))(

    [100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89]

    )

提交作业

  1. 正常作业:(打包文件在文章开头处setup)
    Download example4_2.py
    Download exercise7_1.py
    Download exercise7_1.py
    Download exercise7_1.py
    Download exercise7_1.py

  2. 我的:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    #-- codeing=utf-8 --
    #@Time:2024-4-9
    #@Author:zzy
    #@File:exercise7.py
    #@Software:vscode

    # 4-2
    print('list1:')
    list1 = [int(input(f'请输入第{i+1}个整数: ')) for i in range(4)]
    print('list2:')
    list2 = [int(input(f'请输入第{i+1}个整数: ')) for i in range(3)]
    print(f'list1: {list1}',f'list2: {list2}',sep='\n')
    (lambda list1: print(f'列表list2合并到list1中后的数据: {list1}'))(list1+list2)
    (lambda list1: print(f'加上90,100后的1ist1的数据: {list1}'))(list1+[90,100])
    (lambda list1: (list1.sort(reverse=True), (lambda: print(f'降序排列后最终列表list1中的数据: {list1}'))()))(list1)

    # 7-1
    (lambda: (print('请输入数字,以-1结束:'), print([int(x) for x in iter(input, '-1')])))()

    # 7-2
    scores = [68, 75, 32, 99, 78, 45, 88, 72, 83, 78]
    grade_ranges = {'优': (90, 100), '良': (80, 89), '中': (60, 79), '差': (0, 59)}
    grade_counts = {grade: sum(1 for score in scores if low <= score <= high) for grade, (low, high) in grade_ranges.items()}
    print({grade: count for grade, count in grade_counts.items() if count > 0})

    # 7-3
    (lambda list1, list2: print('新的list1::', list1+[i for i in list2 if i % 2 ==0]))([20, 99, 88, 79, 59, 43, 23, 90, 44, 89],[25, 91, 82, 72, 51, 46, 28, 98, 42, 87])

    # 7-4
    (lambda scores: print('\n'.join(['成绩:{} \t {}'.format(score, '成绩错误' if score > 100 or score < 0 else ('优良' if score >= 85 else ('中等' if score >= 70 else ('及格' if score >= 60 else '不及格')))) for score in scores])))([100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89])
    '''
    (lambda scores:
    print('\n'.join(['成绩:{} \t {}'.format(score,
    '成绩错误' if score > 100 or score < 0 else
    ('优良' if score >= 85 else
    ('中等' if score >= 70 else
    ('及格' if score >= 60 else '不及格'))))
    for score in scores])))(
    [100, 90, 45, 200, 67, 82, 91, 39, 78, 34, -49, 20, 99, 88, 79, 59, 43, 23, 90, 44, 89]
    )
    '''
本文作者:zzy
本文链接:http://周梓煜.com/2024/04/09/school-hhw-7-python/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可