{"id":3892,"date":"2023-11-28T10:09:27","date_gmt":"2023-11-28T02:09:27","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3892"},"modified":"2023-11-28T17:02:22","modified_gmt":"2023-11-28T09:02:22","slug":"5%e7%a7%8d%e6%96%b9%e5%bc%8f%ef%bc%9a%e7%94%a8python%e6%b1%82%e5%87%ba1-100%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3892","title":{"rendered":"6\u79cd\u65b9\u5f0f\uff1a\u7528python\u6c42\u51fa1-100\u7684\u548c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#\u65b9\u6cd5\u4e00\r\nsum2=0\r\nfor i in range(1,101):\r\n    sum2+=i\r\nprint(\"___sum1___\")\r\nprint(sum2)\r\n\r\n#\u65b9\u6cd5\u4e8c\r\ndef fsum(n):\r\n    s=0\r\n    for i in range(1,n+1):\r\n       s+=i\r\n    print(s)\r\nprint(\"----sum2----\")\r\nfsum(100)\r\n\r\n\r\n#\u65b9\u6cd5\u4e09while\u5faa\u73af\u5b9e\u73b0\r\ndef fsum1(n):\r\n    i=0 #\u521d\u59cb\u5316\u53d8\u91cf\r\n    s=0\r\n    while i &lt; n+1: #\u6761\u4ef6\u5224\u65ad\r\n        s +=i #\u5faa\u73af\u4f53\r\n        i +=1 #\u6539\u53d8\u53d8\u91cf\r\n    print(s)\r\nprint(\"----sum3----\")\r\nfsum1(100)\r\n\r\n#\u65b9\u6cd5\u56db\u9012\u5f52\u7684\u601d\u8def\r\ndef fsum2(n):\r\n    if n==1:\r\n        return 1\r\n    else:\r\n        return n+fsum2(n-1)\r\nprint(\"----sum4----\")\r\nprint(fsum2(100))\r\n\r\n#\u65b9\u6cd5\u4e94\u4e00\u53e5\u4ee3\u7801\u641e\u5b9a\r\nprint(\"----sum5----\")\r\nprint(sum(list(range(1,101))))\r\n\r\n#\u65b9\u6cd5\u516d\uff1a\u4f7f\u7528Python\u7684reduce\u51fd\u6570\uff08\u9700\u8981\u5b89\u88c5functools\u5e93\uff09\r\n\r\n#!\/usr\/bin\/python\r\n#coding:utf-8\r\n#author:\u83dc\u5c31\u591a\u7ec3\u5440\r\nfrom functools import reduce\r\ndef add(x, y):\r\n    return x + y\r\ntotal = reduce(add, range(1, 101))\r\nprint(\"----Sum6 Total----\")\r\nprint(total)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3892","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3892"}],"version-history":[{"count":3,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3892\/revisions"}],"predecessor-version":[{"id":3900,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3892\/revisions\/3900"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3892"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}