遍历列表 bis= ["sss", "ass222","kjjga","dksjkgj"]for xx in bis : print(xx)sssass222kjjgadksjkgj 循环临时存储建议: for cat in cats:for dog in dogs:for item in list_of_items: 循环中执行更多操作: bis= ["sss", "ass222",
遍历列表
bis= ["sss", "ass222","kjjga","dksjkgj"] for xx in bis : print(xx) sss ass222 kjjga dksjkgj
循环临时存储建议:
for cat in cats: for dog in dogs: for item in list_of_items:
循环中执行更多操作:
bis= ["sss", "ass222","kjjga","dksjkgj"] for xx in bis : print(xx + " 你好" "\n") print("loop is end ") sss 你好 ass222 你好 kjjga 你好 dksjkgj 你好 loop is end
避免缩进错误
Python根据缩进来判断代码行与前一个代码行的关系
不必要的缩进/忘记缩进/循环后不必要的缩进/遗漏冒号
创建数值列表
列表非常适合用于存储数字集合
range()函数
for xx in range(1,5): print(xx) 1 2 3 4
使用 range()创建数字列表
xxx = list(range(1,5)) print(xxx) for x in xxx: print(x) for value in range(1,11): xx = value ** 2 print(xx)
对数字列表执行简单的统计计算
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> min(digits) 0 >>> max(digits) 9 >>> sum(digits) 45
列表解析
squares = [value**2 for value in range(1,11)] print(squares)