py各种简单代码
1.
# 编程实现兔子序列
//输出一定范围内的斐波那契数列
a=0
b=1
while b<100:
print(b, end=",")
a,b=b,a+b
//输出一定循环次数的斐波那契数列
a=0
b=1
for i in range(10):
print(b)
a,b=b,a+b
2.列表输出
list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]
tinylist = [123, 'runoob']
print(list) # 输出列表
print(list[0]) # 输出列表第一个元素
print(list[1:2]) # 输出列表第二个和第三个元素
print(list[2:]) # 输出列表第三个到最后
print(list*2) # 输出两次列表
print(list,tinylist) # 两个列表并列输出
3.利用formate实现乘法口诀和普通不完美算法
a=1
b=1
while a<10:
b=1
while b<=a:
c=a*b
print(a,"*",b,"=",c,end=" ")
b=b+1
print(" ")
a=a+1
for x in range(1,10):
print("\n");
for y in range(1,x+1):
print("{0}*{1}={2}\t".format(y,x,x*y),end=" ")
4.四色圆
import turtle as t
c=0
t.speed(20)
t.pensize(1)
t.bgcolor("black")
while c<23:
t.penup()
t.goto(0,0)
t.pendown()
t.pencolor("red")
t.forward(100)
t.left(91)
t.penup()
t.goto(0, 0)
t.pendown()
t.pencolor("yellow")
t.forward(100)
t.left(91)
t.penup()
t.goto(0, 0)
t.pendown()
t.pencolor("purple")
t.forward(100)
t.left(91)
t.penup()
t.goto(0, 0)
t.pendown()
t.pencolor("blue")
t.forward(100)
t.left(91)
c=c+1
t.done()
# 个人理解是我每次旋转相对上次增加一度,所以如果欲增加多少度,就旋转几次,
# 此处我旋转23,但是每次增加4度,所以23*4=90度,所以每个边旋转九十度
5.圆中圆
import turtle as t
t.speed(15) #设置笔速,最高10,最低1
t.pensize(1)
t.pencolor("red") #设置笔色
i=302
while i>0: #循环条件
t.goto(0,-i)
while i%4==0:
t.fillcolor("red")
t.pendown()
t.begin_fill()
t.circle(i)
t.end_fill()
i = i - 6
t.penup()
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(i)
t.end_fill()
i = i - 6
t.penup()
t.done()
2.
import turtle as t
t.speed(15) #设置笔速,最高10,最低1
t.pensize(1)
t.pencolor("red") #设置笔色
i=302
while i>0: #循环条件
while i%4==0:
t.fillcolor("red")
t.goto(0, -i)
t.pendown()
t.begin_fill()
t.circle(i)
t.end_fill()
i = i - 6
t.penup()
t.goto(0,-i)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(i)
t.end_fill()
i = i - 6
t.penup()
t.done()
6.整数排序
l=[]
for i in range(3):
x=int(input("请输入整数: "))
l.append(x)
l.sort()
print(l)
7.输出正四边形,五边形,六边形,八边形,圆
import turtle as t
t.pencolor("red")
t.fillcolor("red")
t.penup()
t.goto(-300,100)
t.begin_fill()
t.pendown()
i=0
while i<3:
t.forward(100)
t.left(120)
i=i+1
t.end_fill()
t.penup()
t.goto(0,100)
t.begin_fill()
t.pendown()
i=0
while i<4:
t.left(90)
t.forward(90)
i=i+1
t.end_fill()
t.penup()
t.goto(100,100)
t.begin_fill()
t.pendown()
i=0
while i<5:
t.forward(80)
t.left(72)
i=i+1
t.end_fill()
t.penup()
t.goto(-300,-100)
t.begin_fill()
t.pendown()
i=0
while i<6:
t.forward(70)
t.left(60)
i=i+1
t.penup()
t.end_fill()
t.goto(-100,-100)
t.pendown()
t.begin_fill()
i=0
while i<8:
t.forward(60)
t.left(45)
i=i+1
t.end_fill()
t.penup()
t.goto(100,-100)
t.begin_fill()
t.pendown()
t.circle(50)
t.end_fill()
t.penup()
t.done()
7.绘制五角星
两种方法
# 方法一
import turtle as t # 导入模块
t.pencolor("red") # 幕布颜色
t.fillcolor("red") # 填充颜色
t.begin_fill()
i=0
while i<5:
t.forward(100) # 直行线段长度
t.right(144) # 线条折线角度
i=i+1
# if abs(t.pos())<1:
# break
t.end_fill()
#方法2
import turtle as t # 导入模块
t.pencolor("red") # 幕布颜色
t.fillcolor("red") # 填充颜色
t.begin_fill()
while True:
t.forward(100) # 直行线段长度
t.right(144) # 线条折线角度
if abs(t.pos())<1:
break
t.end_fill()
8.输出三角形
# 第一种 直角三角形
print("直角三角")
for x in range(1,9):
print(" ")
for y in range(1,x+1):
print("*",end="")
print(" ")
# 第二种 对称三角形
print("正三角")
a=10
while a>0:
b=11-a
c=1
while c<a//2:
print(" ",end=" ")
c=c+1
while b>0:
print("*",end=" ")
b=b-1
a=a-2
print(" ")
# 倒三角
print("倒三角")
a=10
while a>0:
b=a-1
c=a
while c<10:
print(" ",end="")
c=c+1
while b>0:
print("*",end=" ")
b=b-1
a=a-2
print(" ")
任课老师 汪峰