python笔记

 

第1课 神奇的笔

import turtle  #导入海龟
kkk = turtle.Turtle()  #制作画笔
kkk.color("red")  #将画笔颜色设定为红色
kkk.forward(100)  #画一条长度为100的线
kkk.left(120)  #左转120
kkk.color("blue")#将颜色设定为蓝色
kkk.forward(100)#画长度为100的线
kkk.left(120)#左转120
kkk.color("green")#将颜色设定为绿色
kkk.forward(100)#
kkk.left(120)
kkk.color("yellow")
kkk.left(120)
kkk.forward(100)
kkk.color("purple")
kkk.right(120)
kkk.forward(100)

第2课 Python绘制简易几何图形

import turtle
t=turtle.Turtle()
t.speed(5)
t.pensize(20)#将画笔的粗细设定为20
t.circle(100)画一个半径为100的⚪
t.color("red")
t.circle(90)
t.color("grey")
t.circle(80)
t.color("pink")
t.circle(70)
t.color("yellow")
t.circle(60)
t.color("green")
t.circle(50)
t.color("orange")
t.circle(40)
t.color("skyblue")
t.circle(30)
t.color("puple")
t.circle(20)
t.color("violet")
t.circle(10)

第3课 阳光微笑

import turtle
t=turtle.Turtle()
t.shape=turtle
t.speed(2)
t.color('yellow')
t.penup()
t.goto(-80,90)
t.pendown()
t.begin_fill()
t.circle(22)
t.end_fill()
t.color('yellow')
t.penup()
t.goto(80,90)
t.pendown()
t.begin_fill()
t.circle(22)
t.end_fill()
t.penup()
t.goto(0,70)
t.pendown()
t.color('brown')
t.begin_fill()
t.circle(-50,steps=3)
t.end_fill()
t.penup()
t.goto(0,-120)
t.pendown()
t.pensize(6)
t.color('blue')
t.circle(165)
t.penup()
t.goto(50,-50)
t.pendown()
t.right(90)
t.color('red')
t.pensize(7)
t.circle(-50,180)

第4课 螺旋风叶

import turtle
t = turtle.Turtle()
t.speed(0)
t.color("red")
for y in range(15):
    for x in range(4):
        t.circle(y*8)
        t.right(90)
    t.left(5)

第5课 正方螺旋

import turtle
t=turtle.Turtle()
t.speed(0)
c=['red','blue','green','yellow']
for x in range(4):
    t.color(c[x%4])
    t.begin_fill()#开始填充
    t.circle(50,steps=3)#将50半径的圆内切3次
    t.end_fill()#结束填充
    t.left(90)

第6课 画爱心

import turtle
t=turtle.Turtle()
t.penup()
t.goto(-20,0)
t.pendown()
t.color('red')
t.write('静夜思',font=('楷体',30,'bold'))
t.penup()
t.goto(-100,-50)
t.pendown()
t.color('green')
t.write('床前明月光,疑是地上霜',font=('楷体',20,'bold'))
t.penup()
t.goto(-100,-90)
t.pendown()
t.color('blue')
t.write('举头望明月,低头思故乡',font=('楷体',20,'bold'))

第7课 太阳花

import random
import turtle
t=turtle.Turtle()
t.speed(0)
a=random.randint(1,50)
t.penup()
t.goto(a,a)
t.pendown()
print(a)
c=['red','orange','yellow','green','skyblue','blue','purple']
for x in range(101):
    t.color(c[x%7])
    if x%2==0:
        t.left(190) 
    else:
        t.left(100) 
    t.fd(100)

第8课 梦幻气泡

#登陆系统
import random
a='Eddie'#准备账号
b=19#准备密码
c=input ('请输入账号:')#所以账号不需要转化类型,本身就是字符串
if a==c:
    print('账号正确,请输入密码')
    d=int(input('请输入密码:'))#7788
    if b==d:
        print('恭喜您登陆成功')
    else:
        print('密码错误,请重新输入')

#猜数字游戏
e=random.randint(1,7)#准备一个答案
f=int(input('请输入一个1-7之间的数字:'))#将字符串类型转化成了整型
if e==f:
    print('恭喜你猜对了')
