{"id":1084783,"date":"2025-01-08T13:10:17","date_gmt":"2025-01-08T05:10:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084783.html"},"modified":"2025-01-08T13:10:20","modified_gmt":"2025-01-08T05:10:20","slug":"python%e5%87%bd%e6%95%b0%e8%bf%94%e5%9b%9e%e5%a4%9a%e4%b8%aa%e5%80%bc%e6%97%b6%e5%a6%82%e4%bd%95%e6%8e%a5%e5%8f%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084783.html","title":{"rendered":"python\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u65f6\u5982\u4f55\u63a5\u53d7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194945\/747f0cbe-1696-4811-be51-2466b98643f7.webp\" alt=\"python\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u65f6\u5982\u4f55\u63a5\u53d7\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5143\u7ec4\u6765\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u6765\u89e3\u5305\u8fd9\u4e9b\u503c\uff0c\u6216\u8005\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u5bb9\u5668\u4e2d\uff0c\u6bd4\u5982\u4e00\u4e2a\u5217\u8868\u6216\u5b57\u5178\u3002<strong>\u4f7f\u7528\u5143\u7ec4\u89e3\u5305\u3001\u4f7f\u7528\u5bb9\u5668\u3001\u901a\u8fc7\u5b57\u5178\u6765\u8fd4\u56de\u591a\u4e2a\u503c<\/strong>\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u63cf\u8ff0\u548c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5143\u7ec4\u89e3\u5305<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u51fd\u6570\u9700\u8981\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u503c\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u5e76\u5728\u51fd\u6570\u8c03\u7528\u65f6\u89e3\u5305\u8fd9\u4e9b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_coordinates():<\/p>\n<p>    x = 10<\/p>\n<p>    y = 20<\/p>\n<p>    return x, y  # \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4<\/p>\n<p>x_val, y_val = get_coordinates()  # \u89e3\u5305\u5143\u7ec4<\/p>\n<p>print(f&quot;x: {x_val}, y: {y_val}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u6613\u4e8e\u9605\u8bfb\u3002\u4f60\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u6570\u91cf\u7684\u503c\uff0c\u5e76\u5728\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u5bf9\u5e94\u6570\u91cf\u7684\u53d8\u91cf\u6765\u89e3\u5305\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5143\u7ec4\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u89e3\u5305\u662f\u4e00\u79cd\u975e\u5e38\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u60c5\u51b5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u591a\u4e2a\u8fd4\u56de\u503c\u53ef\u4ee5\u88ab\u76f4\u63a5\u5206\u914d\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_statistics(data):<\/p>\n<p>    mean = sum(data) \/ len(data)<\/p>\n<p>    variance = sum((x - mean)  2 for x in data) \/ len(data)<\/p>\n<p>    std_dev = variance  0.5<\/p>\n<p>    return mean, variance, std_dev<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>mean, variance, std_dev = calculate_statistics(data)<\/p>\n<p>print(f&quot;Mean: {mean}, Variance: {variance}, Standard Deviation: {std_dev}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>calculate_statistics<\/code>\u51fd\u6570\u8fd4\u56de\u4e09\u4e2a\u503c\uff1a\u5747\u503c\u3001\u65b9\u5dee\u548c\u6807\u51c6\u5dee\u3002\u8fd9\u4e9b\u503c\u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u5e76\u5728\u51fd\u6570\u8c03\u7528\u65f6\u89e3\u5305\u5230\u4e09\u4e2a\u53d8\u91cf\u4e2d\u3002\u8fd9\u6837\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u7b80\u6d01\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fd4\u56de\u4e00\u7ec4\u76f8\u5173\u7684\u503c\uff0c\u4f46\u4e0d\u786e\u5b9a\u5b83\u4eec\u7684\u5177\u4f53\u6570\u91cf\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u5b57\u5178\u6765\u5b58\u50a8\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u6570\u91cf\u7684\u5143\u7d20\u3002\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u591a\u4e2a\u8fd4\u56de\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_extremes(numbers):<\/p>\n<p>    return [min(numbers), max(numbers)]<\/p>\n<p>numbers = [4, 1, 8, 3, 7]<\/p>\n<p>extremes = find_extremes(numbers)<\/p>\n<p>print(f&quot;Min: {extremes[0]}, Max: {extremes[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>find_extremes<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff1a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u4f7f\u8fd4\u56de\u503c\u7684\u542b\u4e49\u66f4\u52a0\u660e\u786e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_user_info():<\/p>\n<p>    return {<\/p>\n<p>        &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>        &#39;age&#39;: 30,<\/p>\n<p>        &#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;: &#39;alice@example.com&#39;<\/p>\n<p>    }<\/p>\n<p>user_info = get_user_info()<\/p>\n<p>print(f&quot;Name: {user_info[&#39;name&#39;]}, Age: {user_info[&#39;age&#39;]}, Email: {user_info[&#39;email&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>get_user_info<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u7528\u6237\u7684\u59d3\u540d\u3001\u5e74\u9f84\u548c\u7535\u5b50\u90ae\u4ef6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u547d\u540d\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\uff08namedtuple\uff09\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7c7b\u4f3c\u4e8e\u5143\u7ec4\uff0c\u4f46\u5177\u6709\u5b57\u6bb5\u540d\u79f0\uff0c\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u4e00\u6837\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<p>def get_car_info():<\/p>\n<p>    Car = namedtuple(&#39;Car&#39;, [&#39;make&#39;, &#39;model&#39;, &#39;year&#39;])<\/p>\n<p>    return Car(make=&#39;Toyota&#39;, model=&#39;Corolla&#39;, year=2020)<\/p>\n<p>car = get_car_info()<\/p>\n<p>print(f&quot;Make: {car.make}, Model: {car.model}, Year: {car.year}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>get_car_info<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u547d\u540d\u5143\u7ec4Car\uff0c\u5305\u542b\u6c7d\u8f66\u7684\u5236\u9020\u5546\u3001\u578b\u53f7\u548c\u5e74\u4efd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5\u540d\u79f0\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u7c7b\uff08Data Class\uff09<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\uff08Data Class\uff09\u662fPython 3.7\u5f15\u5165\u7684\u4e00\u79cd\u7b80\u5316\u7c7b\u5b9a\u4e49\u7684\u65b9\u5f0f\u3002\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u6765\u5b58\u50a8\u591a\u4e2a\u76f8\u5173\u7684\u503c\uff0c\u5e76\u4e14\u6613\u4e8e\u4f7f\u7528\u548c\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<p>@dataclass<\/p>\n<p>class Book:<\/p>\n<p>    title: str<\/p>\n<p>    author: str<\/p>\n<p>    year: int<\/p>\n<p>def get_book_info():<\/p>\n<p>    return Book(title=&#39;1984&#39;, author=&#39;George Orwell&#39;, year=1949)<\/p>\n<p>book = get_book_info()<\/p>\n<p>print(f&quot;Title: {book.title}, Author: {book.author}, Year: {book.year}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>get_book_info<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aBook\u6570\u636e\u7c7b\u5b9e\u4f8b\uff0c\u5305\u542b\u4e66\u7684\u6807\u9898\u3001\u4f5c\u8005\u548c\u5e74\u4efd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fd4\u56de\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u5c06\u5b83\u4eec\u5c01\u88c5\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u7ed3\u6784\u6027\uff0c\u5e76\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age, email):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.email = email<\/p>\n<p>def create_person():<\/p>\n<p>    return Person(name=&#39;Bob&#39;, age=25, email=&#39;bob@example.com&#39;)<\/p>\n<p>person = create_person()<\/p>\n<p>print(f&quot;Name: {person.name}, Age: {person.age}, Email: {person.email}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>create_person<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aPerson\u5bf9\u8c61\uff0c\u5305\u542b\u4e2a\u4eba\u7684\u59d3\u540d\u3001\u5e74\u9f84\u548c\u7535\u5b50\u90ae\u4ef6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        yield i<\/p>\n<p>for number in generate_numbers():<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>generate_numbers<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u9010\u4e2a\u751f\u62100\u52304\u7684\u6570\u5b57\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u8fed\u4ee3\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u7c7b\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5c06\u51fd\u6570\u5b9a\u4e49\u4e3a\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u6a21\u5757\u6027\u548c\u53ef\u8bfb\u6027\u3002\u8fd9\u6837\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fd4\u56de\u503c\u5c01\u88c5\u5728\u7c7b\u5b9e\u4f8b\u4e2d\uff0c\u5e76\u901a\u8fc7\u65b9\u6cd5\u6765\u8bbf\u95ee\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Calculator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def calculate_statistics(self):<\/p>\n<p>        mean = sum(self.data) \/ len(self.data)<\/p>\n<p>        variance = sum((x - mean)  2 for x in self.data) \/ len(self.data)<\/p>\n<p>        std_dev = variance  0.5<\/p>\n<p>        return mean, variance, std_dev<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>calculator = Calculator(data)<\/p>\n<p>mean, variance, std_dev = calculator.calculate_statistics()<\/p>\n<p>print(f&quot;Mean: {mean}, Variance: {variance}, Standard Deviation: {std_dev}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>calculate_statistics<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e09\u4e2a\u503c\uff1a\u5747\u503c\u3001\u65b9\u5dee\u548c\u6807\u51c6\u5dee\u3002\u8fd9\u4e9b\u503c\u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u5e76\u5728\u65b9\u6cd5\u8c03\u7528\u65f6\u89e3\u5305\u5230\u4e09\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    mean = sum(data) \/ len(data)<\/p>\n<p>    variance = sum((x - mean)  2 for x in data) \/ len(data)<\/p>\n<p>    std_dev = variance  0.5<\/p>\n<p>    return mean, variance, std_dev<\/p>\n<p>def save_to_file(data, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        for item in data:<\/p>\n<p>            file.write(f&quot;{item}\\n&quot;)<\/p>\n<p>def load_from_file(filename):<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        data = [float(line.strip()) for line in file]<\/p>\n<p>    return data<\/p>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5143\u7ec4\u89e3\u5305<\/strong><\/h2>\n<p>mean, variance, std_dev = process_data(data)<\/p>\n<p>print(f&quot;Mean: {mean}, Variance: {variance}, Standard Deviation: {std_dev}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868<\/strong><\/h2>\n<p>extremes = [min(data), max(data)]<\/p>\n<p>print(f&quot;Min: {extremes[0]}, Max: {extremes[1]}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178<\/strong><\/h2>\n<p>user_info = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;email&#39;: &#39;alice@example.com&#39;}<\/p>\n<p>print(f&quot;Name: {user_info[&#39;name&#39;]}, Age: {user_info[&#39;age&#39;]}, Email: {user_info[&#39;email&#39;]}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u547d\u540d\u5143\u7ec4<\/strong><\/h2>\n<p>from collections import namedtuple<\/p>\n<p>Car = namedtuple(&#39;Car&#39;, [&#39;make&#39;, &#39;model&#39;, &#39;year&#39;])<\/p>\n<p>car = Car(make=&#39;Toyota&#39;, model=&#39;Corolla&#39;, year=2020)<\/p>\n<p>print(f&quot;Make: {car.make}, Model: {car.model}, Year: {car.year}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u6570\u636e\u7c7b<\/strong><\/h2>\n<p>from dataclasses import dataclass<\/p>\n<p>@dataclass<\/p>\n<p>class Book:<\/p>\n<p>    title: str<\/p>\n<p>    author: str<\/p>\n<p>    year: int<\/p>\n<p>book = Book(title=&#39;1984&#39;, author=&#39;George Orwell&#39;, year=1949)<\/p>\n<p>print(f&quot;Title: {book.title}, Author: {book.author}, Year: {book.year}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5bf9\u8c61<\/strong><\/h2>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age, email):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.email = email<\/p>\n<p>person = Person(name=&#39;Bob&#39;, age=25, email=&#39;bob@example.com&#39;)<\/p>\n<p>print(f&quot;Name: {person.name}, Age: {person.age}, Email: {person.email}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/h2>\n<p>def generate_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        yield i<\/p>\n<p>for number in generate_numbers():<\/p>\n<p>    print(number)<\/p>\n<h2><strong>\u4f7f\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>class Calculator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def calculate_statistics(self):<\/p>\n<p>        mean = sum(self.data) \/ len(self.data)<\/p>\n<p>        variance = sum((x - mean)  2 for x in self.data) \/ len(self.data)<\/p>\n<p>        std_dev = variance  0.5<\/p>\n<p>        return mean, variance, std_dev<\/p>\n<p>calculator = Calculator(data)<\/p>\n<p>mean, variance, std_dev = calculator.calculate_statistics()<\/p>\n<p>print(f&quot;Mean: {mean}, Variance: {variance}, Standard Deviation: {std_dev}&quot;)<\/p>\n<h2><strong>\u4fdd\u5b58\u548c\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>save_to_file(data, &#39;data.txt&#39;)<\/p>\n<p>loaded_data = load_from_file(&#39;data.txt&#39;)<\/p>\n<p>print(f&quot;Loaded data: {loaded_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u8fd4\u56de\u548c\u63a5\u53d7\u591a\u4e2a\u503c\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u6a21\u5757\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u9017\u53f7\u5206\u9694\u7684\u65b9\u5f0f\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5217\u8868\u6216\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u503c\uff1a<\/p>\n<pre><code class=\"language-python\">def calculate(a, b):\n    return a + b, a - b\n<\/code><\/pre>\n<p><strong>\u8c03\u7528\u4e00\u4e2a\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u51fd\u6570\u65f6\u5e94\u8be5\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8c03\u7528\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u6765\u63a5\u6536\u8fd9\u4e9b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">sum_result, diff_result = calculate(10, 5)\nprint(sum_result)  # \u8f93\u51fa 15\nprint(diff_result)  # \u8f93\u51fa 5\n<\/code><\/pre>\n<p>\u786e\u4fdd\u63a5\u6536\u7684\u53d8\u91cf\u6570\u91cf\u4e0e\u8fd4\u56de\u503c\u7684\u6570\u91cf\u76f8\u5339\u914d\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u51fd\u6570\u8fd4\u56de\u7684\u591a\u4e2a\u503c\uff1f<\/strong><br \/>\u63a5\u6536\u591a\u4e2a\u8fd4\u56de\u503c\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e9b\u503c\u8fdb\u884c\u540e\u7eed\u7684\u8fd0\u7b97\u6216\u903b\u8f91\u5904\u7406\u3002\u8fd4\u56de\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8ba1\u7b97\u3001\u5b58\u50a8\u6216\u4f20\u9012\u5230\u5176\u4ed6\u51fd\u6570\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def get_statistics(data):\n    mean = sum(data) \/ len(data)\n    variance = sum((x - mean) ** 2 for x in data) \/ len(data)\n    return mean, variance\n\ndata = [1, 2, 3, 4, 5]\nmean, variance = get_statistics(data)\nprint(f&quot;Mean: {mean}, Variance: {variance}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u51fd\u6570\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u8fd4\u56de\u591a\u4e2a\u76f8\u5173\u7684\u7ed3\u679c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5143\u7ec4\u6765\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u6765\u89e3\u5305\u8fd9\u4e9b\u503c\uff0c\u6216\u8005\u5c06\u5b83\u4eec [&hellip;]","protected":false},"author":3,"featured_media":1084786,"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\/1084783"}],"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=1084783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084783\/revisions"}],"predecessor-version":[{"id":1084790,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084783\/revisions\/1084790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084786"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}