目录

​输出1~100所有数字​

​输出1~100所有数字的和​

​输出1~100之间所有奇数​

​输出1~100之间所有偶数​

​输出1~100之间所有质数​

​输出1~100之间所有质数的和​

​输出1~100所有合数​

​输出圆周率与自然常数e​


输出1~100所有数字

for i in range(1,101):
print(i,end = ' ')

输出1~100所有数字的和

sumNumber = 0
for i in range(1,101):
sumNumber += i
print('1~100之间所有数字之和是:',sumNumber)
print('1~100之间所有数字之和是:',sum(range(1,101)))

输出1~100之间所有奇数

numbers = []
for i in range(1,101):
if (i % 2) == 1:
numbers.append(i)
print(numbers)

输出1~100之间所有偶数

numbers = []
for i in range(1,101):
if (i % 2) == 0:
numbers.append(i)
print(numbers)

输出1~100之间所有质数

def prime(maxNumber):
numbers = []
min = 2
while min <= maxNumber:
is_prime = True
for i in range(2,min):
if (min % i) == 0:
is_prime = False
break
if is_prime == True:
numbers.append(min)
min += 1
print('{0} 以内的质数有:'.format(maxNumber))
print(numbers)
print()

prime(100)

输出1~100之间所有质数的和

# coding : utf-8
def prime(maxNumber):
numbers = []
min = 2
numberSum = 0
while min <= maxNumber:
is_prime = True
for i in range(2,min):
if (min % i) == 0:
is_prime = False
break
if is_prime == True:
numbers.append(min)
min += 1
for i in numbers:
numberSum += i
print('%s以内的质数和是:%s' % (maxNumber,numberSum))

prime(100)

输出1~100所有合数

maxNumber = 100
numbers = []
min = 2
numberSum = 0
for i in range(1,101):
numbers.append(i)

while min <= maxNumber:
is_prime = True
for i in range(2,min):
if (min % i) == 0:
is_prime = False
break
if is_prime == True:
numbers.remove(min)
min += 1
print(numbers)

输出圆周率与自然常数e

from math import pi
from math import e

print('圆周率:',pi)

print('自然常数:',e)

希望能对入门的Python小伙伴有点帮助。