我说的换行是快捷键的换行,也就是第二行换行到第三行那里继续输入;
快捷键为:Enter或者Tab+Enter,enter失效就用组合键;
for循环语句:
for 目标 in 表达式 :
>>> name ="Aggy"; //1
>>> for i in name: //2
print(i,end="") //3
Aggy
end代表输出的代码不换行;
换行情况如下:
>>> for i in name:
print(i)
A
g
g
y
看哔哩哔哩小甲鱼视频了不起的循环与分支3,有些疑问然后试出来的快捷键。如果有误欢迎指出;
补充一个each:
>>> member=["xiao1","dayi","aaa","bbb","ccc"]
>>> for each in member:
print(each,len(member))
xiao1 5
dayi 5
aaa 5
bbb 5
ccc 5
>>> for each in member:
print (each , len(each))
xiao1 5
dayi 4
aaa 3
bbb 3
ccc 3
#break 终止循环跳出循环体
#continue 结束本次循环。
quit="yes"
answer=input("yes or no:")
while True:
if answer==quit:
break
anwser=input("fault,please try again:")
print ("ok")
===================== RESTART: H:\python\break-while.py =====================
yes or no:no
fault,please try again:yes
ok
>>>
仔细看好range()取值,不然下面会出现误区!
>>> list(range(2))
[0, 1]
#continue 不符合继续退出循环执行循环外部语句,符合条件的继续循环
for i in range(1):
if i%2 !=0: #i除以二的余数不等于0
print(i)
continue
==================== RESTART: H:\python\continue-range.py ====================
>>>
for i in range(2):
if i%2 !=0: #i除以二的余数不等于0
print(i)
continue
==================== RESTART: H:\python\continue-range.py ====================
1
>>>
#continue 不符合继续退出循环执行循环外部语句,符合条件的继续循环
for i in range(10):
if i%2 !=0: #i除以二的余数不等于0
print(i)
continue
# i +=2
# print(i)
======================== RESTART: H:/python/range.py ========================
1
3
5
7
9
>>>
#continue 不符合继续退出循环执行循环外部语句,符合条件的继续循环
for i in range(10):
if i%2 !=0: #i除以二的余数不等于0
print(i)
continue
i +=2
print(i)
======================== RESTART: H:/python/range.py ========================
2 #0+2=2
1 #是continue循环一次之后执行的print(i)
4 #2+2=4
3 #continue
6 #4+2
5 #continue
8 #6+2
7 #continue
10 #8+2
9 #continue
for i in range(10):
if i%2 !=0: #i除以二的余数不等于0
print(i)
continue
print(i)
i -=2
print(i)
==================== RESTART: H:\python\continue-range.py ====================
-2 #0-2
1 #continue
0 #2-2
3 #continue
2 #4-2
5 #continue
4 #6-2
7 #continue
6 #8-2
9 #continue
欢迎指教qq79723521
---------------------