while循环
lista=["hilary","mike",1,2,3,4,6.1,7,8]
listb=[]
listc=[]
i=0
while i<len(lista):
if (i+1)&1==1:
listb.append(lista[i])
else:
listc.append(lista[i])
i+=1
else:
print(listb,end=' ') #不换行
print(listc,end=' ')
print("Over")
#结果为 ['hilary', 1, 3, 6.1, 8] ['mike', 2, 4, 7] Over
for循环
lista=["hilary","mike",1,2,3,4,6.1,7,8]
listb,listc,listd=[],[],[]
str="ASDFGHJKL"
for c in str:
print("letter is " + c)
for item in lista:
print(item,end=' ')
else:
print("Over")
for i in range(len(lista)):
if (i+1)%3==1:
listb.append(lista[i])
elif (i+1)%3==2:
listc.append(lista[i])
else:
listd.append(lista[i])
else:
print("Over")
实例:素数筛选
N=int(input("input N:"))
listprime=list(range(1,N+1))
listprime[0]=0
for i in range(2,N+1):
if listprime[i-1]!=0:
for j in range(i*2,N+1,i):
listprime[j-1]=0
#判断num是否是素数只需看listprime[num-1]是否为0
print(listprime)