for...in循环
依次把list或tuple中的每个元素迭代出来
names = ['Michael', 'Bob', 'Tracy']
for name in names:
print(name)
range(101)
就可以生成0-100的整数序列,计算如下:
sum = 0 for x in range(101): sum = sum + x print(sum)
第二种
用while 语句
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print(sum)
作业1
L = ['Bart', 'Lisa', 'Adam'];
n = 0;
while n>=0 and n<3:
print(L[n]);
n = n+1;
break
n = 1
while n <= 100:
if n > 10: # 当n = 11时,条件满足,执行break语句
break # break语句会结束当前循环
print(n)
n = n + 1
print('END')
continue
n = 0 while n < 10: n = n + 1 if n % 2 == 0: # 如果n是偶数,执行continue语句 continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行 print(n)
python那个:后面是根据线来搞的,所以要排好位子
作业2
n = 0
while n <11:
n = n + 1
if n>10:
n = n-1
print(n)