python第八次作业

文章展示了如何在Python中定义一个Book类,包括类属性、对象属性、对象相加操作、打印输出、__call__方法、静态方法和类方法的实现。此外,还模拟了range功能的类以及创建了一个生成器函数,用于组合两个列表的元素并生成元组。
摘要由CSDN通过智能技术生成

目录

1.定义一个Book类:(20分)        定义类属性: count        定义对象属性或变量: title(书名), author(作者), publish(出版社), price(价格)        定义对象相加操作: book1 + book2 = book1.title + book2.title              举例: book1 + book2 = Java程序设计Python程序设计        定义打印对象的输出: 使用print打印 book1 => 书的名字-作者-出版社-价格                   举例: print(book1) => Python程序设计-吉米勒-机械出版社-35         定义调用对象的方法:__call__(): 并让其返回书的名字  定义静态方法: static_print_obj: 执行输出print("This is Static Method of class Book")  定义类方法: class_print_obj: 执行输出print("This is class Method of class Book ")   按要求执行底下的操作:       实例化对象book1: Python程序设计, 王铮, 机械教育出版社, 22    实例化对象book2:  Java程序设计, 李刚, 清华大学出版社, 34        执行book1 + book2: 并输出相加的结果        执行print(book1)        执行 book1()        调用类变量,进行赋值100, 输出类变量        调用book1对象,并修改书名: Python程序设计修改为流畅的Python        调用静态方法 static_print_obj        调用类方法 class_print_obj

2.模拟range的功能

3.定义一个生成器函数:(10分)            传入两个列表:    列表1: ["red", "black", "green"]    列表2:  ["S", "M", "L"]   1.调用next返回结果: 调用9次next()       输出结果:       (red, S)       (red, M)       (red, L)       ........       (green, L)   2.使用循环去访问生成器:    输出结果:       (red, S)       (red, M)       (red, L)       ........       (green, L)    注意循环终止的条件


1.定义一个Book类:(20分)
        定义类属性: count
        定义对象属性或变量: title(书名), author(作者), publish(出版社), price(价格)
        定义对象相加操作: book1 + book2 = book1.title + book2.title
              举例: book1 + book2 = Java程序设计Python程序设计
        定义打印对象的输出: 使用print打印 book1 => 书的名字-作者-出版社-价格
                   举例: print(book1) => Python程序设计-吉米勒-机械出版社-35 
        定义调用对象的方法:__call__(): 并让其返回书的名字
  定义静态方法: static_print_obj: 执行输出print("This is Static Method of class Book")
  定义类方法: class_print_obj: 执行输出print("This is class Method of class Book ")
  
 按要求执行底下的操作:   
    实例化对象book1: Python程序设计, 王铮, 机械教育出版社, 22
    实例化对象book2:  Java程序设计, 李刚, 清华大学出版社, 34
        执行book1 + book2: 并输出相加的结果
        执行print(book1)
        执行 book1()
        调用类变量,进行赋值100, 输出类变量
        调用book1对象,并修改书名: Python程序设计修改为流畅的Python
        调用静态方法 static_print_obj
        调用类方法 class_print_obj

class Book:
    count = 10000

    def __init__(self, title, author, publish, price):
        self.title = title
        self.author = author
        self.publish = publish
        self.price = price

    def __add__(self, other):
        return self.title + other.title

    def __str__(self):
        return self.title + "-" + self.author + "-" + self.publish + "-" + str(self.price)

    def __call__(self, *args, **kwargs):
        return self.title

    @staticmethod                        # 静态方法装饰器
    def static_print_obj():
        print("This is Static Method of class Book")

    @classmethod                         # 类方法装饰器
    def class_print_obj(cls):
        print("This is class Method of class Book")


book1 = Book("Python程序设计", "王铮", "机械教育出版社", "22")
book2 = Book("Java程序设计", "李刚", "清华大学出版社", "34")


print(book1 + book2)
print(book1)
print(book1())
Book.count = 100
print(Book.count)
print(book1.count)
book1.title = "流畅的python"
print(book1.title)
book1.static_print_obj()
book2.class_print_obj()
Book.static_print_obj()
Book.class_print_obj()






