{"id":3435,"date":"2023-06-06T07:26:05","date_gmt":"2023-06-05T23:26:05","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3435"},"modified":"2023-06-13T18:40:37","modified_gmt":"2023-06-13T10:40:37","slug":"python%e7%9a%84%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%95%e5%8c%85%e6%8b%ac%ef%bc%9a","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3435","title":{"rendered":"Python\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\uff1a"},"content":{"rendered":"\n<p><a>Python\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\uff1a<\/a><a>\u53d8\u91cf<\/a><a>\u6570\u636e\u7c7b\u578b<\/a><a>\u5217\u8868\u3001\u5b57\u5178\u4f7f\u7528\u4e3e\u4f8b<\/a><a>python\u8fd0\u7b97\u7b26\u4e3e\u4f8b<\/a><a>\u63a7\u5236\u8bed\u53e5\uff1a<\/a><a>\u51fd\u6570<\/a><a>\u6a21\u5757<\/a><a>\u5f02\u5e38\u5904\u7406<\/a><a>\u6587\u4ef6\u64cd\u4f5c<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Python\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\uff1a<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53d8\u91cf\uff1aPython\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u4e0d\u9700\u8981\u4e8b\u5148\u58f0\u660e\u53d8\u91cf\u7c7b\u578b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002\u5982\uff1a<code>a = 1<\/code>\u3002<\/li>\n\n\n\n<li>\u6570\u636e\u7c7b\u578b\uff1aPython\u652f\u6301\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u8fd0\u7b97\u7b26\uff1aPython\u652f\u6301\u5e38\u89c1\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u7b49\u3002<\/li>\n\n\n\n<li>\u63a7\u5236\u8bed\u53e5\uff1aPython\u652f\u6301if\/elif\/else\u3001for\u3001while\u7b49\u63a7\u5236\u8bed\u53e5\uff0c\u7528\u4e8e\u5b9e\u73b0\u6761\u4ef6\u5224\u65ad\u3001\u5faa\u73af\u7b49\u529f\u80fd\u3002<\/li>\n\n\n\n<li>\u51fd\u6570\uff1aPython\u652f\u6301\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u3001\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u7b49\u3002<\/li>\n\n\n\n<li>\u6a21\u5757\uff1aPython\u4e2d\u7684\u6a21\u5757\u53ef\u7531\u81ea\u5df1\u7f16\u5199\uff0c\u4e5f\u53ef\u4ee5\u5916\u90e8\u5f15\u5165\u3002\u901a\u8fc7\u6a21\u5757\u53ef\u4ee5\u6709\u6548\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u590d\u7528\u7387\u3002<\/li>\n\n\n\n<li>\u5f02\u5e38\u5904\u7406\uff1aPython\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ca\u65f6\u6355\u83b7\u9519\u8bef\u4fe1\u606f\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u6587\u4ef6\u64cd\u4f5c\uff1aPython\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684open\u51fd\u6570\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0a\u662fPython\u57fa\u672c\u8bed\u6cd5\u7684\u51e0\u4e2a\u65b9\u9762\uff0c\u9700\u8981\u4e0d\u65ad\u5b66\u4e60\u548c\u638c\u63e1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53d8\u91cf<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684Python\u53d8\u91cf\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6574\u578b\u53d8\u91cf\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">age = 25<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>age<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u6574\u657025\u3002\u5728Python\u4e2d\uff0c\u6574\u6570\u7c7b\u578b\u4f7f\u7528<code>int<\/code>\u8868\u793a\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u6d6e\u70b9\u578b\u53d8\u91cf\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">price = 19.99<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>price<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u6d6e\u70b9\u657019.99\u3002\u5728Python\u4e2d\uff0c\u6d6e\u70b9\u6570\u7c7b\u578b\u4f7f\u7528<code>float<\/code>\u8868\u793a\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u5b57\u7b26\u4e32\u53d8\u91cf\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">name = 'Alice'<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>name<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u5b57\u7b26\u4e32&#8221;Alice&#8221;\u3002\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\u4f7f\u7528<code>str<\/code>\u8868\u793a\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u5e03\u5c14\u578b\u53d8\u91cf\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">is_student = True<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>is_student<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u5e03\u5c14\u503cTrue\u3002\u5728Python\u4e2d\uff0c\u5e03\u5c14\u503c\u7c7b\u578b\u4f7f\u7528<code>bool<\/code>\u8868\u793a\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u58f0\u660e\u5176\u7c7b\u578b\uff0cPython\u4f1a\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7c7b\u578b\u3002\u5728\u8d4b\u503c\u65f6\u4f7f\u7528\u7684\u6570\u636e\u503c\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u540c\u65f6\uff0cPython\u53d8\u91cf\u4e5f\u5141\u8bb8\u52a8\u6001\u7c7b\u578b\uff0c\u5373\u53d8\u91cf\u53ef\u4ee5\u968f\u65f6\u6539\u53d8\u5176\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">my_var = 10<br>my_var = 'hello'<br>my_var = True<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u540d\u4e3a<code>my_var<\/code>\u7684\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u6574\u6570\u3001\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\u3002\u6bcf\u6b21\u8d4b\u503c\u540e\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u90fd\u4f1a\u6539\u53d8\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u53d8\u91cf\u793a\u4f8b\u5c55\u793a\u4e86Python\u4e2d\u6700\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u548c\u53d8\u91cf\u7528\u6cd5\uff0cPython\u7684\u53d8\u91cf\u548c\u6570\u636e\u7c7b\u578b\u975e\u5e38\u7075\u6d3b\uff0c\u4f7f\u5f97Python\u5728\u5404\u79cd\u7f16\u7a0b\u4efb\u52a1\u4e2d\u90fd\u5f88\u53d7\u6b22\u8fce\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u7c7b\u578b<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u76f8\u5e94\u7684Python\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6574\u578b\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">x = 1<br>y = 100<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u6574\u578b\u53d8\u91cf<code>x<\/code>\u548c<code>y<\/code>\uff0c\u5206\u522b\u8d4b\u503c\u4e3a1\u548c100\u3002\u5728Python\u4e2d\u6574\u578b\u6570\u636e\u7c7b\u578b\u8868\u793a\u4e3a<code>int<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u6d6e\u70b9\u578b\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">pi = 3.14<br>radius = 5.5<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u6d6e\u70b9\u578b\u53d8\u91cf<code>pi<\/code>\u548c<code>radius<\/code>\uff0c\u5206\u522b\u8d4b\u503c\u4e3a3.14\u548c5.5\u3002\u5728Python\u4e2d\u6d6e\u70b9\u578b\u6570\u636e\u7c7b\u578b\u8868\u793a\u4e3a<code>float<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">name = \"Alice\"<br>greeting = 'Hello, world!'<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf<code>name<\/code>\u548c<code>greeting<\/code>\uff0c\u5206\u522b\u8d4b\u503c\u4e3a&#8221;Alice&#8221;\u548c&#8221;Hello, world!&#8221;\u3002\u5728Python\u4e2d\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u8868\u793a\u4e3a<code>str<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u5e03\u5c14\u578b\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">is_student = True<br>has_car = False<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u5e03\u5c14\u578b\u53d8\u91cf<code>is_student<\/code>\u548c<code>has_car<\/code>\uff0c\u5206\u522b\u8d4b\u503c\u4e3aTrue\u548cFalse\u3002\u5728Python\u4e2d\u5e03\u5c14\u578b\u6570\u636e\u7c7b\u578b\u8868\u793a\u4e3a<code>bool<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>\u5217\u8868\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = ['apple', 'banana', 'orange']<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5217\u8868\u53d8\u91cf<code>fruits<\/code>\uff0c\u5305\u542b\u4e86\u4e09\u79cd\u4e0d\u540c\u7684\u6c34\u679c\u3002\u5217\u8868\u6570\u636e\u7c7b\u578b\u5728Python\u4e2d\u53ef\u4ee5\u4fdd\u5b58\u591a\u4e2a\u503c\uff0c\u5e76\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u64cd\u4f5c\u548c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li>\u5b57\u5178\u6570\u636e\u7c7b\u578b:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">person = {'name': 'Alice', 'age': 25, 'is_student': True}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178\u53d8\u91cf<code>person<\/code>\uff0c\u5305\u542b\u4e86\u4e00\u4e2a\u4eba\u7684\u59d3\u540d\u3001\u5e74\u9f84\u548c\u5b66\u751f\u72b6\u6001\u3002\u5b57\u5178\u6570\u636e\u7c7b\u578b\u5728Python\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u548c\u8bbf\u95ee\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u793a\u4f8b\u663e\u793a\u4e86Python\u4e2d\u6700\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u548c\u7528\u6cd5\u3002Python\u4e2d\u8fd8\u6709\u5176\u4ed6\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u8282\u4e32\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u662fPython\u7f16\u7a0b\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5217\u8868\u3001\u5b57\u5178\u4f7f\u7528\u4e3e\u4f8b<\/h3>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u5217\u8868\u548c\u5b57\u5178\u662f\u4e24\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4e0b\u662f\u5b83\u4eec\u7684\u7528\u6cd5\u793a\u4f8b\u53ca\u76f8\u5e94\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5217\u8868<\/li>\n<\/ol>\n\n\n\n<p>\u5217\u8868\u4f7f\u7528\u4e2d\u62ec\u53f7[]\u6765\u8868\u793a\uff0c\u5176\u4e2d\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u503c\uff0c\u6bcf\u4e2a\u503c\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7\u5206\u9694\u5f00\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = ['apple', 'banana', 'orange']<br>print(fruits)  # \u8f93\u51fa ['apple', 'banana', 'orange']<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>fruits<\/code>\u7684\u5217\u8868\u53d8\u91cf\uff0c\u5305\u542b\u4e86\u4e09\u79cd\u6c34\u679c\u3002\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u6574\u4e2a\u5217\u8868\u3002<\/p>\n\n\n\n<p>\u5217\u8868\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e0b\u6807\u6765\u8bbf\u95ee\u5176\u4e2d\u7684\u5143\u7d20\uff0c\u4e0b\u6807\u4ece0\u5f00\u59cb\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(fruits[0])  # \u8f93\u51fa 'apple'<br>print(fruits[1])  # \u8f93\u51fa 'banana'<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8bbf\u95ee\u4e86\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u5217\u8868\u8fd8\u5177\u5907\u66f4\u6539\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u7b49\u64cd\u4f5c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u66f4\u6539\u5217\u8868\u4e2d\u7b2c\u4e8c\u4e2a\u5143\u7d20<br>fruits[1] = 'kiwi'<br>print(fruits)  # \u8f93\u51fa ['apple', 'kiwi', 'orange']<br><br># \u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20<br>fruits.append('grape')<br>print(fruits)  # \u8f93\u51fa ['apple', 'kiwi', 'orange', 'grape']<br><br># \u4ece\u5217\u8868\u4e2d\u5220\u9664\u7b2c\u4e00\u4e2a\u5143\u7d20<br>del fruits[0]<br>print(fruits)  # \u8f93\u51fa ['kiwi', 'orange', 'grape']<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u66f4\u6539\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5217\u8868\u5143\u7d20\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u5b57\u5178<\/li>\n<\/ol>\n\n\n\n<p>\u5b57\u5178\u4f7f\u7528\u82b1\u62ec\u53f7{}\u6765\u8868\u793a\uff0c\u5176\u4e2d\u5305\u542b\u82e5\u5e72\u952e\u503c\u5bf9\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">person = {'name': 'Alice', 'age': 25, 'is_student': True}<br>print(person)  # \u8f93\u51fa {'name': 'Alice', 'age': 25, 'is_student': True}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>person<\/code>\u7684\u5b57\u5178\u53d8\u91cf\uff0c\u5305\u542b\u4e86\u4e00\u4e2a\u4eba\u7684\u59d3\u540d\u3001\u5e74\u9f84\u548c\u5b66\u751f\u72b6\u6001\u3002\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u6574\u4e2a\u5b57\u5178\u3002<\/p>\n\n\n\n<p>\u5b57\u5178\u7684\u952e\u503c\u5bf9\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(person['name'])  # \u8f93\u51fa 'Alice'<br>print(person['age'])  # \u8f93\u51fa 25<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8bbf\u95ee\u4e86\u5b57\u5178\u4e2d\u952e\u4e3a&#8217;name&#8217;\u548c&#8217;age&#8217;\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u5b57\u5178\u8fd8\u652f\u6301\u66f4\u6539\u3001\u6dfb\u52a0\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\u64cd\u4f5c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5c06\u5e74\u9f84\u6539\u4e3a27<br>person['age'] = 27<br>print(person)  # \u8f93\u51fa {'name': 'Alice', 'age': 27, 'is_student': True}<br><br># \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<br>person['gender'] = 'female'<br>print(person)  # \u8f93\u51fa {'name': 'Alice', 'age': 27, 'is_student': True, 'gender': 'female'}<br><br># \u5220\u9664\u4e00\u4e2a\u952e\u503c\u5bf9<br>del person['is_student']<br>print(person)  # \u8f93\u51fa {'name': 'Alice', 'age': 27, 'gender': 'female'}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u66f4\u6539\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5b57\u5178\u952e\u503c\u5bf9\u7684\u64cd\u4f5c\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u5f97\u6211\u4eec\u7075\u6d3b\u5730\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">python\u8fd0\u7b97\u7b26\u4e3e\u4f8b<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6709\u5f88\u591a\u79cd\u8fd0\u7b97\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8fd0\u7b97\u7b26\u53ca\u5176\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7b97\u672f\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n\n\n\n<p>\u7b97\u672f\u8fd0\u7b97\u7b26\u7528\u4e8e\u6267\u884c\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 10<br>b = 3<br><br>print(a + b)  # \u8f93\u51fa 13<br>print(a - b)  # \u8f93\u51fa 7<br>print(a * b)  # \u8f93\u51fa 30<br>print(a \/ b)  # \u8f93\u51fa 3.3333333333333335<br>print(a % b)  # \u8f93\u51fa 1\uff08\u53d6\u6a21\u8fd0\u7b97\uff0c\u5373\u6c42\u4f59\u6570\uff09<br>print(a ** b)  # \u8f93\u51fa 1000\uff08\u4e58\u65b9\u8fd0\u7b97\uff09<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3001\u6c42\u4f59\u548c\u4e58\u65b9\u8fd0\u7b97\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n\n\n\n<p>\u6bd4\u8f83\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u8fd4\u56de\u5e03\u5c14\u503cTrue\u6216False\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 10<br>b = 3<br><br>print(a &gt; b)   # \u8f93\u51fa True<br>print(a &lt; b)   # \u8f93\u51fa False<br>print(a == b)  # \u8f93\u51fa False<br>print(a != b)  # \u8f93\u51fa True<br>print(a &gt;= b)  # \u8f93\u51fa True<br>print(a &lt;= b)  # \u8f93\u51fa False<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5927\u4e8e\u3001\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u4e0d\u7b49\u4e8e\u3001\u5927\u4e8e\u7b49\u4e8e\u548c\u5c0f\u4e8e\u7b49\u4e8e\u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u903b\u8f91\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n\n\n\n<p>\u903b\u8f91\u8fd0\u7b97\u7b26\u7528\u4e8e\u6267\u884c\u5e03\u5c14\u8fd0\u7b97\uff0c\u8fd4\u56de\u5e03\u5c14\u503cTrue\u6216False\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 10<br>b = 3<br><br>print(a &gt; 5 and b &gt; 1)  # \u8f93\u51fa True<br>print(a &lt; 5 or b &lt; 1)   # \u8f93\u51fa False<br>print(not(a &gt; 5 or b &lt; 1))  # \u8f93\u51fa True<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86and\u3001or\u548cnot\u8fd0\u7b97\u7b26\u3002\u5176\u4e2d\uff0cand\u8868\u793a\u903b\u8f91\u4e0e\u64cd\u4f5c\uff0cor\u8868\u793a\u903b\u8f91\u6216\u64cd\u4f5c\uff0cnot\u8868\u793a\u903b\u8f91\u975e\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u8d4b\u503c\u8fd0\u7b97\u7b26<\/li>\n<\/ol>\n\n\n\n<p>\u8d4b\u503c\u8fd0\u7b97\u7b26\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 10<br>b = 3<br><br>c = a + b<br>print(c)  # \u8f93\u51fa 13<br><br>a += b<br>print(a)  # \u8f93\u51fa 13<br><br>a -= b<br>print(a)  # \u8f93\u51fa 10<br><br>a *= b<br>print(a)  # \u8f93\u51fa 30<br><br>a \/= b<br>print(a)  # \u8f93\u51fa 10.0<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec+=\u3001-=\u3001*=\u548c\/=\u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u662fPython\u4e2d\u5e38\u7528\u7684\u8fd0\u7b97\u7b26\u53ca\u5176\u7528\u6cd5\u793a\u4f8b\uff0c\u4e0d\u540c\u7684\u8fd0\u7b97\u7b26\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u6709\u7740\u4e0d\u540c\u7684\u7528\u9014\uff0c\u719f\u6089\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u5f00\u53d1Python\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u63a7\u5236\u8bed\u53e5\uff1a<\/h2>\n\n\n\n<p>Python\u652f\u6301\u591a\u79cd\u63a7\u5236\u8bed\u53e5\uff0c\u5305\u62ecif\/elif\/else\u3001for\u3001while\u7b49\uff0c\u4ee5\u4e0b\u662f\u5b83\u4eec\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>if\/elif\/else\u6761\u4ef6\u8bed\u53e5<\/li>\n<\/ol>\n\n\n\n<p>if\u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6765\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5757\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">age = 18<br><br>if age &lt; 18:<br>    print(\"Sorry, you are too young.\")<br>elif age &gt;=18 and age &lt; 35:<br>    print(\"Welcome to the party!\")<br>else:<br>    print(\"Sorry, you are too old.\")<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5224\u65ad\u4e86\u4e00\u4e2a\u4eba\u7684\u5e74\u9f84\uff0c\u5982\u679c\u5c0f\u4e8e18\u5c81\u8f93\u51fa&#8221;Sorry, you are too young.&#8221;\uff0c\u5982\u679c\u5927\u4e8e\u7b49\u4e8e18\u5c0f\u4e8e35\u5c81\u8f93\u51fa&#8221;Welcome to the party!&#8221;\uff0c\u5426\u5219\u8f93\u51fa&#8221;Sorry, you are too old.&#8221;\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>for\u5faa\u73af\u8bed\u53e5<\/li>\n<\/ol>\n\n\n\n<p>for\u8bed\u53e5\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u6216\u5b57\u7b26\u4e32\uff09\uff0c\u5e76\u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\u5757\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fruits = ['apple', 'banana', 'orange']<br><br>for fruit in fruits:<br>    print(fruit)<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u904d\u5386\u4e86\u4e00\u4e2a\u540d\u4e3a<code>fruits<\/code>\u7684\u5217\u8868\u53d8\u91cf\uff0c\u4f7f\u7528for\u5faa\u73af\u8f93\u51fa\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>while\u5faa\u73af\u8bed\u53e5<\/li>\n<\/ol>\n\n\n\n<p>while\u8bed\u53e5\u7528\u4e8e\u5faa\u73af\u6267\u884c\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u624d\u505c\u6b62\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">count = 0<br><br>while count &lt; 3:<br>    print(\"Count:\", count)<br>    count += 1<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528while\u5faa\u73af\u8f93\u51fa\u4ece0\u52302\u7684\u6570\u5b57\u3002\u5f53<code>count<\/code>\u53d8\u91cf\u5c0f\u4e8e3\u65f6\uff0c\u4e0d\u65ad\u8f93\u51fa\u5f53\u524d\u8ba1\u6570\u5668\u7684\u503c\uff0c\u5e76\u5c06\u8ba1\u6570\u5668\u52a01\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u662fPython\u4e2d\u51e0\u4e2a\u5e38\u89c1\u7684\u63a7\u5236\u8bed\u53e5\u7684\u7528\u6cd5\u793a\u4f8b\u3002\u638c\u63e1\u8fd9\u4e9b\u8bed\u53e5\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6570<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528def\u8bed\u53e5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5b9a\u4e49\u4e00\u4e2a\u8ba1\u7b97\u5706\u9762\u79ef\u7684\u51fd\u6570<br>def calculate_area(radius):<br>    # \u5f15\u7528 math \u5e93\u4e2d\u7684 pi \u5e38\u91cf<br>    global math<br>    import math<br>    <br>    return math.pi * radius ** 2<br><br># \u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u5176\u4ed6\u51fd\u6570<br>def calculate_volume(radius, height):<br>    area = calculate_area(radius)<br>    return area * height<br><br># \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf<br>PI = 3.1415926<br><br>def calculate_circle_area(radius):<br>    global PI<br>    <br>    return PI * radius ** 2<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e09\u4e2a\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570<code>calculate_area<\/code>\u8ba1\u7b97\u7ed9\u5b9a\u534a\u5f84\u7684\u5706\u9762\u79ef\uff0c\u5f15\u7528\u4e86Python\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u4e86\u5168\u5c40\u53d8\u91cf<code>math.pi<\/code>\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570<code>calculate_volume<\/code>\u8c03\u7528\u4e86\u51fd\u6570<code>calculate_area<\/code>\u6765\u8ba1\u7b97\u5706\u67f1\u4f53\u7684\u4f53\u79ef\u3002\u7b2c\u4e09\u4e2a\u51fd\u6570<code>calculate_circle_area<\/code>\u8ba1\u7b97\u7ed9\u5b9a\u534a\u5f84\u7684\u5706\u9762\u79ef\uff0c\u5f15\u7528\u4e86\u5168\u5c40\u53d8\u91cf<code>PI<\/code>\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u975e\u5e38\u7075\u6d3b\u7684\uff0c\u53ef\u4ee5\u81ea\u7531\u5730\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5176\u4ed6\u51fd\u6570\u3001\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u3001\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u7b49\u3002\u8fd9\u4f7f\u5f97Python\u6210\u4e3a\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u80fd\u591f\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u5757<\/h2>\n\n\n\n<p>Python\u4e2d\u7684\u6a21\u5757\u53ef\u7531\u81ea\u5df1\u7f16\u5199\uff0c\u4e5f\u53ef\u4ee5\u5916\u90e8\u5f15\u5165<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6a21\u5757\u662f\u4e00\u79cd\u7ec4\u7ec7Python\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u4ee3\u7801\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5f15\u7528\u8fd9\u4e2a\u6587\u4ef6\u3002Python\u4e2d\u7684\u6a21\u5757\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u7531\u81ea\u5df1\u7f16\u5199\uff0c\u4e5f\u53ef\u4ee5\u4ece\u5916\u90e8\u5f15\u5165\u3002\u4ee5\u4e0b\u662fPython\u4e2d\u6a21\u5757\u7684\u51e0\u4e2a\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u81ea\u5df1\u7f16\u5199\u6a21\u5757<\/li>\n<\/ol>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a.py\u6587\u4ef6\u4e2d\u7f16\u5199\u81ea\u5df1\u7684\u6a21\u5757\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a my_module \u7684\u6a21\u5757<br><br># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<br>def hello_world():<br>    print(\"Hello, world!\")<br><br># \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf<br>my_var = 42<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_module<\/code>\u7684\u6a21\u5757\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a<code>hello_world<\/code>\u51fd\u6570\u548c\u4e00\u4e2a<code>my_var<\/code>\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u6765\u4ece\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import my_module<br><br>my_module.hello_world()<br>print(my_module.my_var)<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u4ece\u6807\u51c6\u5e93\u4e2d\u5f15\u5165\u6a21\u5757<\/li>\n<\/ol>\n\n\n\n<p>Python\u6807\u51c6\u5e93\u5305\u542b\u4e86\u5f88\u591a\u6709\u7528\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u4e0d\u5b89\u88c5\u4efb\u4f55\u5176\u4ed6\u8f6f\u4ef6\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff0c<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u6765\u5f15\u5165<code>math<\/code>\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import math<br><br>print(math.sqrt(2))<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u4e86<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u6765\u8ba1\u7b972\u7684\u5e73\u65b9\u6839\u503c\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/li>\n<\/ol>\n\n\n\n<p>\u9664\u4e86Python\u6807\u51c6\u5e93\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9Python\u5e93\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u7b49\u5305\u7ba1\u7406\u5de5\u5177\u6765\u5b89\u88c5\u3002\u4f8b\u5982\uff0c<code>numpy<\/code>\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6570\u5b66\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u6765\u5b89\u88c5\u5e76\u5f15\u5165\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!pip install numpy  # \u5b89\u88c5 numpy \u5e93<br><br>import numpy as np<br><br>print(np.array([1, 2, 3]))<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u4e86<code>numpy<\/code>\u5e93\u4e2d\u7684<code>array<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u3002<\/p>\n\n\n\n<p>\u603b\u4e4b\uff0c\u5728Python\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528\u6a21\u5757\u975e\u5e38\u65b9\u4fbf\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f02\u5e38\u5904\u7406<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53ca\u65f6\u6355\u83b7\u9519\u8bef\u4fe1\u606f\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u589e\u5f3a\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try:<br>    num1 = int(input(\"\u8bf7\u8f93\u5165\u88ab\u9664\u6570\uff1a\"))<br>    num2 = int(input(\"\u8bf7\u8f93\u5165\u9664\u6570\uff1a\"))<br>    result = num1 \/ num2<br>    print(\"\u7ed3\u679c\u4e3a\uff1a\", result)<br>except ValueError:<br>    print(\"\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002\")<br>except ZeroDivisionError:<br>    print(\"\u9664\u6570\u4e0d\u80fd\u4e3a 0\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002\")<br>except:<br>    print(\"\u53d1\u751f\u4e86\u672a\u77e5\u5f02\u5e38\uff0c\u8bf7\u68c0\u67e5\u4ee3\u7801\u3002\")<br>finally:<br>    print(\"\u7a0b\u5e8f\u7ed3\u675f\u3002\")<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5bf9\u7528\u6237\u8f93\u5165\u7684\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\uff0c\u6216\u8005\u9664\u6570\u4e3a0\uff0c\u6216\u8005\u53d1\u751f\u672a\u77e5\u7684\u5f02\u5e38\uff0c\u90fd\u4f1a\u88ab\u6355\u83b7\u5e76\u4f5c\u51fa\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>try<\/code>\u8bed\u53e5\u5757\u4e2d\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u3002\u5982\u679c\u8fd9\u4e9b\u4ee3\u7801\u5f15\u53d1\u4e86\u5f02\u5e38\uff0c\u5219\u4f1a\u8df3\u8f6c\u5230\u76f8\u5e94\u7684<code>except<\/code>\u5904\u7406\u5757\u4e2d\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\u3002\u6700\u540e\uff0c\u65e0\u8bba\u53d1\u751f\u4e86\u4ec0\u4e48\u5f02\u5e38\uff0c<code>finally<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u4e00\u5b9a\u4f1a\u88ab\u6267\u884c\uff0c\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u91ca\u653e\u8d44\u6e90\u7b49\u3002<\/p>\n\n\n\n<p>Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u5982\u679c\u6211\u4eec\u7f16\u5199\u7684\u7a0b\u5e8f\u9700\u8981\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u6216\u8005\u8fdb\u884c\u590d\u6742\u7684\u8fd0\u7b97\uff0c\u90a3\u4e48\u5408\u7406\u5730\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6587\u4ef6\u64cd\u4f5c<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6<br>with open(\"file.txt\", \"r\") as f:<br> &nbsp; &nbsp;# \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<br> &nbsp; &nbsp;content = f.read()<br>\u200b<br># \u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<br>print(content)<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u540d\u4e3a<code>file.txt<\/code>\u7684\u6587\u672c\u6587\u4ef6\uff0c\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u8bfb\u53d6\u5176\u4e2d\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u5199\u5165\u6587\u672c\u6587\u4ef6<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<br>with open(\"file.txt\", \"w\") as f:<br> &nbsp; &nbsp;# \u5199\u5165\u6587\u4ef6\u5185\u5bb9<br> &nbsp; &nbsp;f.write(\"Hello, world!\")<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u540d\u4e3a<code>file.txt<\/code>\u7684\u6587\u672c\u6587\u4ef6\uff0c\u4ee5\u5199\u5165\u6a21\u5f0f\u5411\u5176\u4e2d\u5199\u5165\u5b57\u7b26\u4e32<code>\"Hello, world!\"<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u4ee5\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<br>with open(\"image.jpg\", \"rb\") as f:<br> &nbsp; &nbsp;# \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<br> &nbsp; &nbsp;content = f.read()<br>\u200b<br># \u8f93\u51fa\u6587\u4ef6\u5927\u5c0f<br>print(len(content))<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u540d\u4e3a<code>image.jpg<\/code>\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4ee5\u4e8c\u8fdb\u5236\u8bfb\u53d6\u6a21\u5f0f\u8bfb\u53d6\u5176\u4e2d\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5185\u5bb9\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u8f93\u51fa\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u4eceURL\u4e0b\u8f7d\u6587\u4ef6<br>import urllib.request<br>\u200b<br>url = \"https:\/\/www.python.org\/static\/img\/python-logo.png\"<br>urllib.request.urlretrieve(url, \"python-logo.png\")<br>\u200b<br># \u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4e0b\u8f7d\u7684\u6587\u4ef6\u5185\u5bb9<br>with open(\"python-logo.png\", \"rb\") as f:<br> &nbsp; &nbsp;with open(\"new-logo.png\", \"wb\") as new_file:<br> &nbsp; &nbsp; &nbsp; &nbsp;new_file.write(f.read())<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528<code>urllib<\/code>\u5e93\u4ece\u6307\u5b9a\u7684URL\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u4fdd\u5b58\u4e3a<code>python-logo.png<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u5206\u522b\u4ee5\u4e8c\u8fdb\u5236\u8bfb\/\u5199\u6a21\u5f0f\u6253\u5f00<code>python-logo.png<\/code>\u548c<code>new-logo.png<\/code>\u6587\u4ef6\uff0c\u5e76\u5c06\u524d\u8005\u7684\u5185\u5bb9\u5199\u5165\u540e\u8005\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u51e0\u4e2a\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u7684\u793a\u4f8b\u3002\u5728Python\u4e2d\uff0c\u6587\u4ef6\u7684\u64cd\u4f5c\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u5f0f\u8fdb\u884c\u8bfb\u5199\uff0c\u4ece\u800c\u5b9e\u73b0\u5404\u79cd\u4e0d\u540c\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\uff1a\u53d8\u91cf\u6570\u636e\u7c7b\u578b\u5217\u8868\u3001\u5b57\u5178\u4f7f\u7528\u4e3e\u4f8bpython\u8fd0\u7b97\u7b26\u4e3e\u4f8b <span class=\"readmore\"><a href=\"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3435\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3435","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3435"}],"version-history":[{"count":1,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3435\/revisions"}],"predecessor-version":[{"id":3436,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3435\/revisions\/3436"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}