##第五章:循环结构
range()函数:用于生成一个整数序列 range()的创建方式:第一种,只有一个参数(小括号中只给一个数)r = range(10)
print® # range(0,9),默认从0开始,步长为1
print(list®) # 用于查看range对象中的整数序列,其中list是列表的意思
r = range(1, 10) # (开始,截止)
print®
print(list®)
r = range(1, 10, 2)
print®
print(list®)
print(10 in r) # 10在r序列里面吗
print(9 in r)
print(10 not in r) # 10不在r序列中
print(9 not in r)
print("-----------while循环结构--------------")
a = 1
while a < 10:
print(a)
a += 1
a = 0
sun = 0 # 用于存储累加和
while a < 5:
sun = sun + a
a += 1
print(sun)
sun = 0
a = 0
while a < 100:
a += 2
sun = a + sun
print(sun)
print("-----------for-in循环--------------")
for item in ‘python’:
print(item)
for i in range(10):
print(i)
for _ in range(10):
print(“人生苦短,我用python”)
sum = 0
for s in range(1, 101):
if s % 2 == 0:
sum += s
print(sum)
for item in range(100, 1000):
ge = item % 10
shi = item // 10 % 10
bai = item // 100
if ge ** 3 + shi ** 3 + bai ** 3 == item:
print(item)
for item in range(3):
pwp = input(“请您输入密码:”)
if pwp == “123456”:
print(“输入正确”)
break
else:
print(“输入错误,请重新输入”)
print(“输入次数过多,稍后重试”)
break:退出这一次循环 continue:结束当前循环,进入下一次循环a = 0
while a < 3:
pwd = input(“请输入密码:”)
if pwd == “888”:
print(‘密码正确!’)
break
else:
print(“密码输入错误”)
a += 1
for item in range(1, 51):
if item % 5 != 0:
continue
print(item)
for i in range(1, 10): # 行数
for j in range(1, i + 1): # 列数
print(i, ‘*’, j, ‘=’, i * j, end="t")
print()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)