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

相关推荐
溪语流沙7 分钟前
【Web全栈进阶】监控与告警:线上系统的“体检报告”
前端·vue.js·python·fastapi
蜡笔羊驼16 分钟前
ModuleNotFoundError: No module named ‘torchvision.transforms.functional_tensor‘
linux·人工智能·python
传奇开心果编程17 分钟前
【Rust入门练中学】第9课:集合类型
开发语言·rust·编程语言理论·集合类型·通用编程语言
李航198319 分钟前
DeepDraw 网页版
人工智能·python·计算机视觉·ai·ai编程
龙孚信息24 分钟前
跨境电商独立站的安全盲区:一次 PHP 核心漏洞的影响评估
开发语言·安全·php
企业数字化笔记28 分钟前
一段视频有两次跳投,动作分析为什么要先做事件分段?
人工智能·python·音视频
牛牛牌蜡笔小新31 分钟前
Java-常用类库
java·开发语言
sunsijia2198340 分钟前
2026年10月22日 PHP漏洞临时处理
开发语言·php
Aurorar0rua1 小时前
CS50 x 2024 Notes Python - 02
开发语言·python·学习方法
樱桃木1 小时前
回测可信度审查:样本外测试的五种常见自欺
python