{"id":48,"date":"2026-06-13T16:25:57","date_gmt":"2026-06-13T08:25:57","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=48"},"modified":"2026-06-13T16:25:57","modified_gmt":"2026-06-13T08:25:57","slug":"python3-%e6%95%b0%e5%ad%97number","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/13\/python3-%e6%95%b0%e5%ad%97number\/","title":{"rendered":"Python3 \u6570\u5b57(Number)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c\u6570\u5b57\uff08Number\uff09\u662f\u4e0d\u53ef\u53d8\u6570\u636e\u7c7b\u578b\u3002Python \u63d0\u4f9b\u4e86\u56db\u79cd\u6570\u5b57\u7c7b\u578b\uff0c\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u8ba1\u6570\u5230\u590d\u6742\u79d1\u5b66\u8ba1\u7b97\u7684\u65b9\u65b9\u9762\u9762\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f Python 3 \u6570\u5b57\u7c7b\u578b\u7684\u5168\u9762\u89e3\u6790\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6574\u6570 (<code>int<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<strong>\u6574\u6570\u7c7b\u578b\u53ea\u6709 <code>int<\/code><\/strong>\uff0c\u4e0d\u518d\u6709 Python 2 \u4e2d\u7684 <code>long<\/code> \u7c7b\u578b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6700\u5927\u7279\u70b9<\/strong>\uff1aPython 3 \u7684 <code>int<\/code> \u652f\u6301<strong>\u4efb\u610f\u7cbe\u5ea6<\/strong>\uff0c\u5b83\u7684\u5927\u5c0f\u4ec5\u53d7\u9650\u4e8e\u4f60\u8ba1\u7b97\u673a\u7684\u53ef\u7528\u5185\u5b58\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u8ba1\u7b97\u51e0\u767e\u4f4d\u7684\u8d85\u5927\u6574\u6570\u800c\u4e0d\u4f1a\u53d1\u751f\u6ea2\u51fa\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u57fa\u672c\u6574\u6570\na = 100\nb = -50\n\n# \u8d85\u5927\u6574\u6570 (\u81ea\u52a8\u5904\u7406\uff0c\u4e0d\u4f1a\u6ea2\u51fa)\nbig_num = 2 ** 100  \nprint(big_num)  # 1267650600228229401496703205376\n\n# \u4e0d\u540c\u8fdb\u5236\u7684\u8868\u793a\u6cd5\nbin_num = 0b1010   # \u4e8c\u8fdb\u5236 (\u4ee5 0b \u5f00\u5934) -&gt; \u5341\u8fdb\u5236 10\noct_num = 0o12     # \u516b\u8fdb\u5236 (\u4ee5 0o \u5f00\u5934) -&gt; \u5341\u8fdb\u5236 10\nhex_num = 0xA      # \u5341\u516d\u8fdb\u5236 (\u4ee5 0x \u5f00\u5934) -&gt; \u5341\u8fdb\u5236 10\n\n# \u8fdb\u5236\u8f6c\u6362\u51fd\u6570\nprint(bin(10))     # &#039;0b1010&#039;\nprint(oct(10))     # &#039;0o12&#039;\nprint(hex(10))     # &#039;0xa&#039;<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6d6e\u70b9\u6570 (<code>float<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d6e\u70b9\u6570\u7528\u4e8e\u8868\u793a\u5e26\u6709\u5c0f\u6570\u70b9\u7684\u6570\u5b57\u3002\u5728 Python \u4e2d\uff0c\u5b83\u5e95\u5c42\u5bf9\u5e94 C \u8bed\u8a00\u7684\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff08double\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">f1 = 3.14\nf2 = -0.05\nf3 = 1.2e3   # \u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u7b49\u540c\u4e8e 1.2 * 10^3 = 1200.0<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u26a0\ufe0f \u6838\u5fc3\u907f\u5751\uff1a\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e\u8ba1\u7b97\u673a\u5e95\u5c42\u4f7f\u7528\u4e8c\u8fdb\u5236\u8868\u793a\u6d6e\u70b9\u6570\uff0c\u67d0\u4e9b\u5341\u8fdb\u5236\u5c0f\u6570\u65e0\u6cd5\u88ab\u7cbe\u786e\u7684\u4e8c\u8fdb\u5236\u5c0f\u6570\u8868\u793a\uff0c\u4ece\u800c\u5bfc\u81f4\u7cbe\u5ea6\u4e22\u5931\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">print(0.1 + 0.2)          # \u8f93\u51fa: 0.30000000000000004 (\u4e0d\u662f\u7cbe\u786e\u7684 0.3\uff01)\nprint(0.1 + 0.2 == 0.3)   # \u8f93\u51fa: False\n\n# \u2705 \u6b63\u786e\u6bd4\u8f83\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 math.isclose()\nimport math\nprint(math.isclose(0.1 + 0.2, 0.3))  # \u8f93\u51fa: True\n\n# \u2705 \u9700\u8981\u7edd\u5bf9\u7cbe\u786e\u7684\u573a\u666f\uff08\u5982\u91d1\u878d\u8ba1\u7b97\uff09\uff1a\u4f7f\u7528 decimal \u6a21\u5757\nfrom decimal import Decimal\nd1 = Decimal(&#039;0.1&#039;)  # \u6ce8\u610f\uff1a\u4f20\u5165\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u6d6e\u70b9\u6570\nd2 = Decimal(&#039;0.2&#039;)\nprint(d1 + d2)       # \u8f93\u51fa: 0.3<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u7279\u6b8a\u6d6e\u70b9\u503c<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import math\ninf_val = float(&#039;inf&#039;)   # \u6b63\u65e0\u7a77\u5927\nneg_inf = float(&#039;-inf&#039;)  # \u8d1f\u65e0\u7a77\u5927\nnan_val = float(&#039;nan&#039;)   # Not a Number (\u975e\u6570\u5b57)\n\nprint(math.isinf(inf_val))  # True\nprint(math.isnan(nan_val))  # True<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u590d\u6570 (<code>complex<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u590d\u6570\u7531\u5b9e\u90e8\u548c\u865a\u90e8\u7ec4\u6210\uff0c\u5728 Python \u4e2d\u865a\u6570\u5355\u4f4d\u7528 <code>j<\/code> \u6216 <code>J<\/code> \u8868\u793a\uff08\u800c\u4e0d\u662f\u6570\u5b66\u4e2d\u7684 <code>i<\/code>\uff0c\u4ee5\u907f\u514d\u4e0e\u53d8\u91cf <code>i<\/code> \u6df7\u6dc6\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">c1 = 3 + 4j\nc2 = complex(3, 4)  # \u4f7f\u7528 complex() \u51fd\u6570\u521b\u5efa\n\n# \u83b7\u53d6\u5b9e\u90e8\u548c\u865a\u90e8\nprint(c1.real)  # 3.0 (\u6ce8\u610f\u8fd4\u56de\u7684\u662f\u6d6e\u70b9\u6570)\nprint(c1.imag)  # 4.0\n\n# \u83b7\u53d6\u5171\u8f6d\u590d\u6570 (\u5b9e\u90e8\u76f8\u540c\uff0c\u865a\u90e8\u7b26\u53f7\u76f8\u53cd)\nprint(c1.conjugate())  # (3-4j)\n\n# \u590d\u6570\u8fd0\u7b97\nc3 = (1 + 2j) + (3 + 4j)  # (4+6j)\nc4 = (1 + 2j) * (3 + 4j)  # (-5+10j)<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5e03\u5c14\u503c (<code>bool<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e03\u5c14\u503c\u662f\u6574\u6570\u7684<strong>\u5b50\u7c7b<\/strong>\u3002<code>True<\/code> \u7b49\u4ef7\u4e8e <code>1<\/code>\uff0c<code>False<\/code> \u7b49\u4ef7\u4e8e <code>0<\/code>\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">print(issubclass(bool, int))  # \u8f93\u51fa: True (bool \u786e\u5b9e\u662f int \u7684\u5b50\u7c7b)\n\nprint(True + True)   # \u8f93\u51fa: 2 (1 + 1)\nprint(True * 5)      # \u8f93\u51fa: 5 (1 * 5)\nprint(False + 10)    # \u8f93\u51fa: 10 (0 + 10)\n\n# \u5728\u7edf\u8ba1\u5217\u8868\u4e2d True \u7684\u4e2a\u6570\u65f6\u975e\u5e38\u6709\u7528\nmy_list = [True, False, True, True, False]\nprint(sum(my_list))  # \u8f93\u51fa: 3<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5e38\u7528\u5185\u7f6e\u6570\u5b66\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u65e0\u9700\u5bfc\u5165\u6a21\u5757\u5373\u53ef\u4f7f\u7528\u7684\u5185\u7f6e\u6570\u5b66\u51fd\u6570\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">x = -3.14159\n\nprint(abs(x))       # 3.14159 (\u7edd\u5bf9\u503c)\nprint(round(x, 2))  # -3.14 (\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e 2 \u4f4d)\nprint(round(x))     # -3 (\u56db\u820d\u4e94\u5165\u5230\u6574\u6570\u3002\u6ce8\u610f\uff1aPython 3 \u4f7f\u7528\u201c\u94f6\u884c\u5bb6\u820d\u5165\u6cd5\u201d\uff0c\u5373\u56db\u820d\u516d\u5165\u4e94\u6210\u53cc)\n\nprint(max(1, 5, 3)) # 5 (\u6700\u5927\u503c)\nprint(min(1, 5, 3)) # 1 (\u6700\u5c0f\u503c)\nprint(pow(2, 3))    # 8 (2 \u7684 3 \u6b21\u65b9\uff0c\u7b49\u540c\u4e8e 2 ** 3)\nprint(pow(2, 3, 3)) # 2 (2 \u7684 3 \u6b21\u65b9\uff0c\u518d\u5bf9 3 \u53d6\u6a21\uff0c\u5373 8 % 3)\n\n# divmod: \u540c\u65f6\u8fd4\u56de\u5546\u548c\u4f59\u6570\nquotient, remainder = divmod(10, 3)\nprint(quotient, remainder)  # 3 1<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <code>math<\/code> \u6a21\u5757 (\u9ad8\u7ea7\u6570\u5b66\u8fd0\u7b97)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\uff08\u5982\u4e09\u89d2\u51fd\u6570\u3001\u5bf9\u6570\u3001\u9636\u4e58\u7b49\uff09\uff0c\u9700\u8981\u5bfc\u5165\u5185\u7f6e\u7684 <code>math<\/code> \u6a21\u5757\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import math\n\n# \u5e38\u7528\u5e38\u91cf\nprint(math.pi)  # 3.141592653589793\nprint(math.e)   # 2.718281828459045\n\n# \u53d6\u6574\u51fd\u6570\nprint(math.ceil(3.1))   # 4 (\u5411\u4e0a\u53d6\u6574)\nprint(math.floor(3.9))  # 3 (\u5411\u4e0b\u53d6\u6574)\n\n# \u5176\u4ed6\u5e38\u7528\u51fd\u6570\nprint(math.sqrt(16))    # 4.0 (\u5e73\u65b9\u6839)\nprint(math.factorial(5))# 120 (\u9636\u4e58: 5!)\nprint(math.log(100, 10))# 2.0 (\u4ee5 10 \u4e3a\u5e95 100 \u7684\u5bf9\u6570)\nprint(math.sin(math.pi \/ 2)) # 1.0 (\u6b63\u5f26\u51fd\u6570\uff0c\u53c2\u6570\u662f\u5f27\u5ea6)<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u6570\u5b57\u7684\u683c\u5f0f\u5316\u8f93\u51fa (f-string \u8fdb\u9636)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6253\u5370\u6570\u5b57\u65f6\uff0c\u4f7f\u7528 f-string \u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u683c\u5f0f\u5316\u9700\u6c42\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><span>26<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">num = 1234567.89123\n\n# 1. \u5343\u5206\u4f4d\u5206\u9694\u7b26\nprint(f&quot;{num:,}&quot;)           # 1,234,567.89123\n\n# 2. \u4fdd\u7559\u5c0f\u6570\u4f4d\u6570 (\u56db\u820d\u4e94\u5165)\nprint(f&quot;{num:.2f}&quot;)         # 1234567.89\n\n# 3. \u767e\u5206\u6bd4\u683c\u5f0f\nratio = 0.856\nprint(f&quot;{ratio:.1%}&quot;)       # 85.6%\n\n# 4. \u6307\u5b9a\u5bbd\u5ea6\u548c\u5bf9\u9f50 (\u5e38\u7528\u4e8e\u6253\u5370\u8868\u683c)\nfor i in range(1, 4):\n    # \u5bbd\u5ea6\u4e3a 5\uff0c\u53f3\u5bf9\u9f50\n    print(f&quot;Number: {i:5d} | Square: {i**2:5d}&quot;) \n    # \u8f93\u51fa:\n    # Number:     1 | Square:     1\n    # Number:     2 | Square:     4\n    # Number:     3 | Square:     9\n\n# 5. \u683c\u5f0f\u5316\u8fdb\u5236\nval = 255\nprint(f&quot;Binary: {val:b}&quot;)   # Binary: 11111111\nprint(f&quot;Hex: {val:x}&quot;)      # Hex: ff\nprint(f&quot;Octal: {val:o}&quot;)    # Octal: 377<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u6838\u5fc3\u603b\u7ed3\u4e0e\u907f\u5751\u6307\u5357<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6574\u9664 <code>\/\/<\/code> \u7684\u9677\u9631<\/strong>\uff1a<code>\/\/<\/code> \u662f<strong>\u5411\u4e0b\u53d6\u6574<\/strong>\uff08\u5411\u8d1f\u65e0\u7a77\u65b9\u5411\uff09\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u622a\u65ad\u5c0f\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>7 \/\/ 3<\/code> \u7ed3\u679c\u662f <code>2<\/code>\u3002<\/li>\n\n\n\n<li><code>-7 \/\/ 3<\/code> \u7ed3\u679c\u662f <code>-3<\/code>\uff08\u56e0\u4e3a -2.33 \u5411\u4e0b\u53d6\u6574\u662f -3\uff09\uff0c\u800c\u4e0d\u662f <code>-2<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6d6e\u70b9\u6570\u6bd4\u8f83<\/strong>\uff1a\u6c38\u8fdc\u4e0d\u8981\u7528 <code>==<\/code> \u6765\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\uff0c\u8bf7\u4f7f\u7528 <code>math.isclose(a, b)<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5927\u6570\u8ba1\u7b97<\/strong>\uff1aPython \u539f\u751f\u652f\u6301\u5927\u6574\u6570\u8fd0\u7b97\uff0c\u4e0d\u9700\u8981\u50cf Java \u90a3\u6837\u4f7f\u7528 <code>BigInteger<\/code> \u7c7b\u3002<\/li>\n\n\n\n<li><strong>\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a<code>int()<\/code> \u8f6c\u6362\u6d6e\u70b9\u6570\u65f6\u662f\u76f4\u63a5<strong>\u622a\u65ad<\/strong>\u5c0f\u6570\u90e8\u5206\uff08\u5411\u96f6\u53d6\u6574\uff09\uff0c<code>int(3.9)<\/code> \u662f <code>3<\/code>\uff0c<code>int(-3.9)<\/code> \u662f <code>-3<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u4e86\u89e3 Python \u4e2d\u5982\u4f55\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6570\u5b66\u8ba1\u7b97\uff08\u4f8b\u5982\u4f7f\u7528 <code>NumPy<\/code> \u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff09\uff0c\u6216\u8005\u5bf9 <code>decimal<\/code> \u6a21\u5757\u7684\u7cbe\u5ea6\u63a7\u5236\u6709\u7591\u95ee\uff0c\u53ef\u4ee5\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u6570\u5b57\uff08Number\uff09\u662f\u4e0d\u53ef\u53d8\u6570\u636e\u7c7b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/48\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}