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()) |

相关推荐
benchmark_cc1 小时前
用 QuantDash 读取单只股票五档盘口:抽样、缺失值检查与复现记录
python·数据分析·pandas·量化交易·股票数据·quantdash
曲径海盗1 小时前
【Python 学习第 55 天】列表基础
python·学习笔记·编程入门
专注仿真1 小时前
知识问答模型用到的算法集详解及代码实例
python·算法·代码·大模型训练·控制算法
jyOverQ2 小时前
我写了一个 CSDN Markdown 图片助手:告别本地图片逐张上传
python·工具·csdn
SomeOtherTime3 小时前
Rust所有权(AI回答)
开发语言·后端·rust
Easyhood3 小时前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
白杨尚青4 小时前
C++进阶篇(六):map 和 set 的模拟实现——用红黑树封装 mymap/myset(迭代器 + operator[])
开发语言·c++·迭代器·stl·set·map·红黑树
刘马想放假5 小时前
CANviz 使用指南|Linux 安装、DBC 解码、实时曲线与 J1939 实战
linux·python·websocket
2601_967987376 小时前
python判断变量名不要调用任何函数实现
python·函数·语法·判断·变量名
卷无止境7 小时前
SALib敏感性分析的python库学习笔记
后端·python