输出从1加到100的结果、打印100以内的质数、计算一个文件中的每个英文单词出现的次数

1、输出从1加到100的结果:

count = 0
for i in range(1,101):
    count = count + i
print(count)

2、打印100以内的质数

def zhishu(x):
    for i in range(2,x):
        if x % i == 0:
            return False
    return True
for i in range(2,101):
    if zhishu(i):
        print(i)

3、计算一个文件中的每个英文单词出现的次数

f = open("C:/Users/20324/Desktop/untitled/练习/a.txt")
line = f.readlines()
count = {}
for l in line:
    tokens = l.strip().split(' ')
    for t in tokens:
        if t not in count:
            count[t] = 0
        count[t] += 1
for word in count:
    print(word,count[word])
原文地址:https://www.cnblogs.com/lyali/p/11691814.html