{"id":2729,"date":"2025-03-14T22:16:03","date_gmt":"2025-03-14T14:16:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2729"},"modified":"2025-03-14T22:16:03","modified_gmt":"2025-03-14T14:16:03","slug":"python-3-%e6%95%b0%e5%ad%97%ef%bc%88number%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-3-%e6%95%b0%e5%ad%97%ef%bc%88number%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python 3 \u6570\u5b57\uff08Number\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>Python 3 \u63d0\u4f9b\u4e86<strong>\u591a\u79cd\u6570\u503c\u7c7b\u578b<\/strong>\uff0c\u4e3b\u8981\u5305\u62ec <strong>\u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u590d\u6570\uff08complex\uff09<\/strong>\u3002\u6b64\u5916\uff0cPython \u8fd8\u652f\u6301 <strong>\u8fdb\u5236\u8f6c\u6362\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u968f\u673a\u6570\u548c\u6570\u5b66\u51fd\u6570<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u6570\u5b57\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p>Python 3 \u4e3b\u8981\u652f\u6301\u4e09\u79cd\u6570\u503c\u7c7b\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>int<\/code><\/td><td><strong>\u6574\u6570<\/strong>\uff0c\u6ca1\u6709\u5927\u5c0f\u9650\u5236<\/td><td><code>10, -5, 1000000<\/code><\/td><\/tr><tr><td><code>float<\/code><\/td><td><strong>\u6d6e\u70b9\u6570<\/strong>\uff0c\u5e26\u5c0f\u6570\u70b9\u6216\u79d1\u5b66\u8ba1\u6570\u6cd5<\/td><td><code>3.14, -0.001, 1.2e3<\/code><\/td><\/tr><tr><td><code>complex<\/code><\/td><td><strong>\u590d\u6570<\/strong>\uff0c\u5305\u542b\u5b9e\u90e8\u548c\u865a\u90e8<\/td><td><code>2 + 3j, -1.5 + 2.7j<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10        # \u6574\u6570\nb = 3.14      # \u6d6e\u70b9\u6570\nc = 2 + 3j    # \u590d\u6570\nprint(type(a))  # &lt;class 'int'&gt;\nprint(type(b))  # &lt;class 'float'&gt;\nprint(type(c))  # &lt;class 'complex'&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u6570\u503c\u8fd0\u7b97<\/strong><\/h2>\n\n\n\n<p>Python \u5141\u8bb8\u5bf9\u6570\u503c\u8fdb\u884c<strong>\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664<\/strong>\u7b49\u8fd0\u7b97\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fd0\u7b97\u7b26<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><th>\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><code>+<\/code><\/td><td>\u52a0\u6cd5<\/td><td><code>5 + 3<\/code><\/td><td><code>8<\/code><\/td><\/tr><tr><td><code>-<\/code><\/td><td>\u51cf\u6cd5<\/td><td><code>10 - 4<\/code><\/td><td><code>6<\/code><\/td><\/tr><tr><td><code>*<\/code><\/td><td>\u4e58\u6cd5<\/td><td><code>6 * 2<\/code><\/td><td><code>12<\/code><\/td><\/tr><tr><td><code>\/<\/code><\/td><td>\u9664\u6cd5\uff08\u8fd4\u56de\u6d6e\u70b9\u6570\uff09<\/td><td><code>9 \/ 2<\/code><\/td><td><code>4.5<\/code><\/td><\/tr><tr><td><code>\/\/<\/code><\/td><td>\u6574\u9664\uff08\u5411\u4e0b\u53d6\u6574\uff09<\/td><td><code>9 \/\/ 2<\/code><\/td><td><code>4<\/code><\/td><\/tr><tr><td><code>%<\/code><\/td><td>\u53d6\u4f59\uff08\u53d6\u6a21\uff09<\/td><td><code>9 % 2<\/code><\/td><td><code>1<\/code><\/td><\/tr><tr><td><code>**<\/code><\/td><td>\u5e42\u8fd0\u7b97<\/td><td><code>2 ** 3<\/code><\/td><td><code>8<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = 9, 2\nprint(a + b)  # 11\nprint(a - b)  # 7\nprint(a * b)  # 18\nprint(a \/ b)  # 4.5\nprint(a \/\/ b) # 4\nprint(a % b)  # 1\nprint(a ** b) # 81<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u8fdb\u5236\u8868\u793a<\/strong><\/h2>\n\n\n\n<p>Python \u652f\u6301<strong>\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u3001\u5341\u516d\u8fdb\u5236<\/strong>\u7684\u6574\u6570\u8868\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fdb\u5236<\/th><th>\u8bed\u6cd5<\/th><th>\u793a\u4f8b<\/th><th>\u7ed3\u679c\uff08\u5341\u8fdb\u5236\uff09<\/th><\/tr><\/thead><tbody><tr><td><strong>\u4e8c\u8fdb\u5236<\/strong><\/td><td><code>0b<\/code> \u5f00\u5934<\/td><td><code>0b1010<\/code><\/td><td><code>10<\/code><\/td><\/tr><tr><td><strong>\u516b\u8fdb\u5236<\/strong><\/td><td><code>0o<\/code> \u5f00\u5934<\/td><td><code>0o12<\/code><\/td><td><code>10<\/code><\/td><\/tr><tr><td><strong>\u5341\u516d\u8fdb\u5236<\/strong><\/td><td><code>0x<\/code> \u5f00\u5934<\/td><td><code>0xA<\/code><\/td><td><code>10<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(0b1010)  # 10\nprint(0o12)    # 10\nprint(0xA)     # 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86 <strong><code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>complex()<\/code><\/strong> \u8fdb\u884c\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><th>\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><code>int(x)<\/code><\/td><td>\u8f6c\u6362\u4e3a\u6574\u6570<\/td><td><code>int(3.6)<\/code><\/td><td><code>3<\/code><\/td><\/tr><tr><td><code>float(x)<\/code><\/td><td>\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/td><td><code>float(5)<\/code><\/td><td><code>5.0<\/code><\/td><\/tr><tr><td><code>complex(x, y)<\/code><\/td><td>\u8f6c\u6362\u4e3a\u590d\u6570<\/td><td><code>complex(2, -3)<\/code><\/td><td><code>(2-3j)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(int(3.9))       # 3\nprint(float(10))      # 10.0\nprint(complex(2, 5))  # (2+5j)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5e38\u7528\u6570\u5b66\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p>Python \u5185\u7f6e <strong><code>math<\/code> \u6a21\u5757<\/strong> \u63d0\u4f9b\u4e86<strong>\u6570\u5b66\u8ba1\u7b97\u51fd\u6570<\/strong>\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u5f15\u5165 <code>math<\/code> \u6a21\u5757<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><th>\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><code>math.ceil(x)<\/code><\/td><td>\u5411\u4e0a\u53d6\u6574<\/td><td><code>math.ceil(3.4)<\/code><\/td><td><code>4<\/code><\/td><\/tr><tr><td><code>math.floor(x)<\/code><\/td><td>\u5411\u4e0b\u53d6\u6574<\/td><td><code>math.floor(3.9)<\/code><\/td><td><code>3<\/code><\/td><\/tr><tr><td><code>math.sqrt(x)<\/code><\/td><td>\u5e73\u65b9\u6839<\/td><td><code>math.sqrt(9)<\/code><\/td><td><code>3.0<\/code><\/td><\/tr><tr><td><code>math.pow(x, y)<\/code><\/td><td><code>x<\/code> \u7684 <code>y<\/code> \u6b21\u5e42<\/td><td><code>math.pow(2, 3)<\/code><\/td><td><code>8.0<\/code><\/td><\/tr><tr><td><code>math.fabs(x)<\/code><\/td><td>\u7edd\u5bf9\u503c<\/td><td><code>math.fabs(-10)<\/code><\/td><td><code>10.0<\/code><\/td><\/tr><tr><td><code>math.factorial(x)<\/code><\/td><td>\u9636\u4e58<\/td><td><code>math.factorial(5)<\/code><\/td><td><code>120<\/code><\/td><\/tr><tr><td><code>math.log(x, base)<\/code><\/td><td>\u5bf9\u6570<\/td><td><code>math.log(8, 2)<\/code><\/td><td><code>3.0<\/code><\/td><\/tr><tr><td><code>math.sin(x)<\/code><\/td><td>\u6b63\u5f26<\/td><td><code>math.sin(math.pi\/2)<\/code><\/td><td><code>1.0<\/code><\/td><\/tr><tr><td><code>math.cos(x)<\/code><\/td><td>\u4f59\u5f26<\/td><td><code>math.cos(0)<\/code><\/td><td><code>1.0<\/code><\/td><\/tr><tr><td><code>math.tan(x)<\/code><\/td><td>\u6b63\u5207<\/td><td><code>math.tan(math.pi\/4)<\/code><\/td><td><code>1.0<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\nprint(math.ceil(3.4))   # 4\nprint(math.floor(3.9))  # 3\nprint(math.sqrt(16))    # 4.0\nprint(math.pow(2, 3))   # 8.0\nprint(math.factorial(5))# 120<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u751f\u6210\u968f\u673a\u6570<\/strong><\/h2>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86 <strong><code>random<\/code> \u6a21\u5757<\/strong> \u7528\u4e8e<strong>\u751f\u6210\u968f\u673a\u6570<\/strong>\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u5f15\u5165 <code>random<\/code> \u6a21\u5757<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><th>\u7ed3\u679c<\/th><\/tr><\/thead><tbody><tr><td><code>random.randint(a, b)<\/code><\/td><td>\u751f\u6210 <code>a<\/code> \u5230 <code>b<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/td><td><code>random.randint(1, 10)<\/code><\/td><td><code>3<\/code><\/td><\/tr><tr><td><code>random.uniform(a, b)<\/code><\/td><td>\u751f\u6210 <code>a<\/code> \u5230 <code>b<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/td><td><code>random.uniform(1, 5)<\/code><\/td><td><code>3.14<\/code><\/td><\/tr><tr><td><code>random.random()<\/code><\/td><td>\u751f\u6210 <code>0~1<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/td><td><code>random.random()<\/code><\/td><td><code>0.674<\/code><\/td><\/tr><tr><td><code>random.choice(seq)<\/code><\/td><td>\u4ece\u5e8f\u5217 <code>seq<\/code> \u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/td><td><code>random.choice([1, 2, 3])<\/code><\/td><td><code>2<\/code><\/td><\/tr><tr><td><code>random.shuffle(seq)<\/code><\/td><td>\u968f\u673a\u6253\u4e71 <code>seq<\/code><\/td><td><code>random.shuffle(lst)<\/code><\/td><td><code>[3, 1, 2]<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\n\nprint(random.randint(1, 10))  # \u751f\u6210 1 \u5230 10 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\nprint(random.uniform(1, 5))   # \u751f\u6210 1 \u5230 5 \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\nprint(random.random())        # \u751f\u6210 0 \u5230 1 \u4e4b\u95f4\u7684\u968f\u673a\u6570\nprint(random.choice(&#91;'a', 'b', 'c']))  # \u968f\u673a\u9009\u62e9 'a', 'b', 'c' \u4e4b\u4e00\n\nlst = &#91;1, 2, 3, 4, 5]\nrandom.shuffle(lst)\nprint(lst)  # \u6253\u4e71\u5217\u8868<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python <strong>\u652f\u6301 <code>int<\/code>\u3001<code>float<\/code>\u3001<code>complex<\/code> \u4e09\u79cd\u6570\u503c\u7c7b\u578b<\/strong>\u3002<\/li>\n\n\n\n<li>Python \u63d0\u4f9b <strong>\u8fdb\u5236\u8f6c\u6362\u3001\u6570\u5b66\u8fd0\u7b97\u548c\u6570\u5b66\u51fd\u6570<\/strong>\u3002<\/li>\n\n\n\n<li><strong><code>math<\/code> \u6a21\u5757<\/strong> \u63d0\u4f9b\u9ad8\u7ea7\u6570\u5b66\u51fd\u6570\uff0c<strong><code>random<\/code> \u6a21\u5757<\/strong> \u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p> <strong>\u4f60\u53ef\u4ee5\u5728 Python \u4ea4\u4e92\u6a21\u5f0f\u4e2d\u5c1d\u8bd5\u8fd9\u4e9b\u4ee3\u7801\uff0c\u4f53\u9a8c Python \u7684\u5f3a\u5927\u6570\u503c\u8ba1\u7b97\u529f\u80fd\uff01<\/strong> \u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3 \u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u503c\u7c7b\u578b\uff0c\u4e3b\u8981\u5305\u62ec \u6574\u6570\uff08int\uff09\u3001\u6d6e\u70b9\u6570\uff08float\uff09\u3001\u590d\u6570\uff08complex\uff09\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2730,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2729"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2729\/revisions"}],"predecessor-version":[{"id":2731,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2729\/revisions\/2731"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2730"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}