Python学习实验报告(第二周)

一、实验目的

1. 掌握:Python常用内置对象-列表、元组、字典、集合。

2. 掌握运算符和内置函数对列表、元组、字典、集合的操作。

3. 理解列表推导式、生成器表达式的工作原理。

4. 掌握切片操作。

5. 掌握序列解包的用法。

二、实验内容

1. 编写程序,用一个变量存储以下信息:学号,姓名,所在学院,专业名称。

class student:
    def __init__(self, num, name, academy, major):
        self.num = num
        self.name = name
        self.academy = academy
        self.major = major
s1 = student("1", "张三", "软件学院", "软件工程")
print(s1.num+":"+s1.name+" "+s1.academy+" "+s1.major)

图片描述

2. 编写程序,用一个变量存储你所在小组的组员名单。

class student:
    def __init__(self, num, name, academy, major):
        self.num = num
        self.name = name
        self.academy = academy
        self.major = major
class group:
    def __init__(self):
        self.member = []
    def add_member(self, *args):
        for s in args:
            st = student(s.num, s.name, s.academy, s.major)
            self.member.append(st)

s1 = student("1", "张三", "软件学院", "软件工程")
s2 = student("2", "李四", "软件学院", "软件工程")
s3 = student("3", "王五", "软件学院", "软件工程")

group_list = group()
group_list.add_member(s1, s2, s3)
for m in group_list.member:
    print(m.num+":"+m.name+" "+m.academy+" "+m.major)

图片描述

3. 对组员名单进行操作,如增加一个同学,减少一个同学,修改一个同学的姓名。

class student:
    def __init__(self, num, name, academy, major):
        self.num = num
        self.name = name
        self.academy = academy
        self.major = major
class group:
    def __init__(self):
        self.member = []
    def add_member(self, *args):
        for s in args:
            st = student(s.num, s.name, s.academy, s.major)
            self.member.append(st)
    def alter_name(self, position, name):
        self.member[position-1].name = name

s1 = student("1", "张三", "软件学院", "软件工程")
s2 = student("2", "李四", "软件学院", "软件工程")
s3 = student("3", "王五", "软件学院", "软件工程")

group_list = group()
group_list.add_member(s1, s2, s3)

s4 = student("4", "周六", "软件学院", "软件工程")
group_list.add_member(s4)

group_list.alter_name(position=1, name="张三改")
for m in group_list.member:
    print(m.num+":"+m.name+" "+m.academy+" "+m.major)

Python学习实验报告(第二周)_第1张图片

4. 编写程序,录入小组成员的姓氏,利用集合输出小组中的所有姓氏。

name_list = input("请输入小组成员的姓氏(空格分隔):")
name_set = set(name_list.split(" "))
print("小组成员的姓氏有:")
for name in name_set:
    print(name, end=" ")

图片描述

5. 编写程序,用一个变量存储组员名单,组员Python成绩,计算组员总人数,总分,平均分。

class grades:
    class student:
        def __init__(self, name, grade):
            self.name = name
            self.grade = grade

    def __init__(self):
        self.member = []

    def add_member(self, *args, **kwargs):
        for s in args:
            st = self.student(s.name, s.grade)
            self.member.append(st)
        for key in kwargs:
            print(key, kwargs[key])
            st = self.student(key, kwargs[key])
            self.member.append(st)

    def count_number(self):
        return len(self.member)

    def sum_grade(self):
        sum = 0
        for m in self.member:
            sum += m.grade
        return sum

    def average_grade(self):
        return round(self.sum_grade()/self.count_number(), 1)

group_grades = grades()
group_grades_dic = {"张三": 79, "李四": 98, "王五": 67, "周六": 89}
group_grades.add_member(**group_grades_dic)

print("组员总人数:", group_grades.count_number())
print("组员总分:", group_grades.sum_grade())
print("组员平均分:", group_grades.average_grade())

Python学习实验报告(第二周)_第2张图片

6. 编写程序,用一个变量存储组员成绩,要求有低于60的成绩,利用列表推导式,筛选出低于60分的成绩。

