1.画图操作
**
(1)画正方形
**
import turtle #导入
turtle.title("画正方形")
turtle.pensize(5) #画笔大小为5
turtle.pencolor("red") #画笔颜色为红
turtle.fillcolor("green") #填充颜色为绿
turtle.begin_fill() #开始填充
for i in range(4): #循环四次
turtle.forward(200) #前进200
turtle.left(90) #左转90度
turtle.end_fill() #结束填充
turtle.done() #导入结束
#turtle.mainloop()
**
(2)画蛇
**
import turtle # 导入turtle库
pen: object = turtle.Pen()
pen.pensize(50) # 画笔大小
pen.pencolor("purple") # 画笔颜色
pen.setheading(-40)
pen.circle(40, 80) # 参数表示半径
pen.circle(-40, 80)
pen.circle(40, 80)
pen.circle(-40, 80)
pen.setheading(0)
pen.forward(30) #向前30
pen.circle(30, 180)
pen.dot(8, "white") # 加点大小8,颜色白
pen.begin_fill()
turtle.done()
(3)画丘比特爱心
from turtle import*
color ("black","red")
pensize(5)
begin_fill()
penup()
goto(50,50)
pendown()
right(45)
goto(100,0)
left(90)
fd(120)
circle(50,225)
penup()
goto(0,0)
pendown()
left(135)
fd(120)
circle(50,225)
seth(90)
circle(50,225)
fd(121)
end_fill()
left(56)
penup()
goto(-210,40)
pendown()
goto(0,80)
penup()
goto(160,110)
pendown()
goto(320,140)
done()
**
(4)画数码数字8
import turtle
size = 10
line = 40
# 拿出笔
pen = turtle.Pen()
pen.pensize(size)
pen.pencolor("blue")
turtle.tracer(False) # 不跟踪动画
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.left(90)
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.left(90)
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.left(90)
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.left(90)
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.left(90)
pen.penup()
pen.forward(size)
pen.pendown()
pen.forward(line)
pen.penup()
pen.forward(size)
pen.pendown()
pen.hideturtle() #隐藏
turtle.tracer(True) # 回复效果
turtle.done() #结束
(5)画同心圆
import turtle
pen = turtle.Pen()
pen.dot(8, "#000000")
pen.write("圆心", font=("宋体", 16, "bold")) # 画圆心
pen.penup() # 抬笔
pen.goto(0, -100)
pen.pendown() # 落笔
pen.circle(100)
pen.penup() # 抬笔
pen.goto(0, -200)
pen.pendown() # 落笔
pen.circle(200)
turtle.done()
(6)画五角星
import turtle # 导入turtle
pen: object = turtle.Pen()
pen.pensize(5) # 画笔大小
pen.pencolor("red") # 画笔颜色
pen.fillcolor("green") # 填充颜色
pen.begin_fill() #开始
for i in range(5): # 5次循环
pen.forward(200)
pen.right(144)
pen.end_fill() # 结束填充
pen.done() # 结束
(7)画四个圆
import turtle # 导入turtle
turtle.title("画圆")
pen = turtle.Pen()
pen.pencolor("red")
pen.pensize(1)
pen.circle(100)
pen.pencolor("yellow")
pen.circle(-100)
pen.setheading(90)
pen.pencolor("blue")
pen.circle(100)
pen.pencolor("purple")
pen.circle(-100)
turtle.done
2.打印杨辉三角
**
杨辉三角形 # 1 # 1 1 # 1 2 1 # 1 3 3 1 # 1 4 6 4 1 # 1 5 10 10 5 1
yh = []
n = int(input("请输入杨辉三角形的行数:"))
for i in range(n):
yh.append([1]*(i+1))
for i in range(2, n):
for j in range(1, i):
yh[i][j] = yh[i-1][j-1]+yh[i-1][j]
for i in range(len(yh)):
print(" "*(n-1-i), end="")
for j in range(len(yh[i])): # 控制列
print(yh[i][j], " "*2, end="")
print()
**
3.求水仙花数
**
i = int(input("100到一个数字范围:"))
for num in range(100, i):
gewei = num % 10
shiwei = num // 10 % 10
baiwei = num//100
if gewei**3 + shiwei**3 + baiwei**3 == num:
print(num)
**
4.在100~n中查找10个回文数
**
# 找出100~999间的10个回文数
count = 0
n = int(input("在100~n中寻找回文数,请输入n: "))
for num in range(100, n+1):
baiWei = num//100
geWei = num % 10
if baiWei == geWei:
print(num)
count += 1
# 删去以下两行,则输出所有回文数
if count == 10:
break
**
5.按行列打印出二维列表
**
arr = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
for i in range(len(arr)): # 控制行,0~2
for j in range(len(arr[i])): # 控制列
print(arr[i][j], end='\t')
print()
**
6.打印蛇形矩阵
**
import pprint
import numpy as np
N = int(input('请输入蛇形矩阵的行数:'))
myarray = np.zeros((N, N), dtype=np.int16)
x, y = 0, N - 1
res = myarray[x][y] = 1
while res < N * N:
while x + 1 < N and (not myarray[x + 1][y]):
res += 1
x += 1
myarray[x][y] = res
while y - 1 >= 0 and not myarray[x][y - 1]:
res += 1
y -= 1
myarray[x][y] = res
while x - 1 >= 0 and not myarray[x - 1][y]:
res += 1
x -= 1
myarray[x][y] = res
while y + 1 < N and not myarray[x][y + 1]:
res += 1
y += 1
myarray[x][y] = res
pprint.pprint(myarray)
print(myarray)
**
7.求1~n的阶乘的累加和
**
sum = 0
shu = int(input("请输入所求的一个数阶乘的累加和: "))
for i in range(1, shu+1):
s = 1
for j in range(2, i+1):
s *= j
sum += s
print("1!+2!+.....+%d!=%d" % (shu, sum))
8.二分查找一个数
# 一维列表
# 条件:起始位置小于等于结束位置
nums = [4, 1, 5, 10, -1, 9, 3, 2, 13, 7, ]
print("原来的列表为:", nums)
nums.sort() # 进行排序
print("列表排序后为:", nums)
num = int(input("请输入你要查找的数:")) # 需要查找的数
pos = -1 # 默认需要查找的数的位置为-1
start = 0
end = len(nums)-1
while start <= end:
mid = (start+end)//2
if num == nums[mid]: # 如果要找的数等于中间的这个数
pos = mid # 那么位置就为中间这个数的位置
break
elif num < nums[mid]: # 若要找的数小于中间
end = mid - 1 # 中间减一
else: # 若要找的数大于中间,则中间加一
start = mid + 1
# 判断是否找到
if pos != -1: # 若找到了
print("%d位置是%d" % (num, pos))
else: # 若没找到
print("没找到%d" % num)
9.选择排序法
# 选择排序法
nums = [4, 1, 5, 10, -1, 9, 3, 2, 13, 7]
count = len(nums) # count等于nums的长
for i in range(count-1):
min = i
for j in range(i+1, count): # 将剩下的进行遍历,遍历到count
if nums[min] > nums[j]:
min = j
if min != i: # 若最小值不等于i,进行交换
t = nums[i]
nums[i] = nums[min]
nums[min] = t
print(nums)
10.冒泡排序法
# 冒泡排序法
nums = [2, 10, 2, 6, 55, 19, 10, -10, 8, 16]
count = len(nums) # count等于nums的长
for i in range(count):
for j in range(count-i-1): # 若不减1,就会出现越界问题
if nums[j] > nums[j+1]: # 相邻元素比较
t = nums[j]
nums[j] = nums[j+1]
nums[j+1] = t # 若前一个大于这个元素,则进行交换
print(nums)
11.生成n注36选7的彩票
import random
tickets = []
for i in range(5): # 产生5注彩票,循环5次
tic = [0]*7 # 一维,只可追加元素
tic[0] = random.randint(1, 36)
for j in range(1, 7):
tic[j] = random.randint(1, 36)
k = 0
while k < j: # 比较j之前的元素
if tic[j] == tic[k]:
tic[j] = random.randint(1, 36) # 一旦和之前相等则重新产生
k = 0
else:
k += 1
tic.sort() # 排序
tickets.append(tic) # 变成二维,5行7列
# 输出二维列表
sum = 0
for m in range(len(tickets)): # 5行
for n in range(len(tickets[i])): # 列
print(tickets[m][n], end="\t")
print()
import random
nums = [0]*7 # 含有7个0元素的列表
nums[0] = random.randint(1, 36) # 产生1~36的随机值,赋给列表元素
for i in range(1, 7): # i从1取到6
nums[i] = random.randint(1, 36) # 产生随机数给i
j = 0 # j等于0
while j < i: # 当j小于i,即从0遍历到i-1
if nums[j] == nums[i]: # 若i等于j
nums[i] = random.randint(1, 36) # 产生新的随机数
j = 0 # 使j为0
else:
j += 1 # 若i不等于j,j往下遍历
# nums.sort(reverse=False) 升序排列
# nums.sort(reverse=True) 降序排列
print(nums) # 打印出nums
12.比较两数大小
num1 = int(input("请输入第一个数的值:"))
num2 = int(input("请输入第二个数的值:"))
if num1 > num2:
max = num1
min = num2
else:
max = num2
min = num1
print("max=%d\nmin=%d" % (max, min))
13.二维数组行列转换
# 转置二维数组
a = [[1, 2, 3], [4, 5, 6]]
b = []
for i in range(3):
b.append([0]*2)
print(b)
for i in range(len(b)):
for j in range(len(b[i])):
b[i][j] = a[j][i]
print(b)
# 转置二维数组
a = [[1, 2, 3], [4, 5, 6]]
b = []
for i in range(len(a[0])): # 行数
t = []
for j in range(len(a)):
t.append(a[j][i])
b.append(t)
print(b)
14.生成随机验证码
# n位数验证码,大小写字母或数字
# 提示:chr(random.randrange(ord('A'),ord('Z'+1)) 随机产生A~Z的字母
# 提示:chr(random.randrange(ord('a'),ord('z'+1)) 随机产生a~z的字母
# 提示:chr(random.randrange(ord('0'),ord('9'+1)) 随机产生A~Z的字母
import random # 导入random
n = int(input("请输入生成验证码的位数:\n")) # 输入n,为位数
str = "" # 字符串
for i in range(n): # 位数循环
ty = random.randint(1, 3) # 随机类型1到3
if ty == 1: # 1类型,大写字母
ch = chr(random.randint(ord('A'), ord('Z')))
elif ty == 2: # 2类型,小写字母
ch = chr(random.randint(ord('a'), ord('z')))
elif ty == 3: # 3类型,数字
ch = chr(random.randint(ord('0'), ord('9')))
str += ch # 拼接字符串
print("生成的验证码为:\n"+str) # 输出str验证码字符串
a = input("用户你好,请输入验证码:\n") # 提示用户输入验证码,有大小写限制
if a == str:
print("验证码正确!")
else:
print("验证码不正确!")