python作业

目录

# 作业一

# 定义类

# 调用函数

# 作业二

# 模拟 range()

# 模拟 range(start, stop)

# 模拟 range(stop, stop, step)

作业三


作业一

定义一个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 = None

    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 class Method of class Book ")
    @classmethod
    def class_print_obj(cls):
        print("This is class Method of class Book ")

# 调用函数

实例化对象book1: Python程序设计, 王铮, 机械教育出版社, 22
实例化对象book2:  Java程序设计, 李刚, 清华大学出版社, 34
book1 = Book("python程序与设计", "王铮", "机械教育出版社", 22 )
book2 = Book("Java程序设计", "李刚", "清华大学出版社", 34)
执行book1 + book2: 并输出相加的结果
print(book1 + book2)      # python程序与设计Java程序设计
 执行print(book1)
print(book1)              # python程序与设计-王铮-机械教育出版社-22
执行 book1()
print(book1())            # python程序与设计
调用类变量,进行赋值100, 输出类变量
Book.count = 100
print(Book.count)         # 100
print(book1.count)        # 100
调用book1对象,并修改书名: Python程序设计修改为流畅的Python
book1.title = "流畅的Python"
print(book1.title)        # 流畅的Python
调用静态方法 static_print_obj
调用类方法 class_print_obj
Book.static_print_obj()   # This is class Method of class Book
Book.class_print_obj()    # This is class Method of class Book
book1.static_print_obj()  # This is class Method of class Book
book1.class_print_obj()   # This is class Method of class Book

作业二

用迭代器 模拟range的功能

range原型 range(start, stop[, step]) -> range object

分别模拟

range()

range(stop, stop)

range(stop, stop, step)

模拟 range()

class MyIterator:
    def __init__(self, num):
        self.num = num
        self.data = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.data < self.num:
            data = self.data
            self.data += 1
            return data
        else:
            raise StopIteration

for i in MyIterator(6):
    print(i)

模拟 range(start, stop)

class MyIterator:
    def __init__(self, start, stop):
        self.start = start
        self.stop = stop
        self.data = self.start
    def __iter__(self):
        return self
    def __next__(self):
        if self.data < self.stop:
            data = self.data
            self.data += 1
            return data
        else:
            raise StopIteration
for i in MyIterator(3, 6):
    print(i)

模拟 range(stop, stop, step)

class MyIterator:
    def __init__(self, start, stop, step):
        self.start = start
        self.stop = stop
        self.data = self.start
        self.step = step
    def __iter__(self):
        return self
    def __next__(self):
        if self.data < self.stop:
            data = self.data
            self.data += self.step
            return data
        else:
            raise StopIteration
for i in MyIterator(1, 10, 2):
    print(i)

作业三

定义一个生成器函数:(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 color_size(color_list, size_list):
    for color in color_list:
        for size in size_list:
            yield (color, size)


color_list = ["red", "black", "green"]
size_list = ["S", "M", "L"]
gen = color_size(color_list, size_list)
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))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱玩网络的小石

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

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

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

打赏作者

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

抵扣说明:

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

余额充值