{"id":374,"date":"2025-01-12T11:32:24","date_gmt":"2025-01-12T03:32:24","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=374"},"modified":"2025-01-12T11:32:24","modified_gmt":"2025-01-12T03:32:24","slug":"python-%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95%e5%ae%9e%e4%be%8b%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=374","title":{"rendered":"Python \u57fa\u672c\u8bed\u6cd5\u5b9e\u4f8b\u7ec3\u4e60"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b Python \u8bed\u6cd5\u7684\u5b9e\u4f8b\u7ec3\u4e60\uff1a<\/p>\n\n\n\n<p><strong>1. \u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6574\u6570\na = 10\nprint(a)\n\n# \u6d6e\u70b9\u6570\nb = 3.14\nprint(b)\n\n# \u5b57\u7b26\u4e32\nc = \"Hello, World!\"\nprint(c)\n\n# \u5e03\u5c14\u503c\nd = True\nprint(d)\n\n# \u5217\u8868\ne = &#91;1, 2, 3, 4, 5]\nprint(e)\n\n# \u5143\u7ec4\nf = (6, 7, 8, 9, 10)\nprint(f)\n\n# \u5b57\u5178\ng = {'name': 'Alice', 'age': 30}\nprint(g)<\/code><\/pre>\n\n\n\n<p><strong>2. \u63a7\u5236\u6d41\u8bed\u53e5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># if \u8bed\u53e5\nx = 10\nif x &gt; 5:\n    print(\"x is greater than 5\")\n\n# if-else \u8bed\u53e5\ny = 3\nif y &gt; 5:\n    print(\"y is greater than 5\")\nelse:\n    print(\"y is not greater than 5\")\n\n# for \u5faa\u73af\nfor i in range(5):\n    print(i)\n\n# while \u5faa\u73af\ncount = 0\nwhile count &lt; 5:\n    print(count)\n    count += 1<\/code><\/pre>\n\n\n\n<p><strong>3. \u51fd\u6570<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u51fd\u6570\ndef add_numbers(a, b):\n    return a + b\n\nprint(add_numbers(3, 4))\n\n# \u533f\u540d\u51fd\u6570\uff08lambda \u8868\u8fbe\u5f0f\uff09\nsquare = lambda x: x**2\nprint(square(5))<\/code><\/pre>\n\n\n\n<p><strong>4. \u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u751f\u6210\u4e00\u4e2a\u5305\u542b 1 \u5230 10 \u7684\u5e73\u65b9\u7684\u5217\u8868\nsquares = &#91;i**2 for i in range(1, 11)]\nprint(squares)<\/code><\/pre>\n\n\n\n<p><strong>5. \u7c7b\u548c\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def introduce(self):\n        print(f\"Hello, my name is {self.name} and I am {self.age} years old.\")\n\np = Person(\"Bob\", 25)\np.introduce()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b Python \u8bed\u6cd5\u7684\u5b9e\u4f8b\u7ec3\u4e60\uff1a 1. \u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b 2. \u63a7\u5236\u6d41\u8bed\u53e5 3. \u51fd\u6570 4. \u5217\u8868\u63a8\u5bfc\u5f0f 5. \u7c7b\u548c\u5bf9\u8c61<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=374"}],"version-history":[{"count":0,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}