else:
    print('很遗憾,猜错了')

第9课 图形组合

import random
a=0
b=0
while True:
    s1=random.randint(1,6)
    s2=random.randint(1,6)
    s=s1+s2
    if 4<s<10:
        a+=1
    else:
        b+=1
    if a==100:
        print('awin')
        break
    if b==100:
        print('bwin')
        break

第10课 彩色螺旋

import  turtle
t=turtle.Turtle()
t.speed(0)
c=['red','blue','orange','green']
for x in range(90):
    t.color(c[x%4])
    t.circle(100)
    t.left(10)

第11课 多彩组合图形

import turtle
import random
t=turtle.Turtle()
t.speed(0)
num=int(input('请输入次数'))
r=int(input('请输入半径'))
c=['green','orange','yellow','blue']#颜色有绿,橘,黄,蓝
for x in range(num):
    a=random.randint(-220,110)
    b=random.randint(-200,220)
    t.color(c[x%4])#随机取4种颜色
    t.penup()    #落笔
    t.goto(a,b)
    t.begin_fill()
    f=x%3
    if f==0:
        t.circle(r)
    elif f==1:
        t.circle(r,steps=3)
    elif f==2:
        for y in range(5):
            t.fd(r)
            t.left(144)          
    t.end_fill()

第12课 海龟跑圈

import turtle
t=turtle.Turtle()
t.hideturtle()
t.penup()#抬笔
t.goto(-50,-50)#移动到-50
t.pendown()#落笔
t.color('red')#将颜色设定为红色
t.width(20)
t.fd(100)#画一条长100米的线
t.circle(50,180)#画一个半径为50的园
t.fd(100)#画一条长100米的线
t.circle(50,180)
t1=turtle.Turtle()
t1.color('green')
t1.shape('turtle')
t1.penup()
t1.goto(-50,-50)
num=int(input('请输入圈数'))
for x in range(num):
    print('当前是第'+str(x+1)+'')
    t1.fd(100)
    t1.circle(50,180)
    t1.fd(100)
    t1.circle(50,180)

第13课 九九乘法表

for x in range(10):
    s=''
  for x in range(x):
    s=s+'*'
print(s)

第14课 123数字黑洞

s='python'
for x in range(len(s)):
    print(s[x])

第15课 骰子赛车

import random #导入随机库
a=0  #a从0开始
b=0  #b从0开始
while True:  #导入while无限循环
    s1=random.randint(1,6)#生成1到6之间的正数
    s2=random.randint(1,6)#生成1到6之间的正数
    s=s1+s2
    if 4<s<10:
        a+=1
    else:
        b+=1
    if a==100:
        print('awin')
        break
    if b==100:
        print('bwin')
        break

第16课 扑克游戏之洗牌

import random#导入随机库
types=['','','','']
nums=['A','2','3','4','5','6','7','8','9','10','J','Q','K']#创造A2345678910JQK
cards=[]#创造一个空盒子
for i in range(4):
    for j in range(13):
        card=types[i]+nums[j]
        cards.append(card)#将牌追加到牌盒中
random.shuffle(cards)
print(cards)#打印出牌盒

第17课 扑克游戏之发牌

"""
一幅扑克52张牌,4个玩家,每人13张牌,已经被拿到的牌,不允许第二个人重复
"""
import random
num = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] # 创造一个A2345678910JQK 
types = ['', '', '', '']
cards = []  # 该列表存放所有的纸牌
for i in types:
    for j in num:
        cards.append(i + j)
random.shuffle(cards)  # 将纸牌列表全部打乱
print(cards)#打印
'''
player1=[]
for x in range(0,52,4):
    player1.append(cards[x])
print('玩家1的牌为',player1)
player2=[]
for x in range(1,52,4):
    player2.append(cards[x])
print('玩家2的牌为',player2)
player3=[]
for x in range(2,52,4):
    player3.append(cards[x])
print('玩家3的牌为',player3)
player4=[]
for x in range(3,52,4):
    player4.append(cards[x])
print('玩家4的牌为',player4)
'''


for i in range(0,4,1):
    L=[]
    for j in range(0,52,4):
        L.append(cards[j+i])
    print('玩家',i+1,'的牌为:',L)

