python是靠“缩进”来识别代码断的,同一代码断“必须缩进方式相同”,但是最好全部代码,缩进统一,官方建议“四个空格”,当然敲tab键很方便,但是绝大多数python开发者都用“四个空格”,随大流还是很必要的,否则复制别人代码时,那可就惨了。
错误for循环:
test = [1,2,3,4,5]
for i in test:
空格空格空格空格 print(i)
一个tab print(i)
以上是会报错的
单for:
test = [1,2,3,4,5]
for i in test:
print(i)
嵌套for:
test = [{"a":1,"b":2,"c":3},{"d":4,"e":5,"f":6}]
for i in test:
print(i)
for j in i:
print("j=%s" %i[j])
时间: 2024-10-09 20:10:17