# i 临时变量, 不用提前声明
# range(start, end, step)
# step 步长默认是 1
# [start, end)
# for i in range(2, 19, 2):
# print(i)
mySum = 0
for i in range(1, 101):
mySum += i
print(mySum)
mySum = 0
for i in range(1, 101, 2):
mySum += i
print(mySum)
mySum = 0
for i in range(2, 101, 2):
mySum += i
print(mySum)
for 循环中的break和continue
# 当累加和类加到 大于1000 时结束循环
mySum = 0
for i in range(1, 101):
if mySum >1000:
break
mySum += i
print(mySum)
# 求 1 ~ 100 之间 所有奇数的累加和
mySum = 0
for i in range(1, 101, 2):
if i%2==0:
continue
mySum += i
print(mySum)
循环嵌套
循环里面还是循环:
外层循环负责多少行, 里层循环负责具体每一行
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
for i in range(5):
for j in range(6):
print("* ", end='\t') # , end='' 不换行 \t tab
print()
*
* *
* * *
* * * *
* * * * *
* * * * * *
for i in range(6):
for j in range(i+1):
print("* ", end='\t') # , end='' 不换行 \t tab
print()
i = 1
while i <= 9:
j = 1
while j < i+1:
print("{} x {} = {}".format(j, i, i*j), end='\t')
j += 1
i += 1
print()
for i in range(1, 10):
for j in range(1, i+1):
print("{} x {} = {}".format(j, i, i * j), end='\t')
print()
列表
# list 列表 存储不同类型数据的容器
hero_list = ["小鲁班", "妲己", "安琪拉", "后羿", 11, 99.9, ["haha", "hehe"]]
print(hero_list)
print(type(hero_list)) # <class 'list'>
# 列表的操作
# 访问 单个 index 索引 从0开始
print(hero_list[0])
print(hero_list[2])
# 修改
hero_list[4] = "赵云"
hero_list[5] = "刘备"
print(hero_list)
# 增加
# 末尾增加
hero_list.append("云樱")
hero_list.append("澜")
# 指定索引增加
hero_list.insert(1, "铠")
print(hero_list)
# 删除
# del hero_list
# 删除单个
del hero_list[7]
del hero_list[0]
print(hero_list)