{"id":1168174,"date":"2025-01-15T15:54:50","date_gmt":"2025-01-15T07:54:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168174.html"},"modified":"2025-01-15T15:54:53","modified_gmt":"2025-01-15T07:54:53","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e5%85%ab%e8%bf%9b%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168174.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u516b\u8fdb\u5236"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065456\/e43c6d71-9d87-4023-929a-f9c7147baecb.webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u516b\u8fdb\u5236\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u524d\u7f00\u201c0o\u201d\u6216\u201c0O\u201d\u6765\u8868\u793a\u516b\u8fdb\u5236\u6570\u3001\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570oct()\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5<\/strong>\u3002\u4f8b\u5982\uff0c\u516b\u8fdb\u5236\u65700123\u53ef\u4ee5\u5199\u4f5c0o123\uff0c\u6574\u657083\u53ef\u4ee5\u901a\u8fc7oct(83)\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u516b\u8fdb\u5236\u6570\u7684\u8868\u793a\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u4ee5\u201c0o\u201d\u6216\u201c0O\u201d\u5f00\u5934\u3002\u8fd9\u91cc\u7684\u201co\u201d\u4ee3\u8868octal\uff08\u516b\u8fdb\u5236\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_number = 0o123<\/p>\n<p>print(octal_number)  # \u8f93\u51fa83<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c0o123 \u662f\u4e00\u4e2a\u516b\u8fdb\u5236\u6570\uff0c\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u503c\u662f83\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528oct()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570oct()\uff0c\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002oct()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ee5\u201c0o\u201d\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decimal_number = 83<\/p>\n<p>octal_representation = oct(decimal_number)<\/p>\n<p>print(octal_representation)  # \u8f93\u51fa&#39;0o123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0coct(83) \u5c06\u6574\u657083\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u201c0o123\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u516b\u8fdb\u5236\u6570\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u516b\u8fdb\u5236\u6570\u662f\u4ee58\u4e3a\u57fa\u6570\u7684\u6570\u503c\u7cfb\u7edf\uff0c\u4ec5\u4f7f\u7528\u6570\u5b570\u52307\u8868\u793a\u3002\u516b\u8fdb\u5236\u6570\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u6709\u7279\u5b9a\u7684\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u8ba1\u7b97\u673a\u5e95\u5c42\u5f00\u53d1\u548c\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01\u6027<\/strong>\uff1a\u516b\u8fdb\u5236\u6570\u6bd4\u4e8c\u8fdb\u5236\u6570\u66f4\u7b80\u6d01\uff0c\u8868\u793a\u76f8\u540c\u7684\u6570\u503c\u65f6\uff0c\u6240\u9700\u7684\u4f4d\u6570\u66f4\u5c11\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u516b\u8fdb\u5236\u6570\u6bd4\u4e8c\u8fdb\u5236\u6570\u66f4\u6613\u8bfb\uff0c\u5c24\u5176\u662f\u5904\u7406\u5927\u91cf\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u3002<\/li>\n<li><strong>\u5386\u53f2\u5e94\u7528<\/strong>\uff1a\u5728\u65e9\u671f\u8ba1\u7b97\u673a\u548c\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u4f8b\u5982\u5728Unix\u6587\u4ef6\u6743\u9650\u8868\u793a\u4e2d\u3002<\/li>\n<\/ol>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5c06\u516b\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u6bcf\u4e00\u4f4d\u4e58\u4ee58\u7684\u76f8\u5e94\u6b21\u5e42\uff0c\u7136\u540e\u6c42\u548c\u3002\u4f8b\u5982\uff0c\u516b\u8fdb\u5236\u6570123\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>1 * 8^2 + 2 * 8^1 + 3 * 8^0 = 64 + 16 + 3 = 83<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Python\u4e2d\u7684\u516b\u8fdb\u5236\u6570\u8868\u793a<\/h3>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u4ee5\u201c0o\u201d\u6216\u201c0O\u201d\u5f00\u5934\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_1 = 0o10   # \u516b\u8fdb\u5236\u657010\uff0c\u5341\u8fdb\u5236\u503c\u4e3a8<\/p>\n<p>octal_2 = 0O77   # \u516b\u8fdb\u5236\u657077\uff0c\u5341\u8fdb\u5236\u503c\u4e3a63<\/p>\n<p>octal_3 = 0o123  # \u516b\u8fdb\u5236\u6570123\uff0c\u5341\u8fdb\u5236\u503c\u4e3a83<\/p>\n<p>print(octal_1)  # \u8f93\u51fa8<\/p>\n<p>print(octal_2)  # \u8f93\u51fa63<\/p>\n<p>print(octal_3)  # \u8f93\u51fa83<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>oct()\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>oct()\u51fd\u6570\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decimal_1 = 8<\/p>\n<p>decimal_2 = 63<\/p>\n<p>decimal_3 = 83<\/p>\n<p>octal_1 = oct(decimal_1)<\/p>\n<p>octal_2 = oct(decimal_2)<\/p>\n<p>octal_3 = oct(decimal_3)<\/p>\n<p>print(octal_1)  # \u8f93\u51fa&#39;0o10&#39;<\/p>\n<p>print(octal_2)  # \u8f93\u51fa&#39;0o77&#39;<\/p>\n<p>print(octal_3)  # \u8f93\u51fa&#39;0o123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528oct()\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u516b\u8fdb\u5236\u6570\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u6587\u4ef6\u6743\u9650\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u6743\u9650\u901a\u5e38\u4ee5\u516b\u8fdb\u5236\u6570\u8868\u793a\u3002\u6587\u4ef6\u6743\u9650\u5206\u4e3a\u4e09\u7ec4\uff1a\u7528\u6237\u3001\u7ec4\u548c\u5176\u4ed6\uff0c\u6bcf\u7ec4\u6709\u8bfb\u3001\u5199\u3001\u6267\u884c\u4e09\u4e2a\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">-rwxr-xr-- 1 user group 1234 Jan 1 12:00 file.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u6587\u4ef6\u6743\u9650\u53ef\u4ee5\u5206\u89e3\u4e3a\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7528\u6237\u6743\u9650\uff08rwx\uff09\uff1a\u8bfb\u3001\u5199\u3001\u6267\u884c<\/li>\n<li>\u7ec4\u6743\u9650\uff08r-x\uff09\uff1a\u8bfb\u3001\u6267\u884c<\/li>\n<li>\u5176\u4ed6\u6743\u9650\uff08r&#8211;\uff09\uff1a\u8bfb<\/li>\n<\/ul>\n<p><p>\u5bf9\u5e94\u7684\u516b\u8fdb\u5236\u8868\u793a\u4e3a\uff1a<code>0754<\/code><\/p>\n<\/p>\n<p><h4>\u4f4d\u8fd0\u7b97\u4e2d\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u516b\u8fdb\u5236\u6570\u5728\u4f4d\u8fd0\u7b97\u4e2d\u4e5f\u6709\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u63a7\u5236\u786c\u4ef6\u8bbe\u5907\u3001\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u4f4d\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u4f4d\u8fd0\u7b97<\/p>\n<p>octal_a = 0o12  # \u5341\u8fdb\u523610<\/p>\n<p>octal_b = 0o25  # \u5341\u8fdb\u523621<\/p>\n<h2><strong>\u6309\u4f4d\u4e0e<\/strong><\/h2>\n<p>result_and = octal_a &amp; octal_b<\/p>\n<p>print(oct(result_and))  # \u8f93\u51fa&#39;0o10&#39;<\/p>\n<h2><strong>\u6309\u4f4d\u6216<\/strong><\/h2>\n<p>result_or = octal_a | octal_b<\/p>\n<p>print(oct(result_or))  # \u8f93\u51fa&#39;0o27&#39;<\/p>\n<h2><strong>\u6309\u4f4d\u5f02\u6216<\/strong><\/h2>\n<p>result_xor = octal_a ^ octal_b<\/p>\n<p>print(oct(result_xor))  # \u8f93\u51fa&#39;0o17&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u4f4d\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u516b\u8fdb\u5236\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>\u8f93\u5165\u516b\u8fdb\u5236\u6570\u65f6\u7684\u5408\u6cd5\u6027<\/h4>\n<\/p>\n<p><p>\u8f93\u5165\u516b\u8fdb\u5236\u6570\u65f6\uff0c\u9700\u6ce8\u610f\u5176\u5408\u6cd5\u6027\u3002\u516b\u8fdb\u5236\u6570\u53ea\u80fd\u5305\u542b\u6570\u5b570\u52307\uff0c\u8d85\u51fa\u8303\u56f4\u7684\u6570\u5b57\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>invalid_octal = 0o89  # 8\u548c9\u4e0d\u5728\u516b\u8fdb\u5236\u6570\u8303\u56f4\u5185\uff0c\u5c06\u5f15\u53d1SyntaxError<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u4e0e\u5176\u4ed6\u6570\u5236\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5728\u4e0d\u540c\u6570\u5236\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e9b\u8f6c\u6362\uff0c\u4f8b\u5982bin()\u3001hex()\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">decimal_number = 83<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236<\/strong><\/h2>\n<p>binary_representation = bin(decimal_number)<\/p>\n<p>print(binary_representation)  # \u8f93\u51fa&#39;0b1010011&#39;<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236<\/strong><\/h2>\n<p>hexadecimal_representation = hex(decimal_number)<\/p>\n<p>print(hexadecimal_representation)  # \u8f93\u51fa&#39;0x53&#39;<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236<\/strong><\/h2>\n<p>octal_representation = oct(decimal_number)<\/p>\n<p>print(octal_representation)  # \u8f93\u51fa&#39;0o123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u6570\u5236\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u516b\u8fdb\u5236\u6570\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66<\/h4>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u6709\u65f6\u7528\u4e8e\u8868\u793a\u989c\u8272\u503c\u548c\u56fe\u50cf\u50cf\u7d20\u3002\u867d\u7136\u73b0\u4ee3\u56fe\u5f62\u5904\u7406\u5927\u591a\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u6570\uff0c\u4f46\u5728\u67d0\u4e9b\u4f4e\u7ea7\u522b\u56fe\u5f62\u5904\u7406\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u4ecd\u7136\u6709\u5176\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u65e9\u671f\u8ba1\u7b97\u673a\u56fe\u5f62\u7cfb\u7edf\u4e2d\uff0c\u989c\u8272\u503c\u53ef\u80fd\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h4>\u7f51\u7edc\u7f16\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u5e38\u7528\u4e8e\u8868\u793aIP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002\u867d\u7136IP\u5730\u5740\u901a\u5e38\u4ee5\u70b9\u5206\u5341\u8fdb\u5236\u8868\u793a\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u7279\u522b\u662f\u5e95\u5c42\u7f51\u7edc\u534f\u8bae\u5904\u7406\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u6709\u65f6\u7528\u4e8e\u8868\u793aIP\u5730\u5740\u7684\u5404\u4e2a\u5b57\u8282\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8868\u793aIP\u5730\u5740\u5b57\u8282<\/p>\n<p>ip_address = (0o192, 0o168, 0o1, 0o1)<\/p>\n<p>print(&quot;IP Address:&quot;, ip_address)  # \u8f93\u51faIP Address: (128, 104, 1, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8868\u793aIP\u5730\u5740\u7684\u5404\u4e2a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h4>\u786c\u4ef6\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u786c\u4ef6\u63a7\u5236\u548c\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7f16\u7a0b\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u7528\u4e8e\u8868\u793a\u786c\u4ef6\u5bc4\u5b58\u5668\u7684\u503c\u548c\u63a7\u5236\u4fe1\u53f7\u3002\u4f8b\u5982\uff0c\u5728\u63a7\u5236\u67d0\u4e2a\u786c\u4ef6\u8bbe\u5907\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u67d0\u4e2a\u5bc4\u5b58\u5668\u7684\u503c\uff0c\u516b\u8fdb\u5236\u6570\u53ef\u4ee5\u7b80\u6d01\u5730\u8868\u793a\u8fd9\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8bbe\u7f6e\u786c\u4ef6\u5bc4\u5b58\u5668\u503c<\/p>\n<p>register_value = 0o177<\/p>\n<p>print(&quot;Register Value:&quot;, register_value)  # \u8f93\u51faRegister Value: 127<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8bbe\u7f6e\u786c\u4ef6\u5bc4\u5b58\u5668\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u516b\u8fdb\u5236\u6570\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u52a0\u51cf\u4e58\u9664<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u53ef\u4ee5\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u7b49\u57fa\u672c\u8fd0\u7b97\u3002\u8fd9\u4e9b\u8fd0\u7b97\u4e0e\u5341\u8fdb\u5236\u6570\u7684\u8fd0\u7b97\u7c7b\u4f3c\uff0c\u8fd0\u7b97\u7ed3\u679c\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u516b\u8fdb\u5236\u6570\u7684\u52a0\u6cd5<\/p>\n<p>octal_sum = 0o123 + 0o45<\/p>\n<p>print(octal_sum)  # \u8f93\u51fa126<\/p>\n<h2><strong>\u516b\u8fdb\u5236\u6570\u7684\u51cf\u6cd5<\/strong><\/h2>\n<p>octal_difference = 0o123 - 0o45<\/p>\n<p>print(octal_difference)  # \u8f93\u51fa58<\/p>\n<h2><strong>\u516b\u8fdb\u5236\u6570\u7684\u4e58\u6cd5<\/strong><\/h2>\n<p>octal_product = 0o12 * 0o7<\/p>\n<p>print(octal_product)  # \u8f93\u51fa70<\/p>\n<h2><strong>\u516b\u8fdb\u5236\u6570\u7684\u9664\u6cd5<\/strong><\/h2>\n<p>octal_quotient = 0o144 \/ 0o12<\/p>\n<p>print(octal_quotient)  # \u8f93\u51fa10.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u7684\u8fd0\u7b97\u7ed3\u679c\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c1\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5bf9\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_a = 0o123<\/p>\n<p>octal_b = 0o77<\/p>\n<p>print(octal_a &gt; octal_b)  # \u8f93\u51faTrue<\/p>\n<p>print(octal_a == octal_b)  # \u8f93\u51faFalse<\/p>\n<p>print(octal_a &lt; octal_b)  # \u8f93\u51faFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5bf9\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u6bd4\u8f83\uff0c\u7ed3\u679c\u4f1a\u81ea\u52a8\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u516b\u8fdb\u5236\u6570\u7684\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5bf9\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_number = 0o123<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/strong><\/h2>\n<p>formatted_output = &quot;{:o}&quot;.format(octal_number)<\/p>\n<p>print(formatted_output)  # \u8f93\u51fa&#39;123&#39;<\/p>\n<h2><strong>\u4f7f\u7528f-string<\/strong><\/h2>\n<p>formatted_output = f&quot;{octal_number:o}&quot;<\/p>\n<p>print(formatted_output)  # \u8f93\u51fa&#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u548cf-string\u5bf9\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u516b\u8fdb\u5236\u6570\u7684\u8fdb\u9636\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u4f4d\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u516b\u8fdb\u5236\u6570\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u4f4d\u64cd\u4f5c\uff0c\u4f8b\u5982\u6309\u4f4d\u4e0e\u3001\u6309\u4f4d\u6216\u3001\u6309\u4f4d\u5f02\u6216\u7b49\u3002\u8fd9\u4e9b\u64cd\u4f5c\u5728\u63a7\u5236\u786c\u4ef6\u8bbe\u5907\u3001\u7f51\u7edc\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_a = 0o12  # \u516b\u8fdb\u5236\u657012\uff0c\u5341\u8fdb\u5236\u503c\u4e3a10<\/p>\n<p>octal_b = 0o25  # \u516b\u8fdb\u5236\u657025\uff0c\u5341\u8fdb\u5236\u503c\u4e3a21<\/p>\n<h2><strong>\u6309\u4f4d\u4e0e<\/strong><\/h2>\n<p>result_and = octal_a &amp; octal_b<\/p>\n<p>print(oct(result_and))  # \u8f93\u51fa&#39;0o10&#39;<\/p>\n<h2><strong>\u6309\u4f4d\u6216<\/strong><\/h2>\n<p>result_or = octal_a | octal_b<\/p>\n<p>print(oct(result_or))  # \u8f93\u51fa&#39;0o27&#39;<\/p>\n<h2><strong>\u6309\u4f4d\u5f02\u6216<\/strong><\/h2>\n<p>result_xor = octal_a ^ octal_b<\/p>\n<p>print(oct(result_xor))  # \u8f93\u51fa&#39;0o17&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u6309\u4f4d\u4e0e\u3001\u6309\u4f4d\u6216\u3001\u6309\u4f4d\u5f02\u6216\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u516b\u8fdb\u5236\u6570\u7684\u4f4d\u79fb\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u516b\u8fdb\u5236\u6570\u8fd8\u53ef\u4ee5\u8fdb\u884c\u4f4d\u79fb\u64cd\u4f5c\uff0c\u4f8b\u5982\u5de6\u79fb\u548c\u53f3\u79fb\u3002\u8fd9\u4e9b\u64cd\u4f5c\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">octal_number = 0o12  # \u516b\u8fdb\u5236\u657012\uff0c\u5341\u8fdb\u5236\u503c\u4e3a10<\/p>\n<h2><strong>\u5de6\u79fb\u64cd\u4f5c<\/strong><\/h2>\n<p>left_shifted = octal_number &lt;&lt; 2<\/p>\n<p>print(oct(left_shifted))  # \u8f93\u51fa&#39;0o50&#39;<\/p>\n<h2><strong>\u53f3\u79fb\u64cd\u4f5c<\/strong><\/h2>\n<p>right_shifted = octal_number &gt;&gt; 1<\/p>\n<p>print(oct(right_shifted))  # \u8f93\u51fa&#39;0o5&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bf9\u516b\u8fdb\u5236\u6570\u8fdb\u884c\u5de6\u79fb\u548c\u53f3\u79fb\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u516b\u8fdb\u5236\u6570\u7684\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u9519\u8bef\u793a\u4f8b\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u516b\u8fdb\u5236\u6570\u8f93\u5165\u9519\u8bef<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>invalid_octal = 0o89  # 8\u548c9\u4e0d\u5728\u516b\u8fdb\u5236\u6570\u8303\u56f4\u5185\uff0c\u5c06\u5f15\u53d1SyntaxError<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u786e\u4fdd\u8f93\u5165\u7684\u516b\u8fdb\u5236\u6570\u4ec5\u5305\u542b\u6570\u5b570\u52307\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6b63\u786e\u793a\u4f8b<\/p>\n<p>valid_octal = 0o77<\/p>\n<p>print(valid_octal)  # \u8f93\u51fa63<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u516b\u8fdb\u5236\u6570\u8f6c\u6362\u9519\u8bef<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u9519\u8bef\u793a\u4f8b<\/p>\n<p>decimal_number = 83<\/p>\n<p>octal_representation = oct(decimal_number)<\/p>\n<p>print(int(octal_representation))  # \u8bd5\u56fe\u5c06\u516b\u8fdb\u5236\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5c06\u5f15\u53d1ValueError<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528int()\u51fd\u6570\u5c06\u516b\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u57fa\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6b63\u786e\u793a\u4f8b<\/p>\n<p>decimal_number = 83<\/p>\n<p>octal_representation = oct(decimal_number)<\/p>\n<p>integer_value = int(octal_representation, 8)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa83<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u516b\u8fdb\u5236\u6570\u7684\u672a\u6765\u53d1\u5c55<\/h3>\n<\/p>\n<p><p>\u968f\u7740\u8ba1\u7b97\u673a\u6280\u672f\u7684\u53d1\u5c55\uff0c\u516b\u8fdb\u5236\u6570\u5728\u73b0\u4ee3\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u53ef\u80fd\u9010\u6e10\u51cf\u5c11\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u9886\u57df\uff0c\u516b\u8fdb\u5236\u6570\u4ecd\u7136\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u3001\u786c\u4ef6\u63a7\u5236\u3001\u4f4e\u7ea7\u522b\u7f16\u7a0b\u4e2d\uff0c\u516b\u8fdb\u5236\u6570\u4ecd\u7136\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u672a\u6765\uff0c\u968f\u7740\u65b0\u6280\u672f\u7684\u4e0d\u65ad\u6d8c\u73b0\uff0c\u516b\u8fdb\u5236\u6570\u53ef\u80fd\u4f1a\u5728\u65b0\u7684\u5e94\u7528\u573a\u666f\u4e2d\u83b7\u5f97\u66f4\u591a\u7684\u5173\u6ce8\u548c\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u91cf\u5b50\u8ba1\u7b97\u3001\u7269\u8054\u7f51\u7b49\u65b0\u5174\u9886\u57df\uff0c\u516b\u8fdb\u5236\u6570\u6709\u671b\u53d1\u6325\u5176\u72ec\u7279\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u524d\u7f00\u201c0o\u201d\u6216\u201c0O\u201d\u53ef\u4ee5\u8868\u793a\u516b\u8fdb\u5236\u6570\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570oct()\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u8868\u793a\u6cd5\u3002\u516b\u8fdb\u5236\u6570\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u3001\u7f51\u7edc\u7f16\u7a0b\u3001\u786c\u4ef6\u63a7\u5236\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u7406\u89e3\u548c\u719f\u7ec3\u638c\u63e1\u516b\u8fdb\u5236\u6570\u7684\u8868\u793a\u3001\u8f6c\u6362\u548c\u64cd\u4f5c\uff0c\u5bf9\u4e8e\u7f16\u7a0b\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u7814\u7a76\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u516b\u8fdb\u5236\u6570\uff0c\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u83b7\u5f97\u66f4\u591a\u7684\u5b9e\u8df5\u7ecf\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u516b\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u201c0o\u201d\u6216\u201c0O\u201d\u6765\u8f93\u5165\u516b\u8fdb\u5236\u6570\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u516b\u8fdb\u5236\u6570\u201c10\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u201c0o10\u201d\u6765\u8868\u793a\u3002\u8fd9\u4f1a\u88abPython\u89e3\u91ca\u4e3a\u516b\u8fdb\u5236\u768410\uff0c\u4e5f\u5c31\u662f\u5341\u8fdb\u5236\u76848\u3002<\/p>\n<p><strong>\u516b\u8fdb\u5236\u6570\u5728Python\u4e2d\u7684\u5e94\u7528\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u516b\u8fdb\u5236\u6570\u5728Python\u4e2d\u4e3b\u8981\u7528\u4e8e\u8868\u793a\u6587\u4ef6\u6743\u9650\u3001\u989c\u8272\u503c\u7b49\u3002\u5728\u5904\u7406\u6d89\u53ca\u7cfb\u7edf\u7ea7\u7f16\u7a0b\u6216\u7279\u5b9a\u6570\u636e\u683c\u5f0f\u65f6\uff0c\u4f7f\u7528\u516b\u8fdb\u5236\u6570\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u7684\u7f16\u5199\u548c\u7406\u89e3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u516b\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u6765\u8f6c\u6362\u516b\u8fdb\u5236\u6570\u4e3a\u5341\u8fdb\u5236\u6570\u3002\u53ea\u9700\u5c06\u516b\u8fdb\u5236\u6570\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9<code>int()<\/code>\uff0c\u5e76\u6307\u5b9a\u57fa\u6570\u4e3a8\u3002\u4f8b\u5982\uff0c<code>int(&#39;10&#39;, 8)<\/code>\u5c06\u8fd4\u56de8\uff0c\u56e0\u4e3a\u516b\u8fdb\u5236\u768410\u7b49\u4e8e\u5341\u8fdb\u5236\u76848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6570\u5b57\u524d\u52a0\u4e0a\u524d\u7f00\u201c0o\u201d\u6216\u201c0O\u201d\u6765\u8868\u793a\u516b\u8fdb\u5236\u6570\u3001\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570oct()\u5c06\u6574\u6570\u8f6c [&hellip;]","protected":false},"author":3,"featured_media":1168180,"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\/1168174"}],"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=1168174"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168174\/revisions"}],"predecessor-version":[{"id":1168181,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168174\/revisions\/1168181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168180"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}