第18课 花色游戏PK

#=================================================洗牌
import random
num = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']#a2345678910jkq
types = ['', '', '', '']
cards = []  # 该列表存放所有的纸牌
for i in types:
    for j in num:
        cards.append(i + j)
random.shuffle(cards)  # 将纸牌列表全部打乱
print(cards)
print("=============================================")

#===================================================发牌
player = [ [], [], [], [] ]玩家的牌盒
for y in range(4):
    for x in range(y,52,4):
        player[y].append(cards[x])
    print( "玩家" + str(y) + "的牌为:" + str(player[y]) )#打印玩家的牌
print("=============================================")

#=============
res=types[random.randint(0,3)]
print('包含',res,'玩家会获胜')
for z in range(4):
    rdc=player[z][random.randint(0,12)]
    print('玩家获取的牌为:',rdc)
    if rdc[0]==res:
        print('win')
    else:
        print('lose')

第19课 海盗的宝藏

for ABC in range(100,1000):#重复执行899次
    A=ABC//100                #a=abc正除100
    BC=ABC%100              #bc=abc对100取余
    B=BC//10                    #b=bc正除10
    C=BC%10                    #c=bc取余10
    if BC %16==0:
        if ABC%13==0:
            if A+B+C==11:
                print(ABC)                        

第20课 巧解数字题

for x in range(0,10):
    if x ==5:
        print(x)
for ab in range(10,100):
    a=ab//10
    b=ab%10
    if a+b==5:
        print(ab)
for abc in range(100,1000):
    a=abc//100
    bc=abc%100
    b=bc//10
    c=bc%10
    if a+b+c==5:
        print(abc)

第21课 趣味自然数

for ab in range(10,100):
    a=ab//10
    b=ab%10
    if ab==(a+b)+(a*b):
        print(ab)     
print('*********') 
for x in range(10000):
    n=str(x**2)   #计算10000以内每一个数的乘积
    m=str(x)      #将x数字转化为字符串
    c=len(m)      #计算字符串m的长度
    if x==int(n[-c::]):   #[]代表字符串列表 
    #通过int将字符串转化为数字,n[开始值:结尾值:步长],负号代表反向
        print(x)

第22课  哥德巴赫猜想

a=int(input('请输入一个数字'))
f='是質數'
for x in range(2,a):
    if a%x==0:
        f='不是質數'
    break
print(str(a)+f)

第23课 扑克牌比赛

m=[23,45,77,21,88]
for x in range(4):
    for y in range(4-x):
        if m[y]>m[y+1]:
            m[y],m[y+1]=m[y+1],m[y]
print(m)

第24课 斐波那契数列

s=[]  #创建一个空列表
a=1
b=1
for x in range(20):#x取0~19(即前20个)个值,循环执行20次
    s.append(a) #执行一次添加动作,将a的值添加到列表s中
    a,b=b,a+b #执行2个赋值动作,分别将b的值赋给a,a+b的值赋给b
print(s)

第25课 蜗牛爬树

day=1
up=1
down=0.78
dis=0.22
while dis<=9.8:
    day+=1
    up=up*0.95
    dis=dis+up
    if dis>9.8:
        break
    down=down*0.8
    dis=dis-down
print('爬行到树顶的天数为:%d'%day)

第26课 李白估酒

a=input('請輸入一個數字')
if a. isdigit():
    print('是數字')
else:
     print('不是數字')

第27课 狐狸找兔子

cave=0
cas=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
for times in range(1000):
    cave=(times+cave)%15
    if cave in cas:
        cas.remove(cave)
print('安全的洞穴为:',cas)

第28课 常青树

import turtle
t=turtle.Turtle()
t.pencolor('orange')
t.fillcolor('red')
t.begin_fill()
t.circle(50)
t.end_fill()

第29课 函数

import turtle
t = turtle.Turtle()
t.hideturtle()
t.pensize(4)
t.fillcolor("#955741")
t.penup()
t.goto(-15, 0)
t.pendown()
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(50)
t.right(90)
t.forward(30)
t.right(90)
t.forward(50)
t.end_fill()
t.right(90)
t.fillcolor("#3d8442")
def draw_crown(x, y, w):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    for i in range(3):
        t.forward(w)
        t.left(120)
    t.end_fill()