class grades:
    class student:
        def __init__(self, name, grade):
            self.name = name
            self.grade = grade

    def __init__(self):
        self.member = []

    def add_member(self, *args, **kwargs):
        for s in args:
            st = self.student(s.name, s.grade)
            self.member.append(st)
        for key in kwargs:
            st = self.student(key, kwargs[key])
            self.member.append(st)

    def count_number(self):
        return len(self.member)

    def sum_grade(self):
        sum = 0
        for m in self.member:
            sum += m.grade
        return sum

    def average_grade(self):
        return round(self.sum_grade()/self.count_number(), 1)

    def fail(self):
        return {m.name: m.grade for m in self.member if m.grade < 60}

group_grades = grades()
group_grades_dic = {"张三": 56, "李四": 98, "王五": 67, "周六": 89}
group_grades.add_member(**group_grades_dic)

print("低于60分的人:")
failed = group_grades.fail()
for name in failed:
    print(name, end=" ")
    print(failed[name])

图片描述

7. 产生两个随机数x,y,要求0
import random

class grades:
    class student:
        def __init__(self, name, grade):
            self.name = name
            self.grade = grade

    def __init__(self):
        self.member = []

    def add_member(self, *args, **kwargs):
        for s in args:
            st = self.student(s.name, s.grade)
            self.member.append(st)
        for key in kwargs:
            st = self.student(key, kwargs[key])
            self.member.append(st)

    def count_number(self):
        return len(self.member)

    def sum_grade(self):
        sum = 0
        for m in self.member:
            sum += m.grade
        return sum

    def average_grade(self):
        return round(self.sum_grade()/self.count_number(), 1)

    def fail(self):
        return {m.name: m.grade for m in self.member if m.grade < 60}

    def slice(self, x, y):
        m_slice = self.member[x:y]
        grades_slice = [m.grade for m in m_slice]
        return grades_slice

group_grades = grades()
group_grades_dic = {"张三": 56, "李四": 98, "王五": 67, "周六": 89}
group_grades.add_member(**group_grades_dic)

length = group_grades.count_number()
x = random.randint(1, length-2)
y = random.randint(x+1, length-1)
group_slice = group_grades.slice(x, y)

print("组员成绩[{}:{}]切片的成绩:".format(x, y))
for g in group_slice:
    print(g, end=" ")

图片描述

8. 利用生成器表达式生成0-9的数值,要求每一个数值乘以你的幸运数字,最后将生成器对象转换成列表,输出列表的结果。

iterator = (i*7 for i in range(10))
to_list = []
for num in iterator:
    to_list.append(num)
print(to_list)

图片描述

9. 复制一段文章或者论文,利用字典统计出每个字出现的次数。

import re

def remove_punctuation(text):
    punctuation = '!,.;:?"\'、,;'
    text = re.sub(r'[{}]+'.format(punctuation), '', text)
    return text.strip()

text = "Please send this message to those people who mean something " \
       "to you,to those who have touched your life in one way or " \
       "another, to those who make you smile when you really need" \
       " it, to those that make you see the brighter side of things " \
       "when you are really down,to those who you want to let them " \
       "know that you appreciate their friendship.And if you don’t, " \
       "don’t worry,nothing bad will happen to you,you will just " \
       "miss out on the opportunity to brighten someone’s day with this message."
text_r = remove_punctuation(text)
text_r = text_r.lower()
words = text_r.split(" ")

words_set = set(words)
words_dic = {word: 0 for word in words_set}
for word in words:
    words_dic[word] += 1
print(words_dic)

Python学习实验报告(第二周)_第3张图片

10. 棋盘一共64个小格子,第一个格子放一粒米,第二个2粒米,第三个4粒米,第四个8粒米,以此类推一直把64个格子放满需要多少粒米?要求使用列表推导式和内置函数进行计算。

print("一共需要{}粒米".format(sum([2**i for i in range(64)])))

图片描述

三、实验心得

经过这次作业,我深刻地发现对于python的学习,还是有很多的不足,很多地方都没有理解透彻,所以在以后的学习中,应该更仔细地理解语句的用法,加深对其的记忆。

你可能感兴趣的:(Python学习实验报告,python)