当前位置: 首页 > news >正文

python开发案例教程-清华大学出版社(张基温)答案(4.2)

目录

练习 4.2

1. 代码分析题

2. 程序设计题


练习 4.2

1. 代码分析题

阅读下面的代码,给出输出结果。

1 

class A:def __init__(self,a,b,c):self.x=a+b+ca= A(3,5,7);b = getattr(a,'x');setattr(a,'x',b+3);print(a.x)

18

2

class Person:def __init__(self, id):self.id = idwang =  Person(357); wang.__dict__['age'] = 26
wang.__dict__['major'] = 'computer';print (wang.age + len(wang.__dict__))

print(wang.__dict__)
print (wang.age + len(wang.__dict__))
print (wang.__dict__['age'] + len(wang.__dict__))

{'id': 357, 'age': 26, 'major': 'computer'}
29
29

3

class A:def __init__(self,x,y,z):self.w = x + y + za = A(3,5,7); b = getattr(a,'w'); setattr(a,'w',b + 1); print(a.w)

 16

4

class Index:def __getitem__(self,index):return indexx = Index()
for i in range(8):print(x[i],end = '*')

0*1*2*3*4*5*6*7*

5

class Index:data = [1,3,5,7,9]def __getitem__(self,index):print('getitem:',index)return self.data[index]x = Index(); x[0]; x[2]; x[3]; x[-1]

getitem: 0
getitem: 2
getitem: 3
getitem: -1

6

class Squares:def __init__(self,start,stop):self.value = start - 1self.stop = stopdef __iter__(self):return selfdef __next__(self):if self.value == self.stop:raise StopIterationself.value += 1return self.value ** 2for i in Squares(1,6):print(i,end = '<')

1<4<9<16<25<36<

7

class Prod:   def __init__(self, value):   self.value = value   def __call__(self, other):   return self.value * otherp = Prod(2); print (p(1) ); print (p(2))

2
4

8

class Life:   def __init__(self, name='name'):   print('Hello', name  ) self.name = name   def __del__(self):   print ('Goodby', self.name)brain = Life('Brain');brain = 'loretta' 

 Hello Brain
Goodby Brain

2. 程序设计题

1)编写一个类,用于实现如下功能。
① 将十进制数转换为二进制数。
② 进行二进制的四则计算。
③ 对于带小数点的数用科学计数法表示。

class BinaryCalculator:  def __init__(self):  self.converter = 2  def decimal_to_binary(self, decimal):  binary = bin(decimal)  return binary[2:]  # 去掉前缀 '0b'  def binary_calculator(self, binary_str):  # 加法  addition = self.calculate('add', binary_str)  # 减法  subtraction = self.calculate('subtract', binary_str)  # 乘法  multiplication = self.calculate('multiply', binary_str)  # 除法  division = self.calculate('divide', binary_str)  return addition, subtraction, multiplication, division  def calculate(self, operation, binary_str):  # 分离操作数和操作符  operator = eval(operation)  num1, num2 = map(int, binary_str.split(self.converter))  result = operator(num1, num2)  return str(result).zfill(len(binary_str))  # 用0填充到和原始二进制字符串一样长  def scientific_notation(self, decimal):  scientific = f"{decimal:.6e}"  # 用6位小数表示科学计数法  return scientific

2)编写一个三维向量类,实现下列功能。
① 向量的加、减计算。
② 向量和标量的乘、除计算。

http://www.lryc.cn/news/273955.html

相关文章:

  • 【MATLAB】【数字信号处理】线性卷积和抽样定理
  • 什么是 MVVM ?
  • Redis(一)
  • 自动驾驶预测-决策-规划-控制学习(1):自动驾驶框架、硬件、软件概述
  • SSM建材商城网站----计算机毕业设计
  • js逆向第9例:猿人学第2题-js混淆-动态cookie1
  • [论文分享]TimesURL:通用时间序列表示学习的自监督对比学习
  • 解决sublime中文符号乱码问题
  • 厚积薄发11年,鸿蒙究竟有多可怕
  • pyDAL一个python的ORM(4) pyDAL查询操作
  • 如何通过Python将各种数据写入到Excel工作表
  • 跟着cherno手搓游戏引擎【2】:日志系统spdlog和premake的使用
  • Ubuntu20.04 上启用 VCAN 用作本地调试
  • LeetCode(31) 下一个排列
  • Git LFS: 简单高效的大文件版本控制
  • 如何培养用户思维
  • 由浅入深理解C#中的事件
  • Nginx(十六) 配置文件详解 - server stream服务流
  • Css中默认与继承
  • gitee上的vue大屏项目
  • 【LeetCode:114. 二叉树展开为链表 | 二叉树 + 递归】
  • 社保养老金发放计算方法
  • 概率论基础复习题
  • c++,mutex,unique_lock,recursive_mutex,shared_mutex对比分析
  • MySQL与Oracle数据库在网络安全等级方面用到的命令
  • MySQL——视图
  • 【响应式编程-03】Lambda表达式底层实现原理
  • 深入理解可变参数
  • Centos7.9和Debian12部署Minio详细流程
  • 软件测试|教你如何使用UPDATE修改数据