draw_crown(-60, 0, 120)
draw_crown(-50, 40, 100)

第30课 五彩多边形

import turtle
t=turtle.Turtle()
def draw(c,s):
    t.color(c)
    t.begin_fill()
    for i in range(s):
        t.fd(50)
        t.left(360/s)
    t.end_fill()
c=input('请输入颜色 ')
s=int(input('请输入边数'))
draw(c,s)

第31课 海龟赛跑

import turtle
import random
t=turtle.Turtle()
t.speed(0) #添加速度
#准备场地
t.color('yellow')
t.up()
t.goto(-180,-60)
t.down()
t.begin_fill()
for x in range(2):
    t.fd(360)
    t.lt(90)
    t.fd(120)
    t.lt(90)
t.end_fill()
#分割线
t.up()
t.goto(-180,0)
t.down()
t.color('black')
t.pensize(3)
t.fd(360)
t.ht()

#第一只海龟
t1=turtle.Turtle()
t1.shape('turtle')
t1.color('green')
t1.up()
t1.goto(-180,30)

#第二只海龟
t2=turtle.Turtle()
t2.shape('turtle')
t2.color('red')
t2.up()
t2.goto(-180,-30)

#开始比赛
x=0   #绿海龟所跑次数
y=0   #红海龟所跑次数
while x<36 and y<36 : #说明两只海龟都没有到达终点
    r=random.randint(0,1) #从0和1之间随机选出一个数字,0表示绿海龟 1表示红海龟
    if r==0:
        t1.fd(10)  #前进10
        x+=1       #绿海龟前进次数加1
    else:
        t2.fd(10) 
        y+=1       #红海龟前进次数加1
    
if x==36:
    print('恭喜绿海龟赢了')
else:
    print('哎呀,红海龟赢了')

 百变常青树

'''
import turtle
class Tree:
    s = 3
    def set_side(self, s):
        self.s = s
    def draw_tree(self, x, y):
        print(id(self))
        t = turtle.Turtle()
        t.hideturtle()
        t.speed(0)
        t.pensize(4)
        t.fillcolor("#955741")
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.begin_fill()
        t.forward(90 / self.s)
        t.right(90)
        t.forward(150 / self.s)
        t.right(90)
        t.forward(90 / self.s)
        t.right(90)
        t.forward(150 / self.s)
        t.end_fill()
        t.right(90)
        t.fillcolor("#3d8442")
        t.penup()
        t.goto(x - (135 / self.s), y)
        t.pendown()
        t.begin_fill()
        for i in range(self.s):
            t.forward(360 / self.s)
            t.left(360 / self.s)
        t.end_fill()
        t.penup()
        t.goto(x - (105 / self.s), y + 40)
        t.pendown()
        t.begin_fill()
        for i in range(self.s):
            t.forward(300 / self.s)
            t.left(360 / self.s)
        t.end_fill()
        t.penup()
        t.goto(x - (75 / self.s), y + 80)
        t.pendown()
        t.begin_fill()
        for i in range(self.s):
            t.forward(240 / self.s)
            t.left(360 / self.s)
        t.end_fill()
a1=Tree()
a1.set_side(5)        
a1.draw_tree(-30,-40)
    
a2=Tree()
a2.set_side(8)        
a2.draw_tree(100,-40) 

a3=Tree()
a3.set_side(4)        
a3.draw_tree(-160,-40)
        
a4=Tree()
a4.set_side(18)        
a4.draw_tree(-80,-200)
'''

import turtle
t=turtle.Turtle()
t.speed(0)
t.begin_fill()
t.circle(80,180)
t.circle(40,180)
t.circle(-40,180)
t.end_fill()
t.circle(-80,180)

t.up()
t.goto(0,30)
t.down()
t.begin_fill()
t.circle(10)
t.end_fill()

t.up()
t.goto(0,110)
t.down()
t.color('white')
t.begin_fill()
t.circle(10)
t.end_fill()
t.ht()
原文地址:https://www.cnblogs.com/haishangmingdeng/p/11484966.html