{"id":589,"date":"2025-10-23T15:36:07","date_gmt":"2025-10-23T07:36:07","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=589"},"modified":"2025-10-23T15:36:07","modified_gmt":"2025-10-23T07:36:07","slug":"python3-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/23\/python3-%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","title":{"rendered":"Python3 \u6570\u636e\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u5185\u7f6e\u7684\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u4f60\u53ea\u9700\u8981\u5c06\u6570\u636e\u7c7b\u578b\u4f5c\u4e3a\u51fd\u6570\u540d\u5373\u53ef\u3002<\/p>\n\n\n\n<p>Python \u6570\u636e\u7c7b\u578b\u8f6c\u6362\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362 &#8211; \u81ea\u52a8\u5b8c\u6210<\/li>\n\n\n\n<li>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362 &#8211; \u9700\u8981\u4f7f\u7528\u7c7b\u578b\u51fd\u6570\u6765\u8f6c\u6362<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/h3>\n\n\n\n<p>\u5728\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u4e2d\uff0cPython \u4f1a\u81ea\u52a8\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u53bb\u5e72\u9884\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bf9\u4e24\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u8fd0\u7b97\uff0c\u8f83\u4f4e\u6570\u636e\u7c7b\u578b\uff08\u6574\u6570\uff09\u5c31\u4f1a\u8f6c\u6362\u4e3a\u8f83\u9ad8\u6570\u636e\u7c7b\u578b\uff08\u6d6e\u70b9\u6570\uff09\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>num_int&nbsp;=&nbsp;123<br>num_flo&nbsp;=&nbsp;1.23<\/p>\n\n\n\n<p>num_new&nbsp;=&nbsp;num_int + num_flo<\/p>\n\n\n\n<p><strong>print<\/strong>(&#8220;num_int \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_int))<br><strong>print<\/strong>(&#8220;num_flo \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_flo))<\/p>\n\n\n\n<p><strong>print<\/strong>(&#8220;num_new \u503c\u4e3a:&#8221;,num_new)<br><strong>print<\/strong>(&#8220;num_new \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_new))<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num_int \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'int'&gt;\nnum_flo \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'float'&gt;\nnum_new: \u503c\u4e3a: 124.23\nnum_new \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'float'&gt;<\/pre>\n\n\n\n<p>\u4ee3\u7801\u89e3\u6790\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u4f8b\u4e2d\u6211\u4eec\u5bf9\u4e24\u4e2a\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u00a0<code>num_int<\/code>\u00a0\u548c\u00a0<code>num_flo<\/code>\u00a0\u8fdb\u884c\u76f8\u52a0\u8fd0\u7b97\uff0c\u5e76\u5b58\u50a8\u5728\u53d8\u91cf\u00a0<code>num_new<\/code>\u00a0\u4e2d\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u67e5\u770b\u4e09\u4e2a\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>\u5728\u8f93\u51fa\u7ed3\u679c\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u00a0<code>num_int<\/code>\u00a0\u662f\u00a0<code>\u6574\u578b\uff08integer\uff09<\/code>\u00a0\uff0c\u00a0<code>num_flo<\/code>\u00a0\u662f\u00a0<code>\u6d6e\u70b9\u578b\uff08float\uff09<\/code>\u3002<\/li>\n\n\n\n<li>\u540c\u6837\uff0c\u65b0\u7684\u53d8\u91cf\u00a0<code>num_new<\/code>\u00a0\u662f\u00a0<code>\u6d6e\u70b9\u578b\uff08float\uff09<\/code>\uff0c\u8fd9\u662f\u56e0\u4e3a Python \u4f1a\u5c06\u8f83\u5c0f\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u8f83\u5927\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u6574\u578b\u6570\u636e\u4e0e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u76f8\u52a0\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>num_int&nbsp;=&nbsp;123<br>num_str&nbsp;=&nbsp;&#8220;456&#8221;<\/p>\n\n\n\n<p><strong>print<\/strong>(&#8220;num_int \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_int))<br><strong>print<\/strong>(&#8220;num_str \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_str))<\/p>\n\n\n\n<p><strong>print<\/strong>(num_int+num_str)<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num_int \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'int'&gt;\nnum_str \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'str'&gt;\nTraceback (most recent call last):\n  File \"\/runoob-test\/test.py\", line 7, in &lt;module&gt;\n    print(num_int+num_str)\nTypeError: unsupported operand type(s) for +: 'int' and 'str'<\/pre>\n\n\n\n<p>\u4ece\u8f93\u51fa\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6574\u578b\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u8fd0\u7b97\u7ed3\u679c\u4f1a\u62a5\u9519\uff0c\u8f93\u51fa TypeError\u3002 Python \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u65e0\u6cd5\u4f7f\u7528\u9690\u5f0f\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0cPython \u4e3a\u8fd9\u4e9b\u7c7b\u578b\u7684\u60c5\u51b5\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u79f0\u4e3a\u663e\u5f0f\u8f6c\u6362\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/h3>\n\n\n\n<p>\u5728\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u4e2d\uff0c\u7528\u6237\u5c06\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002 \u6211\u4eec\u4f7f\u7528 int()\u3001float()\u3001str() \u7b49\u9884\u5b9a\u4e49\u51fd\u6570\u6765\u6267\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n\n\n\n<p><strong>int()<\/strong>&nbsp;\u5f3a\u5236\u8f6c\u6362\u4e3a\u6574\u578b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>x&nbsp;=&nbsp;int(1)&nbsp;&nbsp;# x \u8f93\u51fa\u7ed3\u679c\u4e3a 1<br>y&nbsp;=&nbsp;int(2.8)&nbsp;# y \u8f93\u51fa\u7ed3\u679c\u4e3a 2<br>z&nbsp;=&nbsp;int(&#8220;3&#8221;)&nbsp;# z \u8f93\u51fa\u7ed3\u679c\u4e3a 3<\/p>\n\n\n\n<p><strong>float()<\/strong>&nbsp;\u5f3a\u5236\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>x&nbsp;=&nbsp;float(1)&nbsp;&nbsp;# x \u8f93\u51fa\u7ed3\u679c\u4e3a 1.0<br>y&nbsp;=&nbsp;float(2.8)&nbsp;&nbsp;# y \u8f93\u51fa\u7ed3\u679c\u4e3a 2.8<br>z&nbsp;=&nbsp;float(&#8220;3&#8221;)&nbsp;&nbsp;# z \u8f93\u51fa\u7ed3\u679c\u4e3a 3.0<br>w&nbsp;=&nbsp;float(&#8220;4.2&#8221;)&nbsp;# w \u8f93\u51fa\u7ed3\u679c\u4e3a 4.2<\/p>\n\n\n\n<p><strong>str()<\/strong>&nbsp;\u5f3a\u5236\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>x&nbsp;=&nbsp;str(&#8220;s1&#8221;)&nbsp;# x \u8f93\u51fa\u7ed3\u679c\u4e3a &#8216;s1&#8217;<br>y&nbsp;=&nbsp;str(2)&nbsp;# y \u8f93\u51fa\u7ed3\u679c\u4e3a &#8216;2&#8217;<br>z&nbsp;=&nbsp;str(3.0)&nbsp;# z \u8f93\u51fa\u7ed3\u679c\u4e3a &#8216;3.0&#8217;<\/p>\n\n\n\n<p>\u6574\u578b\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u8fdb\u884c\u8fd0\u7b97\uff0c\u5c31\u53ef\u4ee5\u7528\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u6765\u5b8c\u6210\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>num_int&nbsp;=&nbsp;123<br>num_str&nbsp;=&nbsp;&#8220;456&#8221;<\/p>\n\n\n\n<p><strong>print<\/strong>(&#8220;num_int \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_int))<br><strong>print<\/strong>(&#8220;\u7c7b\u578b\u8f6c\u6362\u524d\uff0cnum_str \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_str))<\/p>\n\n\n\n<p>num_str&nbsp;=&nbsp;int(num_str)&nbsp;# \u5f3a\u5236\u8f6c\u6362\u4e3a\u6574\u578b<br><strong>print<\/strong>(&#8220;\u7c7b\u578b\u8f6c\u6362\u540e\uff0cnum_str \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_str))<\/p>\n\n\n\n<p>num_sum&nbsp;=&nbsp;num_int + num_str<\/p>\n\n\n\n<p><strong>print<\/strong>(&#8220;num_int \u4e0e num_str \u76f8\u52a0\u7ed3\u679c\u4e3a:&#8221;,num_sum)<br><strong>print<\/strong>(&#8220;sum \u6570\u636e\u7c7b\u578b\u4e3a:&#8221;,type(num_sum))<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num_int \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'int'&gt;\n\u7c7b\u578b\u8f6c\u6362\u524d\uff0cnum_str \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'str'&gt;\n\u7c7b\u578b\u8f6c\u6362\u540e\uff0cnum_str \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'int'&gt;\nnum_int \u4e0e num_str \u76f8\u52a0\u7ed3\u679c\u4e3a: 579\nsum \u6570\u636e\u7c7b\u578b\u4e3a: &lt;class 'int'&gt;<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u51e0\u4e2a\u5185\u7f6e\u7684\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u8fd9\u4e9b\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u8868\u793a\u8f6c\u6362\u7684\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u51fd\u6570<\/th><th>\u63cf\u8ff0<\/th><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-int.html\" target=\"_blank\" rel=\"noreferrer noopener\">int(x [,base])<\/a><\/td><td>\u5c06x\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-float.html\" target=\"_blank\" rel=\"noreferrer noopener\">float(x)<\/a><\/td><td>\u5c06x\u8f6c\u6362\u5230\u4e00\u4e2a\u6d6e\u70b9\u6570<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-complex.html\" target=\"_blank\" rel=\"noreferrer noopener\">complex(real [,imag])<\/a><\/td><td>\u521b\u5efa\u4e00\u4e2a\u590d\u6570<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-str.html\" target=\"_blank\" rel=\"noreferrer noopener\">str(x)<\/a><\/td><td>\u5c06\u5bf9\u8c61 x \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-repr.html\" target=\"_blank\" rel=\"noreferrer noopener\">repr(x)<\/a><\/td><td>\u5c06\u5bf9\u8c61 x \u8f6c\u6362\u4e3a\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-eval.html\" target=\"_blank\" rel=\"noreferrer noopener\">eval(str)<\/a><\/td><td>\u7528\u6765\u8ba1\u7b97\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6709\u6548Python\u8868\u8fbe\u5f0f,\u5e76\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python3-func-tuple.html\" target=\"_blank\" rel=\"noreferrer noopener\">tuple(s)<\/a><\/td><td>\u5c06\u5e8f\u5217 s \u8f6c\u6362\u4e3a\u4e00\u4e2a\u5143\u7ec4<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python3-att-list-list.html\" target=\"_blank\" rel=\"noreferrer noopener\">list(s)<\/a><\/td><td>\u5c06\u5e8f\u5217 s \u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-set.html\" target=\"_blank\" rel=\"noreferrer noopener\">set(s)<\/a><\/td><td>\u8f6c\u6362\u4e3a\u53ef\u53d8\u96c6\u5408<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-dict.html\" target=\"_blank\" rel=\"noreferrer noopener\">dict(d)<\/a><\/td><td>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u3002d \u5fc5\u987b\u662f\u4e00\u4e2a (key, value)\u5143\u7ec4\u5e8f\u5217\u3002<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-frozenset.html\" target=\"_blank\" rel=\"noreferrer noopener\">frozenset(s)<\/a><\/td><td>\u8f6c\u6362\u4e3a\u4e0d\u53ef\u53d8\u96c6\u5408<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-chr.html\" target=\"_blank\" rel=\"noreferrer noopener\">chr(x)<\/a><\/td><td>\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-ord.html\" target=\"_blank\" rel=\"noreferrer noopener\">ord(x)<\/a><\/td><td>\u5c06\u4e00\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5b83\u7684\u6574\u6570\u503c<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-hex.html\" target=\"_blank\" rel=\"noreferrer noopener\">hex(x)<\/a><\/td><td>\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><a href=\"https:\/\/www.runoob.com\/python3\/python-func-oct.html\" target=\"_blank\" rel=\"noreferrer noopener\">oct(x)<\/a><\/td><td>\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u516b\u8fdb\u5236\u5b57\u7b26\u4e32<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u5185\u7f6e\u7684\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":475,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":676,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":590,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/589\/revisions\/590"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/475"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}