循环
range(start,end,step)
range()函数创建序列,包括下限,不包括上限
例如
range(101),范围0~100
range(1,101),访问1~100
sum = 0
for i in range(1,101):
sum += i
print(sum)
sum = 0
for i in range(1,101):
sum += i
print(sum)
# 打印99乘法表
for i in range(1, 10): # 控制行
for j in range(1, i + 1): # 控制列
print('{}*{}={}'.format(j, i, i * j), sep='', end='\t')
print()
# 求200以内能被17整除的最大正整数
for i in range(200, 1, -1):
if i % 17 == 0:
break
print('200以内能被17整除的最大正整数是{}'.format(i))
# 求1~100以内所有偶数和
sum = 0
for i in range(1, 101):
if i % 2 == 0:
sum += i
else:
continue
print(sum)
# 方法1
num = 0
for i in range(2, 200):
k = True
for j in range(2, i):
if(i % j == 0):
k = False
break
if(k==True):
print(i)
num += 1
print('200以内的素数有{}个'.format(num))
# 方法2
import math
num = 0
for i in range(2, 200):
m = int(math.sqrt(i))
k = True
for j in range(2, m + 1):
if(i % j == 0):
k = False
break
if(k):
print(i)
num += 1
print('200以内的素数有{}个'.format(num))