\u904d\u5386\u4e00\u4e2a\u5217\u8868\u975e\u5e38\u7b80\u5355\u3002\u7ed9\u4e00\u4e2a\u503c\u5217\u8868\uff0c\u5e76\u8981\u6c42\u5bf9\u6bcf\u4e2a\u9879\u505a\u4e00\u4e9b\u4e8b\u60c5\u3002\u5047\u8bbe\u4f60\u6709:<\/p>\n
\r\nmy_list = [1,2,3,4] \r\n<\/pre>\n\u7136\u540e\u8981\u6c42\u4f60\u8ba1\u7b97\u5217\u8868\u4e2d\u6bcf\u4e2a\u503c\u7684\u5e73\u65b9:<\/p>\n
\r\nfor each_value in my_list: \r\n print(each_value * each_value)Out: \r\n1 \r\n4 \r\n9 \r\n16 \r\n<\/pre>\n\u7c7b\u4f3c\u5730\uff0c\u4f60\u53ef\u4ee5\u505a\u4e00\u4e9b\u66f4\u590d\u6742\u7684\u5faa\u73af(\u4f8b\u5982'\u5d4c\u5957\u5faa\u73af')\u3002\u4f8b\u5982\uff0c\u7ed9\u4f60\u4e24\u4e2a\u5217\u8868\u5e76\u8981\u6c42:<\/p>\n
(i)\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u503c\u4e0e\u53e6\u4e00\u4e2a\u5217\u8868\u76f8\u4e58<\/p>\n
(ii)\u5c06\u5b83\u4eec\u8ffd\u52a0\u5230\u4e00\u4e2a\u7a7a\u5217\u8868\u4e2d<\/p>\n
(iii)\u6253\u5370\u51fa\u65b0\u7684\u5217\u8868\u3002<\/p>\n
\r\nnew_list = []list1 = [2, 3, 4] \r\nlist2 = [4, 5, 6]for i in list1: \r\n for j in list2: \r\n new_list.append(i*j) \r\n \r\nprint(new_list)Out: \r\n[8, 10, 12, 12, 15, 18, 16, 20, 24] \r\n<\/pre>\n\u904d\u5386\u5143\u7ec4<\/strong><\/div>\n\u904d\u5386\u5143\u7ec4\u53ef\u80fd\u4f1a\u6709\u70b9\u590d\u6742\uff0c\u8fd9\u53d6\u51b3\u4e8e\u5143\u7ec4\u7684\u7ed3\u6784\u548c\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u3002<\/p>\n
\u8ba9\u6211\u4eec\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u5b58\u50a8\u4e00\u4e9b\u5143\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7ec4\u4ee3\u8868\u4e00\u4e2a\u7c7b\u4e2d\u5b66\u751f\u7684\u59d3\u540d\u548c\u5e74\u9f84:<\/p>\n
\r\nstudents = [('Allie', 22), ('Monty', 18), ('Rebecca', 19)] \r\n<\/pre>\n\u73b0\u5728\u7684\u4efb\u52a1\u662f\uff1a<\/p>\n
(i)\u63d0\u53d6\u6240\u6709\u7684\u5e74\u9f84<\/p>\n
(ii)\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/p>\n
(iii)\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84<\/p>\n
\r\nages = []for i,j in students: \r\n ages.append(j) \r\n \r\navg = sum(ages)\/len(ages) \r\nprint(avg) \r\n \r\nOut: \r\n19.666666666666668 \r\n<\/pre>\n\u8fd9\u91cc\u7684\u6bcf\u4e2a\u5143\u7ec4\u90fd\u5305\u542b\u4e24\u4e2a\u6761\u76ee(\u59d3\u540d\u548c\u5e74\u9f84)\u3002\u5373\u4f7f\u60a8\u5bf9\u540d\u79f0\u4e0d\u611f\u5174\u8da3\uff0c\u901a\u8fc7i\u548cj\uff0c\u60a8\u5c06\u6307\u5b9a\u8fd9\u4e24\u4e2a\u9879\u76ee\uff0c\u5e76\u8981\u6c42\u5c06\u9879\u76eej (age)\u8ffd\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002\u5b83\u88ab\u79f0\u4e3a\u201c\u5143\u7ec4\u62c6\u5305\u201d\u3002<\/p>\n
\u904d\u5386\u5b57\u5178<\/strong><\/div>\nPython\u4e2d\u7684\u5b57\u5178\u662f\u952e-\u503c\u5bf9\u7684\u96c6\u5408\uff1a\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u6709\u4e00\u4e2a\u952e\u548c\u4e00\u4e2a\u76f8\u5173\u8054\u7684\u503c\u3002\u4e00\u4e2a\u5b57\u5178\u7684\u4f8b\u5b50:<\/p>\n
\r\n# fruit price dictionary \r\nfruit_prices = {\"apple\": 2.50, \"orange\": 4.99, \"banana\": 0.59} \r\n<\/pre>\n\u60a8\u53ef\u4ee5\u5faa\u73af\u904d\u5386\u8fd9\u4e9bdictionary\u5143\u7d20\u5e76\u6267\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50:<\/p>\n
\u63d0\u53d6\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c:<\/p>\n
\r\nfor i in fruit_prices.keys(): \r\n print(i) \r\n \r\nOut: \r\napple \r\norange \r\nbanana \r\n<\/pre>\n\u5c06\u6240\u6709\u7684\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d:<\/p>\n
\r\nvalues = [] \r\n for i in fruit_prices.values(): \r\n values.append(i) \r\n print(values) \r\n \r\n Out: \r\n [2.5, 4.99, 0.59] \r\n<\/pre>\n\u904d\u5386\u5b57\u7b26\u4e32<\/strong><\/div>\n\u8ba9\u6211\u4eec\u8003\u8651\u5b57\u7b26\u4e32- \" Hello \"\u3002\u5b83\u770b\u8d77\u6765\u50cf\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5417?\u4e8b\u5b9e\u4e0a\uff0c\u5b83\u662f\u3002<\/p>\n
\r\nfor i in 'Hello': \r\n print(i) \r\n \r\nOut: \r\nH \r\ne \r\nl \r\nl \r\no \r\n<\/pre>\n\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u89e3\u5305\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u5bf9\u5b83\u4eec\u6267\u884c\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n
\u540c\u6837\uff0c\u4e5f\u53ef\u4ee5\u904d\u5386\u53e5\u5b50\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u3002\u4f46\u662f\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684\u6b65\u9aa4\u6765\u5206\u5272\u53e5\u5b50\u3002<\/p>\n
\r\nsent = 'the sky is blue' \r\n# splitting the sentence into words \r\nsent_split = sent.split() \r\n# extract each word with a loop \r\nfor i in sent_split: \r\n print(i) \r\n \r\nOut: \r\nthe \r\nsky \r\nis \r\nblue \r\n<\/pre>\nWhile\u5faa\u73af<\/strong><\/div>\n\u4e0efor\u5faa\u73af\u7c7b\u4f3c\uff0cwhile\u5faa\u73af\u91cd\u590d\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u5757\u2014\u2014\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\u3002\u53ea\u6709\u5f53\u5faa\u73af\u6761\u4ef6\u4e3afalse\u65f6\uff0c\u5faa\u73af\u624d\u4f1a\u4e2d\u6b62\u3002<\/p>\n
while\u5faa\u73af\u7684\u4e00\u822c\u7ed3\u6784\u662f\u8fd9\u6837\u7684:<\/p>\n
\r\ni = 0while i <=5: \r\n print(i) \r\n i = i+1 \r\n \r\n# option to break out of the loop \r\nOut: \r\n0 \r\n1 \r\n2 \r\n3 \r\n4 \r\n5 \r\n<\/pre>\n\u5728\u4e0a\u9762\u7684\u6bcf\u4e00\u6b21\u8fed\u4ee3\u4e2d\uff0ci\u7684\u503c\u90fd\u88ab\u8f93\u51fa\u52305\u3002\u5728\u6b64\u4e4b\u540e\uff0cwhile\u5faa\u73af\u6761\u4ef6\u53d8\u4e3afalse(\u5373i = 6\u65f6\uff0ci\u22645\u53d8\u4e3afalse)\u3002<\/p>\n
\r\nuser_id = 'user101' \r\n while True: \r\n user = input('Enter your user ID: ') \r\n \r\n if user == user_id: \r\n print(\"You have entered \", user) \r\n break \r\n else: \r\n print(\"Enter a valid user ID \") \r\n<\/pre>\n\u5faa\u73af\u63a7\u5236:continue, break, pass<\/strong><\/div>\n\u6709\u4e09\u79cd\u6240\u8c13\u7684\u5faa\u73af\u63a7\u5236\u5173\u952e\u5b57:continue, break, pass\u3002\u8fd9\u4e9b\u8bed\u53e5\u6539\u53d8\u5faa\u73af\u6d41\uff0c\u5e76\u5141\u8bb8\u7a0b\u5e8f\u5728\u89e6\u53d1\u7279\u5b9a\u5916\u90e8\u6761\u4ef6\u65f6\u9000\u51fa\u6216\u8df3\u8fc7\u90e8\u5206\u5faa\u73af\u3002<\/p>\n
Break<\/strong><\/div>\n\u5982\u679c\u5728\u5faa\u73af\u4e2d\u5b58\u5728break\u8bed\u53e5\uff0c\u5219\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u7ec8\u6b62\u5faa\u73af\u3002<\/p>\n
\r\nstring = 'hello, there' \r\nfor i in string: \r\n if i == ',': \r\n break \r\n print(i) \r\nOut: \r\nh \r\ne \r\nl \r\nl \r\no \r\n<\/pre>\n\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u4eec\u8981\u6c42\u7a0b\u5e8f\u5728\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u5e76\u6267\u884c\u4e0b\u4e00\u6761\u8bed\u53e5(\u6253\u5370i)<\/p>\n
continue<\/strong><\/div>\ncontinue\u8bed\u53e5\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u8fed\u4ee3\u5e76\u7ee7\u7eed\u5230\u4e0b\u4e00\u4e2a\u8fed\u4ee3\uff0c\u800c\u4e0d\u662f\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n
\u8ba9\u6211\u4eec\u6267\u884c\u4e0a\u9762\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u4f7f\u7528continue\u5173\u952e\u5b57\u3002<\/p>\n
\r\nstring = 'hello, there' \r\nfor i in string: \r\n if i == ',': \r\n continue \r\n print(i)Out: \r\nh \r\ne \r\nl \r\nl \r\no \r\n \r\nt \r\nh \r\ne \r\nr \r\ne \r\n<\/pre>\n\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u5faa\u73af\u9047\u5230\u4e86\u9017\u53f7\u5faa\u73af\u4f1a\u7ee7\u7eed\u8df3\u8fc7\u9017\u53f7\u3002<\/p>\n
Pass<\/strong><\/div>\npass\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u8fd8\u6ca1\u6709\u5199\u7684\u8bed\u53e5\u7684\u5360\u4f4d\u7b26\u3002<\/p>\n
\r\nstring = 'hello, there' \r\nfor i in string: \r\n pass \r\n<\/pre>\n\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u90a3\u91cc\u653e\u5165\u4e00\u4e2apass\uff0c\u5b83\u5c06\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u6d88\u606f\uff0c\u5176\u4f59\u7684\u4ee3\u7801\u5c06\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\n\u672c\u6587\u7684\u76ee\u7684\u662f\u76f4\u89c2\u5730\u4e86\u89e3Python\u4e2d\u7684for\u5faa\u73af\u548cwhile\u5faa\u73af\u3002\u7ed9\u51fa\u4e86\u5982\u4f55\u5faa\u73af\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u4f8b\u5b50\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u548c\u5b57\u7b26\u4e32\u3002\u5728\u6587\u7ae0\u7684\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u793a\u4f8b\u4ecb\u7ecd\u4e86\u5faa\u73af\u63a7\u5236\u8bed\u53e5\u7684\u6982\u5ff5\u2014\u2014break\u3001continue\u548cpass\u3002<\/p>\n
\u672c\u6587\u65e8\u5728\u5bf9\u5faa\u73af\u7684\u5de5\u4f5c\u65b9\u5f0f\u8fdb\u884c\u521d\u6b65\u6982\u8ff0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u7f16\u7a0b\u4e2d\uff0c\u5faa\u73af\u610f\u5473\u7740\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u591a\u6b21\u91cd\u590d\u540c\u4e00\u7ec4\u8ba1\u7b97\u3002 \u60f3\u60f3\u73b0\u5b9e\u751f\u6d3b\u4e2d\u7684\u60c5\u51b5\u3002\u4f60\u662f\u4e00\u4f4d\u5728\u68ee\u6797\u91cc\u6d4b\u91cf\u6811\u6728\u7684\u91ce\u5916\u751f\u7269 […]<\/p>\n","protected":false},"author":1898,"featured_media":215701,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[153],"class_list":["post-215697","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/215697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=215697"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/215697\/revisions"}],"predecessor-version":[{"id":215702,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/215697\/revisions\/215702"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/215701"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=215697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=215697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=215697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}