{"id":1139713,"date":"2025-01-08T22:12:56","date_gmt":"2025-01-08T14:12:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139713.html"},"modified":"2025-01-08T22:13:00","modified_gmt":"2025-01-08T14:13:00","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e6%95%b0%e4%b8%ba%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139713.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u4e3a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102926\/3056f918-4417-4425-8e9f-f0f2172db5a4.webp\" alt=\"python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u4e3a\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\uff0c\u4f8b\u5982\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\u3001<code>type<\/code>\u51fd\u6570\u3001\u53d6\u6a21\u8fd0\u7b97\u3001\u6216\u8005\u6d6e\u70b9\u6570\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u51c6\u786e\u5224\u65ad\u6570\u636e\u7c7b\u578b\u3002<strong>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>isinstance(num, int)<\/code>\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u662f\u6574\u6570<\/strong>\uff0c\u5982\u679c\u662f\u6574\u6570\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>isinstance<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>isinstance<\/code> \u51fd\u6570\u662f\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u6700\u4f73\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u6574\u6570\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u5176\u5b83\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 5<\/p>\n<p>if isinstance(num, int):<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u597d\u5904\u662f<strong>\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyInt:<\/p>\n<p>    pass<\/p>\n<p>my_int = MyInt()<\/p>\n<p>print(isinstance(my_int, MyInt))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>type<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>type<\/code> \u51fd\u6570\u548c <code>isinstance<\/code> \u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u66f4\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 5<\/p>\n<p>if type(num) is int:<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a<code>type<\/code>\u51fd\u6570\u5728\u68c0\u67e5\u7c7b\u578b\u65f6\u4e0d\u4f1a\u8003\u8651\u7ee7\u627f\u5173\u7cfb\uff0c\u56e0\u6b64\u5728\u5904\u7406\u81ea\u5b9a\u4e49\u7c7b\u578b\u65f6\uff0c<code>isinstance<\/code>\u66f4\u4e3a\u53ef\u9760\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u53d6\u6a21\u8fd0\u7b97\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\uff0c<strong>\u7279\u522b\u662f\u5728\u5904\u7406\u6d6e\u70b9\u6570\u65f6<\/strong>\u3002\u5982\u679c\u4e00\u4e2a\u6d6e\u70b9\u6570\u5bf91\u53d6\u6a21\u7ed3\u679c\u4e3a0\uff0c\u5219\u5b83\u662f\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 5.0<\/p>\n<p>if num % 1 == 0:<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4e00\u4e2a\u4f18\u52bf\u662f<strong>\u53ef\u4ee5\u5904\u7406\u6d6e\u70b9\u6570\u7684\u60c5\u51b5<\/strong>\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6d6e\u70b9\u6570\u65b9\u6cd5 <code>is_integer<\/code><\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u6d6e\u70b9\u6570\u6709\u4e00\u4e2a\u65b9\u6cd5 <code>is_integer<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u4e00\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 5.0<\/p>\n<p>if num.is_integer():<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u7eaf\u7cb9\u7684\u6d6e\u70b9\u6570\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\u7684\u7efc\u5408\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u7efc\u5408\u5224\u65ad\uff0c\u4ee5\u786e\u4fdd\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_integer(num):<\/p>\n<p>    if isinstance(num, int):<\/p>\n<p>        return True<\/p>\n<p>    elif isinstance(num, float) and num.is_integer():<\/p>\n<p>        return True<\/p>\n<p>    else:<\/p>\n<p>        return False<\/p>\n<p>num = 5.0<\/p>\n<p>if is_integer(num):<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e2d\u7684\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u4fa7\u91cd\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5224\u65ad\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2.0, 3.5, &#39;4&#39;, 5]<\/p>\n<p>integers = [num for num in data if is_integer(num)]<\/p>\n<p>print(f&quot;\u6574\u6570\u5217\u8868\uff1a{integers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7528\u6237\u8f93\u5165\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u4e2d\uff0c\u5224\u65ad\u8f93\u5165\u662f\u5426\u4e3a\u6574\u6570\u662f\u5e38\u89c1\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u7528\u6237\u586b\u5199\u8868\u5355\u65f6\uff0c\u786e\u4fdd\u5e74\u9f84\u5b57\u6bb5\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = &#39;25&#39;<\/p>\n<p>if user_input.isdigit():<\/p>\n<p>    print(f&quot;{user_input} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{user_input} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\u53ef\u80fd\u6d89\u53ca\u5230\u6d6e\u70b9\u6570\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>num = math.sqrt(16)<\/p>\n<p>if num.is_integer():<\/p>\n<p>    print(f&quot;{num} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong><code>isinstance<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u548c\u53ef\u9760\u7684\u65b9\u6cd5<\/strong>\uff0c\u7279\u522b\u9002\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u3002<strong><code>type<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u7b80\u5355\u7c7b\u578b\u5224\u65ad<\/strong>\uff0c\u4f46\u4e0d\u8003\u8651\u7ee7\u627f\u5173\u7cfb\u3002<strong>\u53d6\u6a21\u8fd0\u7b97\u548c\u6d6e\u70b9\u6570\u65b9\u6cd5 <code>is_integer<\/code> \u9002\u7528\u4e8e\u5904\u7406\u6d6e\u70b9\u6570\u7684\u60c5\u51b5<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c<strong>\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u7efc\u5408\u5224\u65ad\u662f\u786e\u4fdd\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u7684\u5173\u952e<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>type()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>type(variable) == int<\/code>\u53ef\u4ee5\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a\u6574\u6570\u3002\u6b64\u5916\uff0c<code>isinstance(variable, int)<\/code>\u65b9\u6cd5\u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5224\u65ad\u53d8\u91cf\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7279\u5b9a\u7684\u7c7b\u6216\u5176\u5b50\u7c7b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u53ef\u4ee5\u88ab\u89c6\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6d6e\u70b9\u6570\u5e76\u60f3\u5224\u65ad\u5b83\u662f\u5426\u53ef\u4ee5\u88ab\u89c6\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>float.is_integer()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8fd9\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u4e3a\u6574\u6570\u5f62\u5f0f\uff08\u5373\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff09\u3002\u4f8b\u5982\uff0c<code>3.0.is_integer()<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u800c<code>3.5.is_integer()<\/code>\u4f1a\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u8d1f\u6570\u548c\u96f6\u7684\u6574\u6570\u5224\u65ad\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u548c\u96f6\u4e5f\u88ab\u89c6\u4e3a\u6574\u6570\u3002\u5982\u679c\u4f60\u60f3\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\uff0c\u5305\u62ec\u8d1f\u6570\u548c\u96f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u3002\u4efb\u4f55\u6574\u6570\uff0c\u5305\u62ec-1, 0\u548c1\uff0c\u90fd\u5c06\u8fd4\u56de<code>True<\/code>\u3002\u786e\u4fdd\u5728\u8fdb\u884c\u5224\u65ad\u65f6\uff0c\u4e0d\u4f1a\u9057\u6f0f\u8d1f\u6570\u6216\u96f6\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u6574\u6570\uff0c\u4f8b\u5982\u4f7f\u7528isinstance\u51fd\u6570\u3001type\u51fd\u6570\u3001\u53d6\u6a21\u8fd0 [&hellip;]","protected":false},"author":3,"featured_media":1139723,"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\/1139713"}],"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=1139713"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139713\/revisions"}],"predecessor-version":[{"id":1139725,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139713\/revisions\/1139725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139723"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}