循環
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))
List of articles 0 Preface 1 S