Python程序设计Java程序设计
Python程序设计-王铮-机械教育出版社-22
Python程序设计
100
100
流畅的python
This is Static Method of class Book
This is class Method of class Book
This is Static Method of class Book
This is class Method of class Book

2.模拟range的功能

class MyIterator:
    def __init__(self, start=None, stop=None, step=1):
        if start is not None and stop is None:
            self.stop = start
            self.start = 0
        if start is not None and stop is not None:
            self.start = start
            self.stop = stop
        self.step = step

    def __iter__(self):
        return self

    def __next__(self):

        if self.step > 0:
            if self.start < self.stop:
                data = self.start
                self.start += self.step
                return data
            else:
                raise StopIteration
        else:
            if self.start > self.stop:
                data = self.start
                self.start += self.step
                return data
            else:
                raise StopIteration


print([i for i in MyIterator(3)])
print([i for i in MyIterator(1, 4)])
print([i for i in MyIterator(1, 4, 1)])
print([i for i in MyIterator(5, -2, -1)])
print([i for i in MyIterator(5, 2, -1)])
print([i for i in MyIterator(-10, -1, 1)])
print([i for i in MyIterator(-1, -10, -1)])





[0, 1, 2]
[1, 2, 3]
[1, 2, 3]
[5, 4, 3, 2, 1, 0, -1]
[5, 4, 3]
[-10, -9, -8, -7, -6, -5, -4, -3, -2]
[-1, -2, -3, -4, -5, -6, -7, -8, -9]

3.定义一个生成器函数:(10分)
            传入两个列表:
    列表1: ["red", "black", "green"]
    列表2:  ["S", "M", "L"]
   1.调用next返回结果: 调用9次next()
       输出结果:
       (red, S)
       (red, M)
       (red, L)
       ........
       (green, L)
   2.使用循环去访问生成器:
    输出结果:
       (red, S)
       (red, M)
       (red, L)
       ........
       (green, L)
    注意循环终止的条件

def gen_num(list_data1, list_data2):
    for i in list_data1:
        for j in list_data2:
            yield i, j


list_data1 = ["red", "black", "green"]
list_data2 = ["S", "M", "L"]

gen = gen_num(list_data1, list_data2)

1.
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))

2.
for i in gen_num(list_data1, list_data2):
    print(i)



('red', 'S')
('red', 'M')
('red', 'L')
('black', 'S')
('black', 'M')
('black', 'L')
('green', 'S')
('green', 'M')
('green', 'L')
('red', 'S')
('red', 'M')
('red', 'L')
('black', 'S')
('black', 'M')
('black', 'L')
('green', 'S')
('green', 'M')
('green', 'L')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
章的作业主要是关于字符串和列表、元组的操作。对于字符串,可以使用索引和切片来获取和修改字符串中的字符,还可以使用内置函数,如len()来获取字符串的长度,以及count()、index()等函数来统计字符出现的次数和找出字符的位置。 对于列表和元组,可以使用索引和切片来获取和修改列表和元组中的元素,还可以使用内置函数,如len()来获取列表和元组的长度,以及count()、index()等函数来统计元素出现的次数和找出元素的位置。 作业可能包括以下内容: 1. 使用字符串的切片和索引操作,截取出指定的子字符串; 2. 使用字符串的内置函数,如count()、index()等来统计指定字符的出现次数和位置; 3. 使用列表的索引来获取和修改指定的元素; 4. 使用列表的切片操作,截取出指定的子列表; 5. 使用列表的内置函数,如append()、count()等来添加元素和统计指定元素的出现次数; 6. 使用元组的索引来获取指定的元素; 7. 使用元组的切片操作,截取出指定的子元组; 8. 使用元组的内置函数,如count()、index()等来统计指定元素的出现次数和位置。 为了完成第章的作业,我们需要熟悉字符串、列表和元组的基本操作和内置函数的使用。可以通过阅读和理解教材中的相关知识点和示例代码,然后按照作业的要求进行编写和测试。编写过程中需要注意细节和语法的正确性,同时也可以参考官方文档和其他相关资料进行查找和学习。完成作业后,可以将代码保存并运行,然后检查输出结果是否符合预期。如果有错误或不清楚的地方,可以再次阅读教材或者寻求帮助。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重生的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值