#五角数
def getPentagonalNumber(n):
sum = 0
for i in range(1,n+1):
num = i * (3 * i - 1) / 2
print(int(num),end = " ")
sum += 1
if sum %10 == 0:
print("\n")
getPentagonalNumber(100)
运行结果:
1 5 12 22 35 51 70 92 117 145
176 210 247 287 330 376 425 477 532 590
651 715 782 852 925 1001 1080 1162 1247 1335
1426 1520 1617 1717 1820 1926 2035 2147 2262 2380
2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
3876 4030 4187 4347 4510 4676 4845 5017 5192 5370
5551 5735 5922 6112 6305 6501 6700 6902 7107 7315
9801 10045 10292 10542 10795 11051 11310 11572 11837 12105
12376 12650 12927 13207 13490 13776 14065 14357 14652 14950
#求一个整数各个数字的和
def sumDigits(n):
ge = int(n) % 10
c = 0
for i in range(len(str(n))):
bai = int(n) // (10 * (10**i)) % 10
c += bai
sum = c+ge
print("这个整数各个数字的和是%d"%sum)
sumDigits(23444)
运行结果:这个整数各个数字的和是17
#对三个数字排序
def display(num1,num2,num3):
list = [num1,num2,num3]
s = sorted(list)
print("The sorted numbers are {}".format(s))
if __name__ == '__main__':
a,b,c = map(int, input("Enter three number:").split(","))
display(a,b,c)
运行结果:
Enter three number:2,5,3
The sorted numbers are [2, 3, 5]
#计算未来投资值
from prettytable import PrettyTable
list = []
def futureInvestmentValue(inAmount,rate,years):
for i in range(1,years+1):
futureInvestment = inAmount + ((1 + rate) ** (12 * i))
list.append([i,futureInvestment])
table = PrettyTable(['year','Future Value'])
for row in list:
table.add_row(row)
print(table)
if __name__ == '__main__':
inAmount = int(input("The amount invested:"))
rate = float(input("Annual interest rate:"))/12
futureInvestmentValue(inAmount,rate,years=30)
运行结果:
The amount invested:1000
Annual interest rate:0.05
+------+--------------------+
| year | Future Value |
+------+--------------------+
| 1 | 1001.0511618978817 |
| 2 | 1001.1049413355584 |
| 3 | 1001.1614722313335 |
| 4 | 1001.2208953550254 |
| 5 | 1001.2833586785035 |
| 6 | 1001.3490177441588 |
| 7 | 1001.4180360522261 |
| 8 | 1001.4905854679226 |
| 9 | 1001.5668466494164 |
| 10 | 1001.6470094976903 |
| 11 | 1001.7312736294214 |
| 12 | 1001.8198488740552 |
| 13 | 1001.9129557963097 |
| 14 | 1002.0108262454128 |
| 15 | 1002.1137039324385 |
| 16 | 1002.2218450371822 |
| 17 | 1002.3355188460836 |
| 18 | 1002.4550084227877 |
| 19 | 1002.5806113130132 |
| 20 | 1002.7126402854819 |
| 21 | 1002.8514241107576 |
| 22 | 1002.9973083799298 |
| 23 | 1003.1506563651839 |
| 24 | 1003.3118499243998 |
| 25 | 1003.4812904520315 |
| 29 | 1004.2502913423797 |
| 30 | 1004.4677443140062 |
+------+--------------------+
#显示字符
def printChars():
for i in range(73,91):
print(chr(i),end=" ")
if i%9==0:
print("\n")
printChars()
运行结果:
I J K L M N O P Q
R S T U V W X Y Z
#一年的天数
def numberDay(year):
for ye in range(year,year+11):
if ye % 4 == 0 and ye % 100 != 0 or ye % 400 == 0:
print("{}年有366天".format(ye))
else:
print("{}年有365天".format(ye))
numberDay(2010)
运行结果:
2010年有365天
2011年有365天
2012年有366天
2013年有365天
2014年有365天
2015年有365天
2016年有366天
2017年有365天
2018年有365天
2019年有365天
2020年有366天
#两点之间的距离
def distance(x1,x2,y1,y2):
dis = ((x2-x1)**2 + (y2-y1)**2) ** 0.5
print("这两个点的距离是:%f"%dis)
distance(1,4,4,2)
运行结果:这两个点的距离是:3.605551
#梅素数
from prettytable import PrettyTable
def mei(p):
c = []
b = []
for p in range(1,32):
if p > 1:
for i in range(2,p):
if p != 2 and (p % i) == 0:
#print(p,"不是质数")
#print(i,"乘于",p//i,"是",p)
break
else:
#print(p,"是质数")
d = 2**(p-1)
c.append([p,d])
for x in c:
if x not in b:
b.append(x)
table = PrettyTable(['p','2**(p-1)'])
for row in b:
table.add_row(row)
print(table)
mei(5)
运行结果:
+----+------------+
| p | 2**(p-1) |
+----+------------+
| 3 | 4 |
| 5 | 16 |
| 7 | 64 |
| 9 | 256 |
| 11 | 1024 |
| 13 | 4096 |
| 15 | 16384 |
| 17 | 65536 |
| 19 | 262144 |
| 21 | 1048576 |
| 23 | 4194304 |
| 25 | 16777216 |
| 27 | 67108864 |
| 29 | 268435456 |
| 31 | 1073741824 |
+----+------------+
#显示当前本地时间和日期
import time
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为 :", localtime)
运行结果:本地时间为 : Mon Aug 19 23:09:23 2019
#矩形周长面积
class Circle(object):
def __init__(self,width=1,height=2):
self.width = width
self.height = height
def getArea(self):
S = self.width * self.height
print("矩形的面积是:{}".format(S))
def getPerimeter(self):
C = (self.width + self.height) *2
print("矩形的周长是:{}".format(C))
if __name__ == '__main__':
ynn = Circle()
ynn.getArea()
ynn.getPerimeter()
ynn1 = Circle(width=4,height=40)
ynn1.getArea()
ynn1.getPerimeter()
ynn2 = Circle(width=3.5,height=35.7)
ynn2.getArea()
ynn2.getPerimeter()
运行结果:
矩形的面积是:2
矩形的周长是:6
矩形的面积是:160
矩形的周长是:88
矩形的面积是:124.95000000000002
矩形的周长是:78.4
#风扇
class Fan():
def __init__(self,speed=1,on=False,radius=5.0,color="blue"):
self.__speed = int(speed)
self.__on = bool(on)
self.__radius = float(radius)
self.__color = str(color)
@property
def speed(self):
return self.__speed
@speed.setter
def speed(self,new_speed):
if self.__speed == 1:
self.__speed=new_speed = "SLOW"
elif self.__speed == 2:
self.__speed=new_speed = "MEDIUM"
else:
self.__speed=new_speed = "FAST"
@property
def on(self):
return self.__on
@property
def radius(self):
return self.__radius
@property
def color(self):
return self.__color
def fans(self):
print("{}颜色的风扇半径为{},速度{},打开状态{}".format(self.__color,self.__radius,self.__speed,self.__on))
if __name__ == '__main__':
ynn = Fan()
ynn.fans()
ynn1 = Fan(3,on=True,radius=10.0,color="yellow")
ynn1.fans()
ynn1 = Fan(2,on=False,radius=5.0,color="blue")
ynn1.fans()
运行结果:
blue颜色的风扇半径为5.0,速度1,打开状态False
yellow颜色的风扇半径为10.0,速度3,打开状态True
blue颜色的风扇半径为5.0,速度2,打开状态False