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

相关推荐
invicinble2 小时前
python 编程语言 认识维度
开发语言·数据库·python
AC赳赳老秦2 小时前
数据采集全链路审计留痕:用 OpenClaw 实现合规审计与追溯
开发语言·汇编·python·php·swift·deepseek·openclaw
czhc11400756632 小时前
退出防呆:用“总闸“收口所有退出入口,以及三个并发语法坑
开发语言·c#
张铁霖2 小时前
深度解析:AI三次浪潮与机器学习三种范式,
python
学也学不废2 小时前
论文阅读:Advancing Semantic Textual Similarity Modeling
python
悠仁さん2 小时前
【C++】vector 类
开发语言·c++
风早爽太3 小时前
Python 开发笔记:配置生产环境中 Celery Worker 的独立进程启动方式
python·fastapi
_upupup3 小时前
智能指针的原理及简单模拟实现(C++)
开发语言·c++
打工仔折腾 AI3 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战