【Python】003_循环与 pass 语句

一、循环

while

numList = [1,2,3,4,5];

evenList = [];
oddList = [];

while (len(numList) > 0):
  num = numList.pop();
  if (num % 2 == 0):
    evenList.append(num)
  else:
    oddList.append(num)
  if (len(numList) == 1):
    break;
    
print(evenList);
print(oddList);	#[5, 3]

在这里插入图片描述

for

Python for 循环可以遍历任何序列的项目,如一个列表或者一个字符串。

写法一:迭代模式

numList = [1,2,3,4,5];

for n in numList:
  print(n),
1,2,3,4,5

写法二:序列索引迭代

numList = [1,2,3,4,5];

for i in range(1, len(numList) - 1):
  print(numList[i]),
2 3 4

二、pass

pass 不做任何事情,一般用做占位语句。

发布了419 篇原创文章 · 获赞 94 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/104474791