50.基础语法-循环
for循环
- 遍历可迭代的类型
- 把其中元素依次去除
l = [1,2,3,4,5,9,8]
for a in l:
print(a)
while循环
#本金10000元,利率6.7%,问存多少年大于20000
#不论while中是不是执行,else中的语句都要执行
bj = 10000
year = 0
while bj < 20000:
bj = bj * (1 + 0.067)
year += 1
else:
print(year)
continue用法
- 用法一
l = [1,2,3,4,5,6,7,8,9,10]
for a in l:
if a % 2 == 0:
print(str(a) + " 是个双数")
else:
continue
- 用法二
l = [1,2,3,4,5,6,7,8,9,10]
for a in l:
if a % 2 ==1:
continue
print(str(a) + " 是个双数")
len()用法
- 返回对象(字符、列表、元组等)元素个数
s = "abcde"
for i in range(len(s)):
print(s[i])