- 给一个半径,求圆的面积和周长
# 求圆面积公式:圆面积(S) = 圆周率(π) * (圆半径(r) ** 2)
# 求圆周长公式:圆周长(C) = 2 * 圆半径(r) * 圆周率(π)
r = int(input('number>>> '))
print('Area = {}'.format(3.14 * r * r))
print('Circumference = {}'.format(3.14 * r * 2))
# ------执行结果-------
number>>> 5
Area = 78.5
Circumference = 31.400000000000002
- 输入两个数,比较大小后,从小到大打印
num1 = int(input('Please input the first num: '))
num2 = int(input('Please input the sencond num: '))
if num1 > num2:
print(num2, num1)
else:
print(num1, num2)
# ------执行结果-------
Please input the first num: 5
Please input the sencond num: 4
4 5
# 三目运算:python没有三目运算符,但是提供了三元表达式: 真值 if 条件 else 假值
num1 = int(input('Please input the first num: '))
num2 = int(input('Please input the sencond num: '))
print(num2, num1) if num1 > num2 else print(num1, num2)
# ------执行结果-------
Please input the first num: 5
Please input the sencond num: 4
4 5
- 依次输入若干个数,打印出最大值,若输入为空则退出程序
num_max = 0
while True:
num = input('Please input a num: ')
if num:
num = int(num)
if num >= num_max:
num_max = num
print(num_max)
else:
print('bye')
break
# ------执行结果-------
Please input a num: 66
66
Please input a num: 55
66
Please input a num: 78
78
Please input a num: 55
78
Please input a num:
bye
- 输出n个数,求每次输入后的算数平均数
sum = 0
counter = 0
while True:
nums = input('Please input num(exit if input empty ): ')
if not nums:
print('Bye~')
break
nums = int(nums)
sum += nums
counter += 1
print("avg is : ", (sum / counter))
# ------执行结果-------
Please input num(exit if input empty ): 22
avg is : 22.0
Please input num(exit if input empty ): 66
avg is : 44.0
Please input num(exit if input empty ): 88
avg is : 58.666666666666664
Please input num(exit if input empty ):
Bye~
- 打印边长为n的正方形
n = int(input("Please input length: "))
for i in range(n):
if i == 0 or i == (n - 1): # 可换成取模:if i % (n - 1) == 0:
print(n * '$')
else:
print('$' + ' ' * (n - 2) + '$' )
# ------执行结果-------
Please input length: 5
$$$$$
$ $
$ $
$ $
$$$$$
- 求100内所有奇数和
sum = 0
for i in range(1,100,2):
sum = sum + i
print(sum)
# ------执行结果-------
2500
- 求1-5的阶乘之和
sum = 0
n = 1
for i in range(1,6):
n *= i
sum += n
print(sum)
# ------执行结果-------
153
- 判断素数
while True:
num = input('Please input a num: ')
if not num: # 输入为空退出
print('Bye~')
break
elif int(num) <= 1: # 排除0和1两个特殊数字
print(num, "is a special number")
else:
num = int(num)
for i in range(2,num): # 对这个数从2测试到num-1
if (num % i) == 0: # 如果被整除,则不是素数,跳出循环
print(num, " is not Prime number")
break
else:
print(num, " is Prime number")
# ------执行结果-------
Please input a num: 0
0 is a special number
Please input a num: 1
1 is a special number
Please input a num: 12577
12577 is Prime number
Please input a num: 88
88 is not Prime number
Please input a num:
Bye~
- 九九乘法表
# 示例1(使用行拼接)
for i in range(1, 10):
line = ""
for j in range(1, i + 1):
line += "{}*{}={:<{}}".format(j, i, j * i, 2 if j == 1 else 3)
print(line)
# 示例2(直接格式化打印)
for i in range(1, 10):
for j in range(1, i + 1):
print("{}x{}={:<{}}".format(j, i, j * i, 2 if j == 1 else 3), end = "\n" if j == i else "")
# ------执行结果-------
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
- 九九乘法表(倒)
for i in range(1, 10):
line = ''
for j in range(i, 10):
line += "" + "{}x{}={:<{}}".format(i, j, i * j, 2 if j < 4 else 3)
print("{:>{}}".format(line, 63), end='\n' if j == 9 else '')
# ------执行结果-------
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9
2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18
3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27
4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36
5x5=25 5x6=30 5x7=35 5x8=40 5x9=45
6x6=36 6x7=42 6x8=48 6x9=54
7x7=49 7x8=56 7x9=63
8x8=64 8x9=72
9x9=81
- 打印菱形
n = 7
m = n // 2
for i in range(-m, n-m):
print(" " * abs(i) + '*' * (n - abs(i) * 2))
# ------执行结果-------
*
***
*****
*******
*****
***
*
- 对顶三角形
n = 7
m = n // 2
for i in range(-m, n - m):
print("{:^{}}".format((abs(i) * 2 + 1) * "*", n))
# ------执行结果-------
*******
*****
***
*
***
*****
*******
- 闪电
n = 7
m = n // 2
l = n - m
for i in range(-m, l):
if i < 0:
print(abs(i) * ' ' + (l - abs(i)) * '*')
elif i > 0:
print(m * ' ' + (l - i) * '*')
else:
print(n * '*')
# ------执行结果-------
*
**
***
*******
***
**
*
- 100以内的斐波那契数列
f1 = 1
fn = 0
for i in range(100):
fn += f1
f1 = fn - f1
if fn > 100: break
print("f({})={}".format(i + 1, fn))
# ------执行结果-------
f(1)=1
f(2)=1
f(3)=2
f(4)=3
f(5)=5
f(6)=8
f(7)=13
f(8)=21
f(9)=34
f(10)=55
f(11)=89
- 斐波那契第101项
num = 101
f1 = 1
fn = 0
for i in range(num):
fn += f1
f1 = fn - f1
if i == (num - 1):
print("f(%s) = %s" % (num, fn))
# ------执行结果-------
f(101) = 573147844013817084101
- 求10W内的素数数量(优化执行时间,可参考每个脚本结果及执行时间)
- 计算脚本执行时长,使用:datetime模块
- 示例1:使用之前判断素数的方式,用时长,不建议使用
import datetime # 导入datetime模块用于计时
start = datetime.datetime.now() #脚本开始时间
num = 100000
count = 0
for i in range(2, num):
for a in range(2, i): # 素数除了1和本身皆不能被整除
if (i % a) == 0: break
else:
# print(i)
count += 1
usetime = (datetime.datetime.now() - start).total_seconds() # 脚本结束时间-开始时间=脚本使用时间
print("count: {}\nuse_time: {}".format(count, usetime))
# ------执行结果-------
count: 9592
use_time: 47.167959
- 示例2:
# 如果一个数n不是素数就是合数,且由两个自然数相乘得到,其中一个大于或等于该数n的平方根,另一个小于该数n的平方根,并成对出现,即:n / n的平方数之前未被整除,则n的平方根之后也不会被整除,所以一个数的最大约数为: n ** 0.5 +1
import datetime
start = datetime.datetime.now()
num = 100000
count = 0
for i in range(2, num):
for a in range(2, int(i ** 0.5) + 1): # 最大约数为(n ** 0.5 + 1)
if (i % a) == 0: break
else:
# print(i)
count += 1
usetime = (datetime.datetime.now() - start).total_seconds()
print("count: {}\nuse_time: {}".format(count, usetime))
# ------执行结果-------
count: 9592
use_time: 0.306311
- 示例3:去除偶数
import datetime
start = datetime.datetime.now()
num = 100000
count = 1 # 舍弃所有偶数,但是2为素数,所以默认计时器+1
for i in range(3, num, 2): # 只有奇数才是质数,所以舍弃所有偶数
for a in range(3, int(i ** 0.5) + 1, 2): # 奇数不能整除偶数,所以被除数偶数去除
if (i % a) == 0: break
else:
# print(i)
count += 1
usetime = (datetime.datetime.now() - start).total_seconds()
print("count: {}\nuse_time: {}".format(count, usetime))
# ------执行结果-------
count: 9592
use_time: 0.157009
- 示例4:利用素数性质,所有大于10的质数中,个位数只有1,3,7,9
import datetime
start = datetime.datetime.now()
num = 100000
count = 1
for i in range(3, num, 2):
if i > 10 and i % 5 == 0: # 所有大于10的质数中,个位数只有1,3,7,9,也就是说,当数字大于5时,当个位数为5时,都能被5整除,能被5整除,说明不是素数
continue
for a in range(3, int(i ** 0.5) + 1, 2):
if (i % a) == 0:
break
else:
# print(i)
count += 1
usetime = (datetime.datetime.now() - start).total_seconds()
print("count: {}\nuse_time: {}".format(count, usetime))
# ------执行结果-------
count: 9592
use_time: 0.155705