嵩天python学习笔记
1.温度转换
TempStr = input("请输入带有符号的温度值:")
if TempStr[-1] in ["C","c"]:
F = 1.8*eval(TempStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
elif TempStr[-1] in ["F","f"]:
C = (eval(TempStr[0:-1]) - 32 )/1.8
print("转换后的温度是{:.2f}C".format(C))
else:
print("输入格式错误")
请输入带有符号的温度值:32f
转换后的温度是0.00C
2.Turtle库
import turtle
turtle.setup(650, 350,)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
turtle.circle(40, 80)
turtle.circle(-40, 80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.done()
3.文本进度条
import time as t
scale = 10
print("- - - - - -执行开始- - - - - -")
for i in range(scale+1):
c = (i/scale) * 100
b = '.' * (scale - i)
a = '*' * i
print("{:^3.0f}%[{}->{}]".format(c,a,b))
t.sleep(0.1)
print("- - - - - -执行结束- - - - - -")
- - - - - -执行开始- - - - - -
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
- - - - - -执行结束- - - - - -
import time as t
for i in range(101):
print("\r{:3}%".format(i),end="")
t.sleep(0.1)
100%
import time
scale = 50
print("执行开始".center(scale//2, "-"))
start = time.perf_counter()
for i in range(scale+1):
a = '*' * i
b = '.' * (scale - i)
c = (i/scale)*100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
time.sleep(0.1)
print("\n"+"执行结束".center(scale//2,'-'))
-----------执行开始----------
100%[**************************************************->]5.07s
-----------执行结束----------
4.蒙特卡罗方法圆周率的计算
from random import random
from time import perf_counter
DARTS = 1000*1000
hits = 0.0
start = perf_counter()
for i in range(1,DARTS+1):
x,y = random(),random()
dist = pow(x ** 2 + y ** 2,0.5)
if dist <= 1.0:
hits = hits + 1
pi = 4*(hits/DARTS)
print("圆周率值是:{}".format(pi))
print("运行时间是:{:.5f}s".format(perf_counter()-start ))
圆周率值是:3.143016
运行时间是:1.19607s
5.七段数码管的绘制
import turtle
import time
def drawGap():
turtle.penup()
turtle.fd(5)
def drawLine(draw):
drawGap()
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()
turtle.right(90)
def drawDigit(digit):
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def drawDate(date):
turtle.pencolor('red')
for i in date:
if i == '-':
turtle.write('年',font=("Arial",26,"normal"))
turtle.fd(40)
turtle.pencolor('green')
elif i == '=':
turtle.write('月',font=("Arial",26,"normal"))
turtle.fd(40)
turtle.pencolor('blue')
elif i == '+':
turtle.write('日',font=("Arial",26,"normal"))
else:
drawDigit(eval(i))
def main():
turtle.setup(800,350)
turtle.penup()
turtle.fd(-330)
turtle.pensize(5)
drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
6.科赫雪花的绘制
import turtle
def koch(size,n):
if n == 0:
turtle.fd(size)
else:
for angle in [0,60,-120,60]:
turtle.left(angle)
koch(size/3,n-1)
def main():
turtle.setup(600,600)
turtle.penup()
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(2)
level = 3
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
turtle.hideturtle()
main()
7.基本统计值计算
def getNum():
nums = []
iNumStr = input("请输入数字(回车退出): ")
while iNumStr != "":
nums.append(eval(iNumStr))
iNumStr = input("请输入数字(回车退出): ")
return nums
def mean(numbers):
s = 0.0
for num in numbers:
s = s + num
return s / len(numbers)
def dev(numbers, mean):
sdev = 0.0
for num in numbers:
sdev = sdev + (num - mean)**2
return pow(sdev / (len(numbers)-1), 0.5)
def median(numbers):
sorted(numbers)
size = len(numbers)
if size % 2 == 0:
med = (numbers[size//2-1] + numbers[size//2])/2
else:
med = numbers[size//2]
return med
n = getNum()
m = mean(n)
print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n,m),median(n)))
请输入数字(回车退出): 1
请输入数字(回车退出): 2
请输入数字(回车退出): 3
请输入数字(回车退出): 4
请输入数字(回车退出): 5
请输入数字(回车退出): 6
请输入数字(回车退出): 7
请输入数字(回车退出): 8
请输入数字(回车退出): 9
请输入数字(回车退出):
平均值:5.0,方差:2.7,中位数:5.