{"id":966466,"date":"2024-12-27T04:47:52","date_gmt":"2024-12-26T20:47:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966466.html"},"modified":"2024-12-27T04:47:54","modified_gmt":"2024-12-26T20:47:54","slug":"python%e4%b8%adname%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/966466.html","title":{"rendered":"python\u4e2dname\u5982\u4f55\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182236\/09cf41f5-44b7-49ca-88f4-78bcee3a4c62.webp\" alt=\"python\u4e2dname\u5982\u4f55\u5b9a\u4e49\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u5b9a\u4e49\u540d\u79f0\uff08\u53d8\u91cf\u6216\u51fd\u6570\u540d\uff09\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u540d\u79f0\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3001\u540d\u79f0\u4e0d\u80fd\u662fPython\u7684\u5173\u952e\u5b57\u3001\u540d\u79f0\u5e94\u5c3d\u91cf\u6709\u610f\u4e49<\/strong>\u3002\u8be6\u7ec6\u6765\u8bf4\uff0c\u9996\u5148\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u662f\u7528\u6765\u5f15\u7528\u548c\u5b58\u50a8\u6570\u636e\u7684\u6807\u7b7e\u3002\u4e00\u4e2a\u597d\u7684\u53d8\u91cf\u540d\u4e0d\u4ec5\u80fd\u8ba9\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u8fd8\u80fd\u51cf\u5c11\u51fa\u9519\u7684\u53ef\u80fd\u6027\u3002\u5176\u6b21\uff0c\u51fd\u6570\u540d\u5219\u662f\u7528\u6765\u5b9a\u4e49\u53ef\u590d\u7528\u4ee3\u7801\u5757\u7684\u6807\u8bc6\u7b26\uff0c\u597d\u7684\u51fd\u6570\u540d\u5e94\u8be5\u80fd\u591f\u4f20\u8fbe\u51fd\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u6765\u8be6\u7ec6\u8ba8\u8bba\u4e00\u4e0b\u8fd9\u4e9b\u539f\u5219\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d8\u91cf\u540d\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5408\u9002\u7684\u5b57\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u53ef\u4ee5\u7531\u5b57\u6bcd\uff08a-z\uff0cA-Z\uff09\u3001\u6570\u5b57\uff080-9\uff09\u548c\u4e0b\u5212\u7ebf\uff08_\uff09\u7ec4\u6210\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u8fd9\u662f\u56e0\u4e3a\u4ee5\u6570\u5b57\u5f00\u5934\u4f1a\u9020\u6210\u6b67\u4e49\uff0cPython\u89e3\u91ca\u5668\u65e0\u6cd5\u533a\u5206\u8fd9\u662f\u4e00\u4e2a\u53d8\u91cf\u540d\u8fd8\u662f\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>variable1<\/code> \u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u53d8\u91cf\u540d\uff0c\u800c <code>1variable<\/code> \u5219\u4e0d\u662f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528Python\u5173\u952e\u5b57<\/strong><\/p>\n<\/p>\n<p><p>Python\u6709\u4e00\u4e9b\u4fdd\u7559\u5173\u952e\u5b57\uff0c\u4e0d\u80fd\u7528\u4f5c\u53d8\u91cf\u540d\uff0c\u4f8b\u5982 <code>if<\/code>\u3001<code>else<\/code>\u3001<code>while<\/code>\u3001<code>for<\/code> \u7b49\u3002\u8fd9\u4e9b\u5173\u952e\u5b57\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4f5c\u53d8\u91cf\u540d\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e0d\u80fd\u4f7f\u7528 <code>def<\/code> \u4f5c\u4e3a\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u5b83\u662f\u5b9a\u4e49\u51fd\u6570\u7684\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6709\u610f\u4e49\u7684\u540d\u79f0<\/strong><\/p>\n<\/p>\n<p><p>\u53d8\u91cf\u540d\u5e94\u8be5\u5c3d\u91cf\u6709\u610f\u4e49\uff0c\u80fd\u591f\u53cd\u6620\u5176\u6240\u5b58\u50a8\u7684\u6570\u636e\u7684\u6027\u8d28\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f7f\u5176\u4ed6\u7a0b\u5e8f\u5458\uff08\u5305\u62ec\u672a\u6765\u7684\u81ea\u5df1\uff09\u80fd\u591f\u66f4\u5bb9\u6613\u7406\u89e3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528 <code>count<\/code> \u6765\u8868\u793a\u8ba1\u6570\u5668\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u4e00\u4e2a\u6a21\u7cca\u7684\u540d\u79f0\u5982 <code>c<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u540d\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9075\u5faa\u547d\u540d\u89c4\u8303<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u540d\u901a\u5e38\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\uff0c\u5e76\u4e14\u5982\u679c\u6709\u591a\u4e2a\u5355\u8bcd\uff0c\u4f7f\u7528\u4e0b\u5212\u7ebf\u8fde\u63a5\u3002\u8fd9\u79cd\u547d\u540d\u65b9\u5f0f\u88ab\u79f0\u4e3a\u201csnake_case\u201d\uff0c\u5b83\u5728Python\u793e\u533a\u4e2d\u975e\u5e38\u6d41\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>calculate_sum<\/code> \u662f\u4e00\u4e2a\u5408\u9002\u7684\u51fd\u6570\u540d\uff0c\u800c <code>calculateSum<\/code> \u5219\u662f\u53e6\u4e00\u79cd\u4e0d\u592a\u63a8\u8350\u7684\u547d\u540d\u65b9\u5f0f\uff08camelCase\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63cf\u8ff0\u51fd\u6570\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u540d\u5e94\u51c6\u786e\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\uff0c\u4fbf\u4e8e\u5176\u4ed6\u4eba\u7406\u89e3\u8be5\u51fd\u6570\u7684\u7528\u9014\u3002\u5728\u51fd\u6570\u540d\u4e2d\uff0c\u52a8\u8bcd\u901a\u5e38\u7528\u4e8e\u63cf\u8ff0\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>get_user_input<\/code> \u662f\u4e00\u4e2a\u80fd\u5f88\u597d\u5730\u63cf\u8ff0\u5176\u529f\u80fd\u7684\u51fd\u6570\u540d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u7b80\u6d01<\/strong><\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u51fd\u6570\u540d\u9700\u8981\u63cf\u8ff0\u529f\u80fd\uff0c\u4f46\u4e5f\u4e0d\u5e94\u8fc7\u4e8e\u5197\u957f\u3002\u8fc7\u957f\u7684\u540d\u79f0\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u3002\u4fdd\u6301\u7b80\u6d01\u800c\u4e0d\u5931\u51c6\u786e\u662f\u547d\u540d\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>calculate_total_price<\/code> \u6bd4 <code>calculate_total_price_of_items_in_cart<\/code> \u66f4\u4e3a\u7b80\u6d01\uff0c\u4f46\u4ecd\u7136\u51c6\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u547d\u540d\u7ea6\u5b9a\u7684\u5e94\u7528<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e38\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5e38\u91cf\u901a\u5e38\u7528\u5168\u5927\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u547d\u540d\u3002\u8fd9\u79cd\u7ea6\u5b9a\u8ba9\u4eba\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u53d8\u91cf\u662f\u4e00\u4e2a\u5e38\u91cf\u503c\uff0c\u901a\u5e38\u4e0d\u5e94\u8be5\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>PI = 3.14159<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7c7b\u540d<\/strong><\/p>\n<\/p>\n<p><p>\u7c7b\u540d\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684CamelCase\u547d\u540d\u7ea6\u5b9a\u3002\u8fd9\u4f7f\u5f97\u7c7b\u540d\u5728\u4ee3\u7801\u4e2d\u663e\u5f97\u4e0e\u4f17\u4e0d\u540c\uff0c\u5e76\u4e14\u7b26\u5408Python\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>class MyClass:<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u540d<\/strong><\/p>\n<\/p>\n<p><p>\u6a21\u5757\u540d\u901a\u5e38\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u3002\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u4e0e\u5176\u4ed6\u547d\u540d\u98ce\u683c\u533a\u5206\u5f00\u6765\uff0c\u5e76\u7b26\u5408PEP 8\u7684\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6587\u4ef6\u540d <code>my_module.py<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5b9e\u8df5\u4e2d\u7684\u547d\u540d\u6280\u5de7<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u907f\u514d\u4f7f\u7528\u5355\u4e2a\u5b57\u6bcd<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u975e\u5728\u5faa\u73af\u6216\u4e34\u65f6\u53d8\u91cf\u4e2d\u660e\u786e\u4e14\u5e38\u7528\u7684\u60c5\u51b5\uff08\u5982 <code>i<\/code>\u3001<code>j<\/code>\uff09\uff0c\u5426\u5219\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5355\u5b57\u6bcd\u53d8\u91cf\u540d\u3002\u5355\u5b57\u6bcd\u53d8\u91cf\u540d\u901a\u5e38\u4e0d\u5177\u5907\u63cf\u8ff0\u6027\uff0c\u5bb9\u6613\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8003\u8651\u7f29\u5199\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7f29\u5199\u53ef\u4ee5\u51cf\u5c11\u540d\u79f0\u7684\u957f\u5ea6\uff0c\u4f46\u8981\u786e\u4fdd\u7f29\u5199\u662f\u666e\u904d\u8ba4\u77e5\u7684\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u4e0d\u5e38\u89c1\u7684\u7f29\u5199\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u4e00\u81f4\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u4fdd\u6301\u4e00\u81f4\u7684\u547d\u540d\u98ce\u683c\u5f88\u91cd\u8981\u3002\u65e0\u8bba\u662f\u53d8\u91cf\u540d\u8fd8\u662f\u51fd\u6570\u540d\uff0c\u7edf\u4e00\u7684\u98ce\u683c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5177\u7ed3\u6784\u6027\uff0c\u66f4\u6613\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u539f\u5219\u548c\u5efa\u8bae\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9a\u4e49\u51fa\u6e05\u6670\u3001\u6709\u610f\u4e49\u7684\u540d\u79f0\uff0c\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u81ea\u5df1\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\uff0c\u4e5f\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\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\u53d8\u91cf\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u7684\u5b9a\u4e49\u9075\u5faa\u4e00\u4e9b\u57fa\u672c\u89c4\u5219\u3002\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\uff08A-Z\uff0ca-z\uff09\u6216\u4e0b\u5212\u7ebf\uff08_\uff09\u5f00\u5934\uff0c\u540e\u9762\u53ef\u4ee5\u8ddf\u5b57\u6bcd\u3001\u6570\u5b57\uff080-9\uff09\u6216\u4e0b\u5212\u7ebf\u3002\u53d8\u91cf\u540d\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u56e0\u6b64<code>Variable<\/code>\u548c<code>variable<\/code>\u662f\u4e0d\u540c\u7684\u3002\u6b64\u5916\uff0c\u907f\u514d\u4f7f\u7528Python\u7684\u4fdd\u7559\u5b57\uff08\u5982<code>if<\/code>\u3001<code>else<\/code>\u3001<code>for<\/code>\u7b49\uff09\u4f5c\u4e3a\u53d8\u91cf\u540d\uff0c\u4ee5\u514d\u5f15\u8d77\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u4e2d\u53d8\u91cf\u540d\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5b9a\u4e49\u53d8\u91cf\u540d\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u6709\u610f\u4e49\u7684\u540d\u79f0\uff0c\u4ee5\u4fbf\u4e8e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>total_price<\/code>\u800c\u4e0d\u662f<code>tp<\/code>\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u8868\u660e\u8fd9\u4e2a\u53d8\u91cf\u7684\u7528\u9014\u3002\u540c\u65f6\uff0c\u9075\u5faa\u547d\u540d\u7ea6\u5b9a\uff0c\u5982\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u591a\u4e2a\u5355\u8bcd\uff08snake_case\uff09\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5Python\u4e2d\u7684\u53d8\u91cf\u540d\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u5f53\u524d\u4f5c\u7528\u57df\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\u540d\u3002\u82e5\u8981\u68c0\u67e5\u67d0\u4e2a\u7279\u5b9a\u7684\u53d8\u91cf\u540d\u662f\u5426\u6709\u6548\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u8d4b\u503c\u5e76\u8bbf\u95ee\u8be5\u53d8\u91cf\u6765\u6d4b\u8bd5\u3002\u5982\u679c\u8bbf\u95ee\u65f6\u51fa\u73b0<code>NameError<\/code>\uff0c\u5219\u8bf4\u660e\u8be5\u53d8\u91cf\u672a\u5b9a\u4e49\u3002\u4f7f\u7528IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7684\u8bed\u6cd5\u9ad8\u4eae\u529f\u80fd\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6709\u6548\u7684\u53d8\u91cf\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u540d\u79f0\uff08\u53d8\u91cf\u6216\u51fd\u6570\u540d\uff09\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u540d\u79f0\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3001\u540d\u79f0\u4e0d\u80fd\u662fP [&hellip;]","protected":false},"author":3,"featured_media":966475,"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\/966466"}],"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=966466"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966466\/revisions"}],"predecessor-version":[{"id":966478,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966466\/revisions\/966478"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966475"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}