python学习笔记--循环

while循环

lista=["hilary","mike",1,2,3,4,6.1,7,8]
listb=[]
listc=[]

i=0
while i

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)



你可能感兴趣的:(python)