{"id":253,"date":"2025-11-09T10:10:45","date_gmt":"2025-11-09T02:10:45","guid":{"rendered":"https:\/\/www.linerroom.cn\/?p=253"},"modified":"2025-11-09T10:10:45","modified_gmt":"2025-11-09T02:10:45","slug":"python%e7%9a%8461%e4%b8%aa%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.linerroom.cn\/?p=253","title":{"rendered":"python\u768461\u4e2a\u5185\u7f6e\u51fd\u6570"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u7840\u8f93\u51fa\u4e0e\u8f93\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. print()\uff1a\u6253\u5370\u8f93\u51fa\u5185\u5bb9\nprint(\"Hello, Python!\")\n\n# 2. input()\uff1a\u63a5\u6536\u7528\u6237\u8f93\u5165\uff08\u8fd4\u56de\u5b57\u7b26\u4e32\uff09\nname = input(\"\u8bf7\u8f93\u5165\u59d3\u540d\uff1a\")\nprint(\"\u4f60\u7684\u59d3\u540d\u662f\", name)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff08\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 3. range()\uff1a\u751f\u6210\u6574\u6570\u5e8f\u5217\nfor num in range(1, 5):\n    print(num)\n\n# 4. append()\uff1a\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\nnumbers = &#91;1, 2, 3]\nnumbers.append(4)\nprint(numbers)\n\n# 5. extend()\uff1a\u5217\u8868\u6269\u5c55\u5143\u7d20\nnumbers = &#91;1, 2, 3]\nmore_numbers = &#91;4, 5, 6]\nnumbers.extend(more_numbers)\nprint(numbers)\n\n# 6. insert()\uff1a\u5217\u8868\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\nnumbers = &#91;1, 2, 3]\nnumbers.insert(1, 4)\nprint(numbers)\n\n# 7. remove()\uff1a\u5217\u8868\u79fb\u9664\u5143\u7d20\nnumbers = &#91;1, 2, 3, 2, 4]\nnumbers.remove(2)\nprint(numbers)\n\n# 8. pop()\uff1a\u5217\u8868\u5f39\u51fa\u5143\u7d20\nnumbers = &#91;1, 2, 3]\npopped = numbers.pop(1)\nprint(popped)\nprint(numbers)\n\n# 9. index()\uff1a\u5217\u8868\u5143\u7d20\u7d22\u5f15\nnumbers = &#91;1, 2, 3, 2, 4]\nindex = numbers.index(2)\nprint(index)\n\n# 10. count()\uff1a\u5217\u8868\u5143\u7d20\u8ba1\u6570\nnumbers = &#91;1, 2, 3, 2, 4]\ncount = numbers.count(2)\nprint(count)\n\n# 11. sort()\uff1a\u5217\u8868\u6392\u5e8f\nnumbers = &#91;5, 2, 4, 1, 3]\nnumbers.sort()\nprint(numbers)\n\n# 12. reverse()\uff1a\u5217\u8868\u53cd\u8f6c\u5143\u7d20\u987a\u5e8f\nnumbers = &#91;1, 2, 3, 4, 5]\nnumbers.reverse()\nprint(numbers)\n\n# 13. dict()\uff1a\u521b\u5efa\u5b57\u5178\nperson = dict(name='Alice', age=25)\nprint(person)\n\n# 14. set()\uff1a\u521b\u5efa\u96c6\u5408\nnumbers = &#91;1, 2, 3, 2, 1]\nunique_numbers = set(numbers)\nprint(unique_numbers)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7c7b\u578b\u8f6c\u6362\u4e0e\u5224\u65ad<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 15. str()\uff1a\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\nnumber = 42\nstring = str(number)\nprint(string)\n\n# 16. int()\uff1a\u8f6c\u6362\u4e3a\u6574\u6570\nstring = \"42\"\nnumber = int(string)\nprint(number)\n\n# 17. float()\uff1a\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\nstring = \"3.14\"\nnumber = float(string)\nprint(number)\n\n# 18. type()\uff1a\u8fd4\u56de\u5bf9\u8c61\u7c7b\u578b\nnumber = 42\nprint(type(number))\n\n# 19. list()\uff1a\u8f6c\u6362\u4e3a\u5217\u8868\nstring = \"Hello\"\nchar_list = list(string)\nprint(char_list)\n\n# 20. tuple()\uff1a\u8f6c\u6362\u4e3a\u5143\u7ec4\nlist_data = &#91;1, 2, 3]\ntuple_data = tuple(list_data)\nprint(tuple_data)\n\n# 21. isinstance()\uff1a\u68c0\u67e5\u5bf9\u8c61\u7c7b\u578b\nnumber = 42\nprint(isinstance(number, int))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6570\u503c\u4e0e\u6570\u5b66\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 22. sum()\uff1a\u6c42\u548c\nnumbers = &#91;1, 2, 3, 4, 5]\ntotal = sum(numbers)\nprint(total)\n\n# 23. max()\uff1a\u6c42\u6700\u5927\u503c\nnumbers = &#91;1, 2, 3, 4, 5]\nmaximum = max(numbers)\nprint(maximum)\n\n# 24. min()\uff1a\u6c42\u6700\u5c0f\u503c\nnumbers = &#91;1, 2, 3, 4, 5]\nminimum = min(numbers)\nprint(minimum)\n\n# 25. abs()\uff1a\u7edd\u5bf9\u503c\nnumber = -42\nabsolute = abs(number)\nprint(absolute)\n\n# 26. round()\uff1a\u56db\u820d\u4e94\u5165\nnumber = 3.14159\nrounded = round(number, 2)\nprint(rounded)\n\n# 27. pow()\uff1a\u5e42\u8fd0\u7b97\nresult = pow(2, 3)\nprint(result)\n\n# 28. divmod()\uff1a\u5546\u548c\u4f59\u6570\nquotient, remainder = divmod(10, 3)\nprint(quotient, remainder)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 29. replace()\uff1a\u5b57\u7b26\u4e32\u66ff\u6362\nst = \"i want a apple\"\nst = st.replace(\"apple\", \"mice\")\nprint(st)\n\n# 30. strip()\uff1a\u5b57\u7b26\u4e32\u53bb\u7a7a\u683c\nst = \"  hello  \"\nst = st.strip()\nprint(st+\"end\")\n\n# 31. format()\uff1a\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\nname = \"Alice\"\nage = 25\nformatted = format(\"Name: {}, Age: {}\", name, age)\nprint(formatted)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d.\u53ef\u8fed\u4ee3\u5bf9\u8c61\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 32. sorted()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6392\u5e8f\nnumbers = &#91;5, 2, 4, 1, 3]\nsorted_numbers = sorted(numbers)\nprint(sorted_numbers)\n\n# 33. reversed()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u53cd\u8f6c\nnumbers = &#91;1, 2, 3, 4, 5]\nreversed_numbers = list(reversed(numbers))\nprint(reversed_numbers)\n\n# 34. zip()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7ec4\u5408\nnames = &#91;'Alice', 'Bob', 'Charlie']\nages = &#91;25, 30, 35]\nzipped = list(zip(names, ages))\nprint(zipped)\n\n# 35. enumerate()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u679a\u4e3e\nnames = &#91;'Alice', 'Bob', 'Charlie']\nfor index, name in enumerate(names):\n    print(f\"Name at index {index}: {name}\")\n\n# 36. any()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5b58\u5728\u771f\u5143\u7d20\nnumbers = &#91;0, 1, 2, 3]\nprint(any(numbers))\n\n# 37. all()\uff1a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6240\u6709\u5143\u7d20\u4e3a\u771f\nnumbers = &#91;0, 1, 2, 3]\nprint(all(numbers))\n\n# 38. filter()\uff1a\u8fc7\u6ee4\u53ef\u8fed\u4ee3\u5bf9\u8c61\nnumbers = &#91;1, 2, 3, 4, 5]\neven_numbers = list(filter(lambda x: x%2==0, numbers))\nprint(even_numbers)\n\n# 39. map()\uff1a\u6620\u5c04\u53ef\u8fed\u4ee3\u5bf9\u8c61\nnumbers = &#91;1, 2, 3, 4, 5]\nsquared_numbers = list(map(lambda x: x**2, numbers))\nprint(squared_numbers)\n\n# 40. reduce()\uff1a\u7d2f\u79ef\u8ba1\u7b97\nfrom functools import reduce\nnumbers = &#91;1, 2, 3, 4, 5]\nproduct = reduce(lambda x, y: x * y, numbers)\nprint(product)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5bf9\u8c61\u4e0e\u5c5e\u6027\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 41. callable()\uff1a\u68c0\u67e5\u53ef\u8c03\u7528\u5bf9\u8c61\ndef say_hello():\n    print(\"Hello!\")\nprint(callable(say_hello))\n\n# 42. getattr()\uff1a\u83b7\u53d6\u5bf9\u8c61\u5c5e\u6027\nclass Person:\n    name = \"Alice\"\nperson = Person()\nname = getattr(person, \"name\")\nprint(name)\n\n# 43. setattr()\uff1a\u8bbe\u7f6e\u5bf9\u8c61\u5c5e\u6027\nclass Person:\n    name = \"\"\nperson = Person()\nsetattr(person, \"name\", \"Alice\")\nprint(person.name)\n\n# 44. delattr()\uff1a\u5220\u9664\u5bf9\u8c61\u5c5e\u6027\nclass Person:\n    name = \"Alice\"\nperson = Person()\ndelattr(person, \"name\")\nprint(hasattr(person, \"name\"))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u6587\u4ef6\u4e0e\u7cfb\u7edf\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 45. open()\u3001read()\u3001close()\uff1a\u6587\u4ef6\u8bfb\u53d6\nfile = open(\"example.txt\", \"r\")\ncontent = file.read()\nprint(content)\nfile.close()\n\n# 46. close()\uff1a\u6587\u4ef6\u5173\u95ed\uff08\u540c\u4e0a\u793a\u4f8b\uff09\n\n# 47. read()\uff1a\u6587\u4ef6\u8bfb\u53d6\uff08\u540c\u4e0a\u793a\u4f8b\uff09\n\n# 48. write()\uff1a\u6587\u4ef6\u5199\u5165\nfile = open(\"example.txt\", \"w\")\nfile.write(\"Hello, World!\")\nfile.close()\n\n# 49. time.sleep()\uff1a\u7a0b\u5e8f\u6682\u505c\nimport time\ntime.sleep(5)\nprint('hello')\n\n# 50. listdir()\uff1a\u663e\u793a\u76ee\u5f55\u6587\u4ef6\nimport os\npath = r'D:\/images'\ndirs = os.listdir(path)\nfor file in dirs:\n    print(file)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u968f\u673a\u4e0e\u7f16\u7801\u64cd\u4f5c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 51. random.random()\uff1a\u751f\u6210\u968f\u673a\u6570\nimport random\nprint(random.random())\n\n# 52. chr()\uff1aUnicode\u8f6c\u5b57\u7b26\nchar = chr(65)\nprint(char)\n\n# 53. ord()\uff1a\u5b57\u7b26\u8f6cUnicode\ncode = ord('A')\nprint(code)\n\n# 54. bin()\uff1a\u6574\u6570\u8f6c\u4e8c\u8fdb\u5236\nbinary = bin(10)\nprint(binary)\n\n# 55. hex()\uff1a\u6574\u6570\u8f6c\u5341\u516d\u8fdb\u5236\nhexadecimal = hex(16)\nprint(hexadecimal)\n\n# 56. oct()\uff1a\u6574\u6570\u8f6c\u516b\u8fdb\u5236\noctal = oct(8)\nprint(octal)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u7279\u6b8a\u529f\u80fd\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># 57. slice()\uff1a\u5207\u7247\u64cd\u4f5c\nnumbers = &#91;0, 1, 2, 3, 4, 5]\nsliced = numbers&#91;slice(2, 5)]\nprint(sliced)\n\n# 58. bytearray()\uff1a\u53ef\u53d8\u5b57\u8282\u6570\u7ec4\nmy_array = bytearray(&#91;0, 1, 2, 3])\nprint(my_array)\n\n# 59. bytes()\uff1a\u4e0d\u53ef\u53d8\u5b57\u8282\u6570\u7ec4\nmy_bytes = bytes(&#91;0, 1, 2, 3])\nprint(my_bytes)\n\n# 60. ascii()\uff1aASCII\u53ef\u6253\u5370\u5b57\u7b26\u4e32\ntext = \"Hello, \u4f60\u597d\"\nascii_text = ascii(text)\nprint(ascii_text)\n\n# 61. exec()\uff1a\u6267\u884c\u52a8\u6001\u4ee3\u7801\ncode = '''\nfor i in range(5):\n    print(i)\n'''\nexec(code)<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u57fa\u7840\u8f93\u51fa\u4e0e\u8f93\u5165 \u4e8c\u3001\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff08\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\uff09 \u4e09\u3001\u7c7b\u578b\u8f6c\u6362\u4e0e\u5224\u65ad \u56db\u3001\u6570\u503c\u4e0e\u6570\u5b66\u64cd\u4f5c \u4e94\u3001\u5b57\u7b26\u4e32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/OIP-C-4.webp?fit=297%2C180&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":1,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}