1.计算圆的面积
radius = 25
area = 3.1415 * radius * radius
print("{:.2f}".format(area))
2.简单的人名对话(切片使用)
name = input("inptu your name:")
print("{}同学,学好python,前途无量".format(name))
print("{}同学,学好python,前途无量".format(name[0]))
print("{}同学,学好python,前途无量".format(name[0:4]))
print("{}同学,学好python,前途无量".format(name[3:5]))
3.斐波那契数列计算
a,b = 0,1
while a<1000:
print(a,end=',')
a,b = b,a+b
4.同切圆的绘制
import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(20)
turtle.circle(40)
turtle.circle(60)
turtle.circle(80)
turtle.circle(100)
turtle.circle(120)
turtle.circle(140)
turtle.circle(160)
turtle.circle(180)
5.日期和时间输出
from datetime import datetime
now = datetime.now()
print(now)
now.strftime("%x")
now.strftime("%x")
6.太阳花绘制
from turtle import *
color('red','yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
7.菜谱组合
dite= ['西红柿','花椰菜','黄瓜','牛排','虾仁']
for x in range(0,5):
for y in range(0,5):
if not(x==y):
print("{}{}".format(dite[x], dite[y]))
8.求1!+2!+3!+…+10!
sum,tmp=0,1
for i in range(1,11):
tmp=tmp*i
sum+=tmp
print("运算结果为:{}".format(sum))
9.九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print("{}*{} = {:2}\t".format(j,i,j*i),end=' ')
print(' ')
10.温度单位转换
def tempConvert(ValueStr):
if TempStr[-1] in ['F','f']:
C = (eval(TempStr[0:-1])-32)/1.8
print("转换后的温度:{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
F = (eval(TempStr[0:-1]))*1.8+32
print("转换后的温度:{:.2f}F".format(F))
else: print("输入格式有错")
TempStr = input("请输入带有符号的温度值:")
tempConvert(TempStr)
11画蛇(turtle的使用)
import turtle
def drawSanke(radius,angle,length):
turtle.seth(-40)
for i in range(length):
turtle.circle(radius,angle)
turtle.circle(-radius,angle)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.setup(650,350,200,200)
#turtle.fd(250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
drawSanke(40,80,4)
turtle.done()
12.turtle的使用
import turtle
turtle.pendown()
turtle.forward(50)
turtle.seth(50)
turtle.forward(50)
turtle.circle(40)
13.turtle的使用(三)
"""import turtle
turtle.color('orange')
turtle.pensize(3)
turtle.circle(75)
turtle.penup()
turtle.goto(0, -200)
turtle.pendown()
turtle.circle(100)
turtle.done()
"""
import turtle
turtle.color('purple')
for i in range(10):
turtle.speed(5)
turtle.circle(i*20)
turtle.pensize(2)
turtle.left(126)#逆时针移动126度
turtle.clear()#清空turtle窗口,但是turtle的位置和状态不会改变
turtle.done()
14.正方式螺旋线
import turtle as t
t.pen(speed=0) #加快绘图速度
t.penup()
t.goto(-200, -200) #以左下角某处为起点
t.pendown()
t.seth(0)
length = 400
while (length !=0): #利用正方形螺旋线的性质来绘图
t.fd(length)
t.left(90)
length -= 2.5
t.hideturtle() #绘图结束后把海龟头(笔触头)隐藏起来
t.done() #绘图结束后使窗口停留
15.同心圆绘制
from turtle import *
setup(600,400,500,200)
color("red")
pensize(2)
circle(20)
pensize(2)
for i in range(1,9):
penup()
goto(0,-10*i)
pendown()
circle(20+i*10)
done()
16.工作效益计算
import math
dayup = math.pow((1+0.01),365)
daydown = math.pow((1-0.01),365)
print("一年以后的工作效益")
print("天天坚持做的效益:")
print("up:{:.2f}\tdown:{:.2f}".format(dayup,daydown))
dayup1,dayfactor=1.0,0.01
for i in range(1,366):
if i%7==1 or i%7==2 or i%7==3 or i%7==4 or i%7==5:
dayup1 = dayup1*(1+dayfactor)
else:
dayup1 = dayup1*(1-dayfactor)
print("工作日学习,周末休息")
print("up: {:.6f}".format(dayup1))
17.凯撒密码转换(明文)
plaincode = input("请输入明文: ")
for p in plaincode:
if ord("a") <= ord("p") <= ord("z"):
print(chr(ord("a")+(ord(p)-ord("a")+3)%26),end = '')
else:
print(p,end='')
#ord()函数把单字符转换成为unicode编码
#chr()把unicode编码转换成为单字符
18.显示进度条
import time
scale = 10
print("开始执行".center(scale//2,'*'))
t = time.clock()
#print("{0:*^20}".format('开始执行'))
for i in range(scale+1):
a = (i/scale)*100
b, c = '**'*i,'--'*(scale-i)
t -= time.clock()
print("%{:^3.0f}\t||{}》》{}{:.2f}".format(a,b,c,-t))
time.sleep(0.05)
print("{0:*^20}".format('执行结束'))
20.利用随机分布计算π值
from random import random
from math import sqrt
from time import clock
DATAS = 10000
hits = 0
#clock()
for i in range(1,DATAS+1):
x = random()
y = random()
s = x**2 + y**2
if s<=1:
hits = hits + 1
pi = 4* (hits/DATAS)
print("pi的值是: {:8f}".format(pi))
#print("运行时间:{}".format(clock()))
21.单行动态刷新(IDLE本身屏蔽了单行刷新功能)
import time
for i in range(101):
print("\r{:2}%".format(i), end="")
time.sleep(0.05)