while循环

语法如下

while 条件语句:# 注意冒号
    循环体

【Python】循环遍历傻傻分不清-LMLPHP


print()

while 嵌套 


题目 - 计算1到100之和 

count = 1
number = 0
while count <= 100:
    number += count  # number = number + count
    count += 1  # count = count + 1
print(number)

遍历for 

【Python】循环遍历傻傻分不清-LMLPHP  

语法如下 

for 迭代变量 in 对象:
     循环体

迭代iterate遍历 

题目 - 循环实现列表中整形元素求和

List = [1,2,3,4,5]
sum = 0
for i in List:
# 从列表当中依次获取数据
    sum += i
print(sum)

range()函数 

for x in range(10):
  print(x)
for x in range(3, 10):
  print(x) 
for x in range(0, 20, 3):
  print(x)

pass占位 

for i in 'Python':
  if i == 'P':
    pass
    print("pass")
  print('当前字符是:',i)
print("END")

【Python】循环遍历傻傻分不清-LMLPHP

02-04 19:23