{"id":1155831,"date":"2025-01-13T18:08:37","date_gmt":"2025-01-13T10:08:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155831.html"},"modified":"2025-01-13T18:08:39","modified_gmt":"2025-01-13T10:08:39","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e7%9f%a9%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155831.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u77e9\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194405\/e6623df0-cd7f-4caa-88e8-1fd7a026b363.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u77e9\u5f62\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u8ba1\u7b97\u77e9\u5f62\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u6765\u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\u3001\u5468\u957f\u3001\u5bf9\u89d2\u7ebf\u957f\u5ea6\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u77e9\u5f62\u7c7b\u3001\u4f7f\u7528\u57fa\u672c\u7684\u6570\u5b66\u516c\u5f0f\u8fdb\u884c\u8ba1\u7b97\u3001\u7f16\u5199\u51fd\u6570\u8fdb\u884c\u8c03\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u7684\u9762\u79ef\u8ba1\u7b97\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9762\u79ef\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a\u9762\u79ef = \u957f\u5ea6 x \u5bbd\u5ea6\u3002<\/strong> \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u957f\u4e3alength\uff0c\u5bbd\u4e3awidth\u7684\u77e9\u5f62\uff0c\u5219\u5176\u9762\u79ef\u4e3alength * width\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7Python\u4ee3\u7801\u5b9e\u73b0\uff0c\u53ef\u4ee5\u5982\u4e0b\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def area(self):<\/p>\n<p>        return self.length * self.width<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>rect = Rectangle(10, 5)<\/p>\n<p>print(f&quot;\u77e9\u5f62\u7684\u9762\u79ef\u662f: {rect.area()}&quot;)  # \u8f93\u51fa: \u77e9\u5f62\u7684\u9762\u79ef\u662f: 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u77e9\u5f62\u7c7b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u77e9\u5f62\u7c7b\u6765\u5c01\u88c5\u77e9\u5f62\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u5305\u542b\u957f\u5ea6\u548c\u5bbd\u5ea6\u4f5c\u4e3a\u5c5e\u6027\uff0c\u4ee5\u53ca\u8ba1\u7b97\u9762\u79ef\u548c\u5468\u957f\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def area(self):<\/p>\n<p>        return self.length * self.width<\/p>\n<p>    def perimeter(self):<\/p>\n<p>        return 2 * (self.length + self.width)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u77e9\u5f62\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u77e9\u5f62\u5bf9\u8c61\u5e76\u8ba1\u7b97\u5176\u9762\u79ef\u548c\u5468\u957f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5)<\/p>\n<p>print(f&quot;\u77e9\u5f62\u7684\u9762\u79ef\u662f: {rect.area()}&quot;)  # \u8f93\u51fa: \u77e9\u5f62\u7684\u9762\u79ef\u662f: 50<\/p>\n<p>print(f&quot;\u77e9\u5f62\u7684\u5468\u957f\u662f: {rect.perimeter()}&quot;)  # \u8f93\u51fa: \u77e9\u5f62\u7684\u5468\u957f\u662f: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u5bf9\u89d2\u7ebf\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u77e9\u5f62\u7684\u5bf9\u89d2\u7ebf\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u52fe\u80a1\u5b9a\u7406\u8ba1\u7b97\u3002\u5bf9\u89d2\u7ebf\u957f\u5ea6\u7684\u516c\u5f0f\u4e3a\uff1a\u5bf9\u89d2\u7ebf\u957f\u5ea6 = sqrt(\u957f\u5ea6^2 + \u5bbd\u5ea6^2)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def diagonal(self):<\/p>\n<p>        return math.sqrt(self.length&lt;strong&gt;2 + self.width&lt;\/strong&gt;2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5)<\/p>\n<p>print(f&quot;\u77e9\u5f62\u7684\u5bf9\u89d2\u7ebf\u957f\u5ea6\u662f: {rect.diagonal()}&quot;)  # \u8f93\u51fa: \u77e9\u5f62\u7684\u5bf9\u89d2\u7ebf\u957f\u5ea6\u662f: 11.180339887498949<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5224\u65ad\u77e9\u5f62\u7684\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u77e9\u5f62\u7684\u957f\u5ea6\u548c\u5bbd\u5ea6\u8fdb\u884c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u5224\u65ad\u77e9\u5f62\u7684\u7c7b\u578b\uff08\u5982\u6b63\u65b9\u5f62\u6216\u666e\u901a\u77e9\u5f62\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def is_square(self):<\/p>\n<p>        return self.length == self.width<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect1 = Rectangle(10, 10)<\/p>\n<p>rect2 = Rectangle(10, 5)<\/p>\n<p>print(f&quot;rect1 \u662f\u6b63\u65b9\u5f62\u5417? {rect1.is_square()}&quot;)  # \u8f93\u51fa: rect1 \u662f\u6b63\u65b9\u5f62\u5417? True<\/p>\n<p>print(f&quot;rect2 \u662f\u6b63\u65b9\u5f62\u5417? {rect2.is_square()}&quot;)  # \u8f93\u51fa: rect2 \u662f\u6b63\u65b9\u5f62\u5417? False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6269\u5c55\u77e9\u5f62\u7c7b\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u77e9\u5f62\u7c7b\u7684\u529f\u80fd\uff0c\u4f7f\u5176\u80fd\u591f\u8fdb\u884c\u66f4\u591a\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u589e\u52a0\u4e00\u4e2a\u65b9\u6cd5\u6765\u7f29\u653e\u77e9\u5f62\u7684\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>    def scale(self, factor):<\/p>\n<p>        self.length *= factor<\/p>\n<p>        self.width *= factor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5)<\/p>\n<p>rect.scale(2)<\/p>\n<p>print(f&quot;\u7f29\u653e\u540e\u7684\u77e9\u5f62\u957f\u5ea6\u662f: {rect.length}, \u5bbd\u5ea6\u662f: {rect.width}&quot;)  # \u8f93\u51fa: \u7f29\u653e\u540e\u7684\u77e9\u5f62\u957f\u5ea6\u662f: 20, \u5bbd\u5ea6\u662f: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u77e9\u5f62\u7684\u5e73\u79fb<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u4e00\u4e2a\u65b9\u6cd5\u6765\u5e73\u79fb\u77e9\u5f62\u7684\u4f4d\u7f6e\u3002\u5047\u8bbe\u77e9\u5f62\u7684\u5de6\u4e0b\u89d2\u5750\u6807\u4e3a(x, y)\uff0c\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u65b9\u6cd5\u6765\u5e73\u79fb\u77e9\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width, x=0, y=0):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def move(self, dx, dy):<\/p>\n<p>        self.x += dx<\/p>\n<p>        self.y += dy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5, 0, 0)<\/p>\n<p>rect.move(3, 4)<\/p>\n<p>print(f&quot;\u5e73\u79fb\u540e\u7684\u77e9\u5f62\u5de6\u4e0b\u89d2\u5750\u6807\u662f: ({rect.x}, {rect.y})&quot;)  # \u8f93\u51fa: \u5e73\u79fb\u540e\u7684\u77e9\u5f62\u5de6\u4e0b\u89d2\u5750\u6807\u662f: (3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u77e9\u5f62\u7684\u65cb\u8f6c<\/h3>\n<\/p>\n<p><p>\u77e9\u5f62\u7684\u65cb\u8f6c\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u901a\u8fc7\u6570\u5b66\u65b9\u6cd5\u8ba1\u7b97\u65cb\u8f6c\u540e\u7684\u9876\u70b9\u5750\u6807\u3002\u8fd9\u91cc\u6211\u4eec\u4e0d\u8be6\u7ec6\u5c55\u5f00\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6570\u5b66\u5e93\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>class Rectangle:<\/p>\n<p>    def __init__(self, length, width, x=0, y=0):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def rotate(self, angle):<\/p>\n<p>        # \u8f6c\u6362\u89d2\u5ea6\u4e3a\u5f27\u5ea6<\/p>\n<p>        radians = np.radians(angle)<\/p>\n<p>        # \u65cb\u8f6c\u77e9\u9635<\/p>\n<p>        rotation_matrix = np.array([[np.cos(radians), -np.sin(radians)], [np.sin(radians), np.cos(radians)]])<\/p>\n<p>        # \u9876\u70b9\u5750\u6807<\/p>\n<p>        vertices = np.array([[self.x, self.y],<\/p>\n<p>                             [self.x + self.length, self.y],<\/p>\n<p>                             [self.x + self.length, self.y + self.width],<\/p>\n<p>                             [self.x, self.y + self.width]])<\/p>\n<p>        # \u65cb\u8f6c\u540e\u7684\u9876\u70b9\u5750\u6807<\/p>\n<p>        rotated_vertices = np.dot(vertices - np.array([self.x, self.y]), rotation_matrix) + np.array([self.x, self.y])<\/p>\n<p>        return rotated_vertices<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5, 0, 0)<\/p>\n<p>rotated_vertices = rect.rotate(45)<\/p>\n<p>print(f&quot;\u65cb\u8f6c\u540e\u7684\u9876\u70b9\u5750\u6807\u662f: {rotated_vertices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u77e9\u5f62\u4e0e\u70b9\u7684\u5173\u7cfb<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u70b9\u662f\u5426\u5728\u77e9\u5f62\u5185\u90e8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width, x=0, y=0):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_point(self, px, py):<\/p>\n<p>        return self.x &lt;= px &lt;= self.x + self.length and self.y &lt;= py &lt;= self.y + self.width<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect = Rectangle(10, 5, 0, 0)<\/p>\n<p>print(f&quot;\u70b9(3, 4)\u5728\u77e9\u5f62\u5185\u5417? {rect.contains_point(3, 4)}&quot;)  # \u8f93\u51fa: \u70b9(3, 4)\u5728\u77e9\u5f62\u5185\u5417? True<\/p>\n<p>print(f&quot;\u70b9(10, 10)\u5728\u77e9\u5f62\u5185\u5417? {rect.contains_point(10, 10)}&quot;)  # \u8f93\u51fa: \u70b9(10, 10)\u5728\u77e9\u5f62\u5185\u5417? False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u77e9\u5f62\u4e0e\u77e9\u5f62\u7684\u5173\u7cfb<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u77e9\u5f62\u662f\u5426\u76f8\u4ea4\u3001\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u77e9\u5f62\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Rectangle:<\/p>\n<p>    def __init__(self, length, width, x=0, y=0):<\/p>\n<p>        self.length = length<\/p>\n<p>        self.width = width<\/p>\n<p>        self.x = x<\/p>\n<p>        self.y = y<\/p>\n<p>    def intersects(self, other):<\/p>\n<p>        return not (self.x &gt; other.x + other.length or<\/p>\n<p>                    self.x + self.length &lt; other.x or<\/p>\n<p>                    self.y &gt; other.y + other.width or<\/p>\n<p>                    self.y + self.width &lt; other.y)<\/p>\n<p>    def contains(self, other):<\/p>\n<p>        return (self.x &lt;= other.x and<\/p>\n<p>                self.x + self.length &gt;= other.x + other.length and<\/p>\n<p>                self.y &lt;= other.y and<\/p>\n<p>                self.y + self.width &gt;= other.y + other.width)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rect1 = Rectangle(10, 5, 0, 0)<\/p>\n<p>rect2 = Rectangle(5, 5, 5, 0)<\/p>\n<p>rect3 = Rectangle(3, 3, 1, 1)<\/p>\n<p>print(f&quot;rect1 \u548c rect2 \u76f8\u4ea4\u5417? {rect1.intersects(rect2)}&quot;)  # \u8f93\u51fa: rect1 \u548c rect2 \u76f8\u4ea4\u5417? True<\/p>\n<p>print(f&quot;rect1 \u5305\u542b rect3 \u5417? {rect1.contains(rect3)}&quot;)  # \u8f93\u51fa: rect1 \u5305\u542b rect3 \u5417? True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u77e9\u5f62\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u77e9\u5f62\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u5305\u62ec\u8ba1\u7b97\u9762\u79ef\u3001\u5468\u957f\u3001\u5bf9\u89d2\u7ebf\u957f\u5ea6\u3001\u5224\u65ad\u7c7b\u578b\u3001\u7f29\u653e\u3001\u5e73\u79fb\u3001\u65cb\u8f6c\u3001\u70b9\u5305\u542b\u5224\u65ad\u3001\u77e9\u5f62\u76f8\u4ea4\u548c\u5305\u542b\u5224\u65ad\u7b49\u591a\u79cd\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u5f62\u7684\u5404\u79cd\u8ba1\u7b97\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u80fd\u529b\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u6765\u5c01\u88c5\u77e9\u5f62\u7684\u5404\u79cd\u5c5e\u6027\u548c\u884c\u4e3a\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u5f62\u7684\u5404\u79cd\u8ba1\u7b97\u548c\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u4e00\u6b65\u6269\u5c55\u77e9\u5f62\u7c7b\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u589e\u52a0\u66f4\u591a\u7684\u51e0\u4f55\u8fd0\u7b97\u65b9\u6cd5\uff0c\u6216\u8005\u7ed3\u5408\u5176\u4ed6\u5e93\uff08\u5982NumPy\u3001Matplotlib\u7b49\uff09\u8fdb\u884c\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u64cd\u4f5c\u3002\u901a\u8fc7\u4e0d\u65ad\u6269\u5c55\u548c\u4f18\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u77e9\u5f62\u7c7b\u66f4\u52a0\u5b8c\u5584\u548c\u5f3a\u5927\uff0c\u4ece\u800c\u66f4\u597d\u5730\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\u548c\u5468\u957f\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\u548c\u5468\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u516c\u5f0f\u3002\u9762\u79ef\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a\u9762\u79ef = \u957f \u00d7 \u5bbd\uff0c\u5468\u957f\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a\u5468\u957f = 2 \u00d7 (\u957f + \u5bbd)\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u8ba1\u7b97\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate_rectangle(length, width):\n    area = length * width\n    perimeter = 2 * (length + width)\n    return area, perimeter\n\nlength = 5\nwidth = 3\narea, perimeter = calculate_rectangle(length, width)\nprint(f&quot;\u9762\u79ef: {area}, \u5468\u957f: {perimeter}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u4efb\u610f\u77e9\u5f62\u7684\u9762\u79ef\u548c\u5468\u957f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u6216\u96f6\u7684\u77e9\u5f62\u8fb9\u957f\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u77e9\u5f62\u8ba1\u7b97\u65f6\uff0c\u8fb9\u957f\u5fc5\u987b\u662f\u6b63\u6570\u3002\u5982\u679c\u8f93\u5165\u7684\u8fb9\u957f\u4e3a\u8d1f\u6570\u6216\u96f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u5408\u7406\u7684\u7ed3\u679c\u3002\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u4ee5\u786e\u4fdd\u8f93\u5165\u6709\u6548\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate_rectangle(length, width):\n    if length &lt;= 0 or width &lt;= 0:\n        raise ValueError(&quot;\u957f\u548c\u5bbd\u5fc5\u987b\u662f\u6b63\u6570&quot;)\n    area = length * width\n    perimeter = 2 * (length + width)\n    return area, perimeter\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4e0d\u5408\u7406\u7684\u8ba1\u7b97\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u77e9\u5f62\u5e76\u663e\u793a\u5176\u9762\u79ef\u548c\u5468\u957f\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684\u56fe\u5f62\u5e93\uff0c\u5982Matplotlib\uff0c\u53ef\u4ee5\u7ed8\u5236\u77e9\u5f62\u5e76\u5728\u56fe\u5f62\u4e0a\u6807\u6ce8\u5176\u9762\u79ef\u548c\u5468\u957f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\ndef draw_rectangle(length, width):\n    rectangle = plt.Rectangle((0, 0), length, width, fill=None, edgecolor=&#39;r&#39;)\n    plt.gca().add_patch(rectangle)\n    plt.xlim(-1, length + 1)\n    plt.ylim(-1, width + 1)\n    plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\n    plt.title(f&quot;\u77e9\u5f62: \u9762\u79ef={length * width}, \u5468\u957f={2 * (length + width)}&quot;)\n    plt.show()\n\ndraw_rectangle(5, 3)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53ef\u89c6\u5316\u77e9\u5f62\u5e76\u540c\u65f6\u5c55\u793a\u5176\u9762\u79ef\u548c\u5468\u957f\uff0c\u589e\u5f3a\u7528\u6237\u7684\u7406\u89e3\u4e0e\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u8ba1\u7b97\u77e9\u5f62\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u6765\u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\u3001\u5468\u957f\u3001\u5bf9\u89d2\u7ebf\u957f\u5ea6\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u77e9\u5f62\u7c7b\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1155836,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155831"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1155831"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155831\/revisions"}],"predecessor-version":[{"id":1155839,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155831\/revisions\/1155839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}