Python打卡第31天

@浙大疏锦行

类的定义和方法作业:

|----------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 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 42 43 44 45 46 47 48 49 50 51 | import math class Circle: ``def __init__(``self``, radius ``= 1``): ``self``.radius ``= radius ``def calculate_area(``self``): ``return math.pi ``* self``.radius ``*``* 2 ``def calculate_circumference(``self``): ``return 2 * math.pi ``* self``.radius circle ``= Circle(``5``) print``(f``"Radius: {circle.radius}"``) print``(f``"Area: {circle.calculate_area()}"``) print``(f``"Circumference: {circle.calculate_circumference()}"``) class Rectangle: ``def __init__(``self``, width ``= 1``, height ``= 1``): ``self``.width ``= width ``self``.height ``= height ``def calculate_area(``self``): ``return self``.width ``* self``.height ``def calculate_perimeter(``self``): ``return 2 * (``self``.width ``+ self``.height) ``def is_square(``self``): ``return self``.width ``=``= self``.height rect ``= Rectangle(``4``, ``6``) print``(f``"Width: {rect.width}, Height: {rect.height}"``) print``(f``"Area: {rect.calculate_area()}"``) print``(f``"Perimeter: {rect.calculate_perimeter()}"``) print``(f``"Is square: {rect.is_square()}"``) def create_shape(shape_type, ``*``args): ``if shape_type ``=``= "circle"``: ``return Circle(``*``args) ``elif shape_type ``=``= "rectangle"``: ``return Rectangle(``*``args) ``else``: ``raise ValueError(``"Unknown shape type"``) # 测试 create_shape 函数 shape1 ``= create_shape(``"circle"``, ``5``) print``(shape1.calculate_circumference()) shape2 ``= create_shape(``"rectangle"``, ``4``, ``6``) print``(shape2.is_square()) |

相关推荐
IvanCodes1 小时前
LangChain(15):LangSmith 应用评估与回归测试
python·langchain·agent
Amumu121382 小时前
TypeScript学习: 进阶知识
开发语言·学习·typescript
炸薯条!2 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
Helix2503 小时前
JDK异常处理最佳实践:受检异常、非受检异常、try-catch-finally与全局异常处理完整指南
java·开发语言·jdk·异常处理·全局异常·自定义异常·try-catch
jason_renyu4 小时前
Playwright 入门与实操:从安装、定位、断言到自动发博客,把浏览器自动化讲明白
运维·python·自动化·前端开发·浏览器自动化·playwright·e2e测试
时间的情敌4 小时前
Three.js 后处理辉光 Bloom、告警发光效果、管线流光、着色器流动管线(ShaderMaterial)
开发语言·javascript·three.js·着色器
维克兜率天4 小时前
【维克】布林带:通道类指标的“量化基石“
人工智能·python·算法
Evand J4 小时前
【CIR系列2】UWB视距(LOS)与非视距(NLOS)信道冲激响应(CIR)建模与对比,MATLAB
开发语言·matlab·信号处理·nlos·cir
风落青芜4 小时前
C++入门:类和对象(二)
开发语言·数据结构·c++·算法
向科→向总工→空间动态计系统5 小时前
机器人大脑系统
开发语言·人工智能·python·机器人