{"id":1069751,"date":"2025-01-08T10:51:51","date_gmt":"2025-01-08T02:51:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069751.html"},"modified":"2025-01-08T10:52:06","modified_gmt":"2025-01-08T02:52:06","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e8%be%be%e4%b8%80%e4%b8%aa%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069751.html","title":{"rendered":"python\u5982\u4f55\u8868\u8fbe\u4e00\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100648\/f18d1e61-ee73-4bf1-8ebf-b2d9eb215fb0.webp\" alt=\"python\u5982\u4f55\u8868\u8fbe\u4e00\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>Python\u8868\u8fbe\u4e00\u4e2a\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u590d\u6570\u548c\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u7b49\u4e0d\u540c\u8fdb\u5236\u8868\u793a\u6cd5<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<strong>\u6574\u6570\uff08int\uff09\u662f\u6700\u5e38\u89c1\u7684\u6570\u503c\u7c7b\u578b\uff0c\u6d6e\u70b9\u6570\uff08float\uff09\u7528\u4e8e\u8868\u793a\u5c0f\u6570\uff0c\u590d\u6570\uff08complex\uff09\u7528\u4e8e\u8868\u793a\u6570\u5b66\u4e2d\u7684\u590d\u6570\uff0c\u800c\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u8868\u793a\u6cd5\u5219\u7528\u4e8e\u7279\u5b9a\u7684\u8ba1\u7b97\u573a\u666f<\/strong>\u3002\u4f8b\u5982\uff0c\u6574\u6570\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\uff0c\u5982<code>42<\/code>\uff0c\u6d6e\u70b9\u6570\u5219\u9700\u8981\u5305\u542b\u5c0f\u6570\u70b9\uff0c\u5982<code>42.0<\/code>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u6574\u6570\uff08int\uff09<\/strong>\uff1a\u6574\u6570\u7c7b\u578b\u5728Python\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u4e3b\u8981\u7528\u4e8e\u8868\u793a\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u503c\u3002Python\u5141\u8bb8\u76f4\u63a5\u5199\u51fa\u6574\u6570\uff0c\u5982<code>42<\/code>\uff0c\u5e76\u4e14\u80fd\u591f\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u503c\u800c\u4e0d\u4f1a\u6ea2\u51fa\u3002\u6574\u6570\u5728Python\u4e2d\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u503c\u5c31\u4e0d\u80fd\u6539\u53d8\u3002\u6574\u6570\u7c7b\u578b\u7684\u53d8\u91cf\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u7b97\u672f\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6574\u6570\uff08int\uff09<\/h3>\n<\/p>\n<p><p>\u6574\u6570\u7c7b\u578b\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u6570\u503c\u7c7b\u578b\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u8868\u793a\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\u7684\u6570\u503c\u3002Python\u4e2d\u7684\u6574\u6570\u7c7b\u578b\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u5927\u5c0f\u7684\u6574\u6570\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u5927\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u6574\u6570\u7684\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6574\u6570\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = -5<\/p>\n<p>c = 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u53d8\u91cf\u5206\u522b\u8868\u793a\u6b63\u6574\u6570\u3001\u8d1f\u6574\u6570\u548c\u96f6\u3002Python\u4f1a\u81ea\u52a8\u63a8\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u65e0\u9700\u663e\u5f0f\u58f0\u660e\u3002<\/p>\n<\/p>\n<p><h4>\u6574\u6570\u7684\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u6574\u6570\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u7b97\u672f\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3001\u53d6\u4f59\u3001\u5e42\u8fd0\u7b97\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>y = 3<\/p>\n<p>sum_result = x + y        # \u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a13<\/p>\n<p>sub_result = x - y        # \u51cf\u6cd5\uff0c\u7ed3\u679c\u4e3a7<\/p>\n<p>mul_result = x * y        # \u4e58\u6cd5\uff0c\u7ed3\u679c\u4e3a30<\/p>\n<p>div_result = x \/ y        # \u9664\u6cd5\uff0c\u7ed3\u679c\u4e3a3.3333333333333335<\/p>\n<p>mod_result = x % y        # \u53d6\u4f59\uff0c\u7ed3\u679c\u4e3a1<\/p>\n<p>pow_result = x  y       # \u5e42\u8fd0\u7b97\uff0c\u7ed3\u679c\u4e3a1000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9664\u6cd5\u64cd\u4f5c\u5728Python 3\u4e2d\u9ed8\u8ba4\u8fd4\u56de\u6d6e\u70b9\u6570\u3002\u5982\u679c\u9700\u8981\u5f97\u5230\u6574\u6570\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5730\u677f\u9664\uff08<code>\/\/<\/code>\uff09\u8fd0\u7b97\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">floor_div_result = x \/\/ y  # \u5730\u677f\u9664\uff0c\u7ed3\u679c\u4e3a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6d6e\u70b9\u6570\uff08float\uff09<\/h3>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7528\u4e8e\u8868\u793a\u5305\u542b\u5c0f\u6570\u90e8\u5206\u7684\u6570\u503c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8868\u793a\u7cbe\u786e\u5ea6\u8f83\u9ad8\u7684\u6570\u503c\u60c5\u51b5\uff0c\u4f8b\u5982\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u6d6e\u70b9\u6570\u7684\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6d6e\u70b9\u6570\u5305\u542b\u5c0f\u6570\u70b9\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10.5<\/p>\n<p>b = -3.14<\/p>\n<p>c = 0.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Python\u4f1a\u81ea\u52a8\u5c06\u8fd9\u4e9b\u6570\u503c\u8bc6\u522b\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u7b97\u672f\u8fd0\u7b97\uff0c\u4e0e\u6574\u6570\u7c7b\u578b\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5.5<\/p>\n<p>y = 2.2<\/p>\n<p>sum_result = x + y        # \u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a7.7<\/p>\n<p>sub_result = x - y        # \u51cf\u6cd5\uff0c\u7ed3\u679c\u4e3a3.3<\/p>\n<p>mul_result = x * y        # \u4e58\u6cd5\uff0c\u7ed3\u679c\u4e3a12.1<\/p>\n<p>div_result = x \/ y        # \u9664\u6cd5\uff0c\u7ed3\u679c\u4e3a2.5<\/p>\n<p>mod_result = x % y        # \u53d6\u4f59\uff0c\u7ed3\u679c\u4e3a1.1<\/p>\n<p>pow_result = x  y       # \u5e42\u8fd0\u7b97\uff0c\u7ed3\u679c\u4e3a42.745491628777316<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u7684\u9664\u6cd5\u64cd\u4f5c\u9ed8\u8ba4\u8fd4\u56de\u6d6e\u70b9\u6570\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u590d\u6570\uff08complex\uff09<\/h3>\n<\/p>\n<p><p>\u590d\u6570\u7528\u4e8e\u8868\u793a\u6570\u5b66\u4e2d\u7684\u590d\u6570\uff0c\u5305\u542b\u5b9e\u90e8\u548c\u865a\u90e8\uff0c\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u8ba1\u7b97\u4e2d\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u590d\u6570\u7684\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u590d\u6570\u7528<code>j<\/code>\u8868\u793a\u865a\u90e8\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 2 + 3j<\/p>\n<p>b = -1 - 1j<\/p>\n<p>c = 0 + 0j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u5747\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u590d\u6570\u7684\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u590d\u6570\u53ef\u4ee5\u53c2\u4e0e\u5404\u79cd\u7b97\u672f\u8fd0\u7b97\uff0c\u4e0e\u5b9e\u6570\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 1 + 2j<\/p>\n<p>y = 3 + 4j<\/p>\n<p>sum_result = x + y        # \u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a(4+6j)<\/p>\n<p>sub_result = x - y        # \u51cf\u6cd5\uff0c\u7ed3\u679c\u4e3a(-2-2j)<\/p>\n<p>mul_result = x * y        # \u4e58\u6cd5\uff0c\u7ed3\u679c\u4e3a(-5+10j)<\/p>\n<p>div_result = x \/ y        # \u9664\u6cd5\uff0c\u7ed3\u679c\u4e3a(0.44+0.08j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u53ef\u4ee5\u5206\u522b\u8bbf\u95ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">real_part = x.real        # \u5b9e\u90e8\uff0c\u7ed3\u679c\u4e3a1.0<\/p>\n<p>imag_part = x.imag        # \u865a\u90e8\uff0c\u7ed3\u679c\u4e3a2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0d\u540c\u8fdb\u5236\u8868\u793a\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u548c\u5341\u516d\u8fdb\u5236\u7684\u6570\u503c\u8868\u793a\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u8ba1\u7b97\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u8fdb\u5236\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u6570\u503c\u4ee5<code>0b<\/code>\u6216<code>0B<\/code>\u5f00\u5934\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_num = 0b1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u516b\u8fdb\u5236\u6570\u503c\u4ee5<code>0o<\/code>\u6216<code>0O<\/code>\u5f00\u5934\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_num = 0o12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5341\u516d\u8fdb\u5236\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u6570\u503c\u4ee5<code>0x<\/code>\u6216<code>0X<\/code>\u5f00\u5934\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_num = 0xA<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u8fdb\u5236\u8868\u793a\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 10<\/p>\n<p>binary_str = bin(num)     # \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\uff0c\u7ed3\u679c\u4e3a&#39;0b1010&#39;<\/p>\n<p>octal_str = oct(num)      # \u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\uff0c\u7ed3\u679c\u4e3a&#39;0o12&#39;<\/p>\n<p>hex_str = hex(num)        # \u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\uff0c\u7ed3\u679c\u4e3a&#39;0xA&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u503c\u7c7b\u578b\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u51fd\u6570\u6765\u8fdb\u884c\u4e0d\u540c\u6570\u503c\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10          # \u6574\u6570<\/p>\n<p>b = 3.14        # \u6d6e\u70b9\u6570<\/p>\n<p>c = 2 + 3j      # \u590d\u6570<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>float_a = float(a)       # \u7ed3\u679c\u4e3a10.0<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>int_b = int(b)           # \u7ed3\u679c\u4e3a3<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u590d\u6570<\/strong><\/h2>\n<p>complex_a = complex(a)   # \u7ed3\u679c\u4e3a(10+0j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u8f6c\u6362\u51fd\u6570\u53ef\u4ee5\u5728\u4e0d\u540c\u6570\u503c\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u8ba1\u7b97\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6570\u503c\u7c7b\u578b\u7684\u6bd4\u8f83\u4e0e\u5224\u65ad<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8fd0\u7b97\u7b26\u548c\u5185\u7f6e\u51fd\u6570\u6765\u6bd4\u8f83\u548c\u5224\u65ad\u6570\u503c\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3.14<\/p>\n<h2><strong>\u6bd4\u8f83\u8fd0\u7b97<\/strong><\/h2>\n<p>equal = (a == b)         # \u5224\u65ad\u662f\u5426\u76f8\u7b49\uff0c\u7ed3\u679c\u4e3aFalse<\/p>\n<p>not_equal = (a != b)     # \u5224\u65ad\u662f\u5426\u4e0d\u76f8\u7b49\uff0c\u7ed3\u679c\u4e3aTrue<\/p>\n<p>greater = (a &gt; b)        # \u5224\u65ad\u662f\u5426\u5927\u4e8e\uff0c\u7ed3\u679c\u4e3aTrue<\/p>\n<p>less = (a &lt; b)           # \u5224\u65ad\u662f\u5426\u5c0f\u4e8e\uff0c\u7ed3\u679c\u4e3aFalse<\/p>\n<h2><strong>\u5224\u65ad\u7c7b\u578b<\/strong><\/h2>\n<p>is_int = isinstance(a, int)      # \u5224\u65ad\u662f\u5426\u4e3a\u6574\u6570\uff0c\u7ed3\u679c\u4e3aTrue<\/p>\n<p>is_float = isinstance(b, float)  # \u5224\u65ad\u662f\u5426\u4e3a\u6d6e\u70b9\u6570\uff0c\u7ed3\u679c\u4e3aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u548c\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8fdb\u884c\u6570\u503c\u6bd4\u8f83\u548c\u7c7b\u578b\u5224\u65ad\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6570\u503c\u7684\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6570\u503c\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u975e\u5e38\u91cd\u8981\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u652f\u6301\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0<code>f<\/code>\u6765\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>formatted_str = f&quot;a = {a}, b = {b}&quot;<\/p>\n<p>print(formatted_str)  # \u8f93\u51fa\uff1aa = 10, b = 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>formatted_str = &quot;a = {}, b = {:.2f}&quot;.format(a, b)<\/p>\n<p>print(formatted_str)  # \u8f93\u51fa\uff1aa = 10, b = 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><p><code>%<\/code>\u64cd\u4f5c\u7b26\u662f\u65e9\u671f\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u683c\u5f0f\u5316\u9700\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>formatted_str = &quot;a = %d, b = %.2f&quot; % (a, b)<\/p>\n<p>print(formatted_str)  # \u8f93\u51fa\uff1aa = 10, b = 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6570\u503c\u7684\u5e38\u7528\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u6570\u503c\u7c7b\u578b\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u5f00\u53d1\u8005\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4><code>abs<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>abs<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u7edd\u5bf9\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = -10<\/p>\n<p>abs_val = abs(a)  # \u7ed3\u679c\u4e3a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><code>round<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>round<\/code>\u51fd\u6570\u7528\u4e8e\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">b = 3.14159<\/p>\n<p>rounded_val = round(b, 2)  # \u7ed3\u679c\u4e3a3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><code>max<\/code>\u548c<code>min<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>max<\/code>\u548c<code>min<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3.14<\/p>\n<p>c = 7<\/p>\n<p>max_val = max(a, b, c)  # \u7ed3\u679c\u4e3a10<\/p>\n<p>min_val = min(a, b, c)  # \u7ed3\u679c\u4e3a3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><code>sum<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sum<\/code>\u51fd\u6570\u7528\u4e8e\u6c42\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>total = sum(numbers)  # \u7ed3\u679c\u4e3a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6570\u503c\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6570\u503c\u7c7b\u578b\u7684\u9ad8\u7ea7\u7528\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u89e3\u51b3\u66f4\u590d\u6742\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>math<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>a = 9<\/p>\n<p>sqrt_val = math.sqrt(a)     # \u6c42\u5e73\u65b9\u6839\uff0c\u7ed3\u679c\u4e3a3.0<\/p>\n<p>log_val = math.log(a)       # \u6c42\u81ea\u7136\u5bf9\u6570\uff0c\u7ed3\u679c\u4e3a2.1972245773362196<\/p>\n<p>factorial_val = math.factorial(a)  # \u6c42\u9636\u4e58\uff0c\u7ed3\u679c\u4e3a362880<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u7cbe\u5ea6\u7684\u5341\u8fdb\u5236\u5b9a\u70b9\u548c\u6d6e\u70b9\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, getcontext<\/p>\n<p>getcontext().prec = 6  # \u8bbe\u7f6e\u7cbe\u5ea6\u4e3a6<\/p>\n<p>a = Decimal(&#39;3.14159265358979323846&#39;)<\/p>\n<p>b = Decimal(&#39;2.71828182845904523536&#39;)<\/p>\n<p>sum_val = a + b  # \u9ad8\u7cbe\u5ea6\u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a5.85987<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>fractions<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5206\u6570\u7684\u8868\u793a\u548c\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<p>a = Fraction(1, 3)<\/p>\n<p>b = Fraction(2, 3)<\/p>\n<p>sum_val = a + b  # \u5206\u6570\u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u6570\u503c\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u6570\u503c\u7c7b\u578b\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u6570\u503c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662f\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4, 5, 6])<\/p>\n<p>sum_val = a + b  # \u6570\u7ec4\u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a[5 7 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>pandas<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662f\u6570\u636e\u5206\u6790\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>sum_val = df[&#39;A&#39;] + df[&#39;B&#39;]  # \u5217\u52a0\u6cd5\uff0c\u7ed3\u679c\u4e3a0    5<\/p>\n<p>                             # 1    7<\/p>\n<p>                             # 2    9<\/p>\n<p>                             # dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u8fbe\u548c\u5904\u7406\u6570\u503c\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u590d\u6570\u548c\u4e0d\u540c\u8fdb\u5236\u8868\u793a\u6cd5\u3002\u8fd9\u4e9b\u6570\u503c\u7c7b\u578b\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u8ba1\u7b97\u573a\u666f\u7684\u9700\u6c42\u3002\u901a\u8fc7\u5bf9\u6570\u503c\u7c7b\u578b\u7684\u6df1\u5165\u7406\u89e3\u548c\u7075\u6d3b\u8fd0\u7528\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u65e0\u8bba\u662f\u57fa\u7840\u7684\u7b97\u672f\u8fd0\u7b97\uff0c\u8fd8\u662f\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8868\u793a\u4e00\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8f93\u5165\u6570\u503c\u6765\u8868\u793a\u3002\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\uff08\u5982<code>5<\/code>\uff09\u3001\u6d6e\u70b9\u6570\uff08\u5982<code>3.14<\/code>\uff09\u6216\u590d\u6570\uff08\u5982<code>2 + 3j<\/code>\uff09\u3002\u8fd9\u4f7f\u5f97Python\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u975e\u5e38\u7075\u6d3b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165<code>x = 10<\/code>\u6765\u8868\u793a\u6574\u657010\uff0c\u6216\u8005<code>y = 2.5<\/code>\u6765\u8868\u793a\u6d6e\u70b9\u65702.5\u3002<\/p>\n<p><strong>Python\u4e2d\u6570\u5b57\u7684\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u5b57\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\uff08<code>int<\/code>\uff09\u3001\u6d6e\u70b9\u6570\uff08<code>float<\/code>\uff09\u548c\u590d\u6570\uff08<code>complex<\/code>\uff09\u3002\u6574\u6570\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u6d6e\u70b9\u6570\u53ef\u4ee5\u8868\u793a\u5c0f\u6570\uff0c\u800c\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code>\u6765\u68c0\u67e5\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982<code>print(type(x))<\/code>\u53ef\u4ee5\u544a\u8bc9\u60a8\u53d8\u91cf<code>x<\/code>\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u5b57\u7684\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u57fa\u672c\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u8fdb\u884c\u6570\u5b57\u8ba1\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\uff08<code>+<\/code>\uff09\u3001\u51cf\u6cd5\uff08<code>-<\/code>\uff09\u3001\u4e58\u6cd5\uff08<code>*<\/code>\uff09\u548c\u9664\u6cd5\uff08<code>\/<\/code>\uff09\u3002\u4f8b\u5982\uff0c<code>result = a + b<\/code>\u5c06\u8ba1\u7b97\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u548c\u3002\u6b64\u5916\uff0cPython\u8fd8\u652f\u6301\u53d6\u4f59\uff08<code>%<\/code>\uff09\u3001\u5e42\u8fd0\u7b97\uff08<code>**<\/code>\uff09\u7b49\u9ad8\u7ea7\u8fd0\u7b97\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8868\u8fbe\u4e00\u4e2a\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u590d\u6570\u548c\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236\u7b49\u4e0d\u540c\u8fdb\u5236\u8868\u793a\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6ee1\u8db3 [&hellip;]","protected":false},"author":3,"featured_media":1069780,"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\/1069751"}],"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=1069751"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069751\/revisions"}],"predecessor-version":[{"id":1069783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069751\/revisions\/1069783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069780"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}