python基础-----for循环

tech2024-04-09  70

for i in range(6) (python中for循环的运用) 高斯:(callable 可调用的)

sum = 0 for i in range(1,101): sum +=i print(sum)

连乘1到9:

fact = 1 for i in range(1,10): fact *= i print(fact)

九九乘法表:

for i in range(10): for j in range(1,i+1): #print(j,"*",i,"=",i*j,end=",") print("%d*%d=%d"%(i,j,i*j),end=",")

#两个输出不一样,因为字符字节为2,整型字节为1.

list(列表1) 数组(相同类型),列表(类型不相同)

a=[0,1,2,3,4,[6,7,8,[8,9],10],"abc"] print(a) b=range(1,10) print(list(b)) c=[i+3 for i in b if i%2!=0] print(c) print(b[::])#step print(b[-8:2:-2])

从哪到哪多长 (第一个冒号前面没写为0,第二个冒号前面没写默认为最大,第二个冒号后默认为1)(SLICE—切片)

list(列表2)

a=[1,3,4] d=['a',"abc",3,'a'] b=[] for i in a: b.append(i+2) print(b) c=a+b print(c) print(d*3)

list(列表3)

a=list(range(101)) b=[0]*len(a) for i in range(len(a)) b[i]=a[i]+3 print(b) print(sum(list(range(101))))
最新回复(0)