python実戦訓練---基礎練習(13)


文書ディレクトリ
  • 楊輝三角
  • 検索文字列
  • 楕円
  • を描く
  • 楕円、矩形
  • を描く
  • 絵の組み合わせ図形
  • 楊輝三角
    タイトル:楊輝三角形の最初の10行を印刷します.
    プログラム解析なし.
    def generate(numRows):
        r = [[1]]
        for i in range(1,numRows):
            r.append(list(map(lambda x,y:x+y, [0]+r[-1],r[-1]+[0])))
        return r[:numRows]
    a=generate(10)
    for i in a:
        print(i)
    

    文字列の検索
    タイトル:文字列を検索します.
    プログラム解析なし.
    s1='aabbxuebixuebi'
    s2='ab'
    s3='xue'
    print(s1.find(s2))
    print(s1.find(s3))
    

    だえんを描く
    テーマは楕円を描く.
    プログラム解析はtkinterを使用します.
    if __name__ == '__main__':
        from tkinter import *
        x = 360
        y = 160
        top = y - 30
        bottom = y - 30
        
        canvas = Canvas(width = 400,height = 600,bg = 'white')
        for i in range(20):
            canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom)
            top -= 5
            bottom += 5
        canvas.pack()
        mainloop()
    

    楕円、矩形を描く
    タイトルはellipseとrectangleを利用して絵を描きます.
    プログラム解析なし.
    if __name__ == '__main__':
        from tkinter import *
        canvas = Canvas(width = 400,height = 600,bg = 'white')
        left = 20
        right = 50
        top = 50
        num = 15
        for i in range(num):
            canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
            canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
            canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
            right += 5
            left += 5
            top += 10
        canvas.pack()
        mainloop()
    

    コンビネーション図を描く
    テーマは最も美しい図案です.
    プログラム解析なし.
    import math
    from tkinter import *
    
    class PTS:
        def __init__(self):
            self.x = 0
            self.y = 0
    points = []
    
    def LineToDemo():
        screenx = 400
        screeny = 400
        canvas = Canvas(width = screenx,height = screeny,bg = 'white')
    
        AspectRatio = 0.85
        MAXPTS = 15
        h = screeny
        w = screenx
        xcenter = w / 2
        ycenter = h / 2
        radius = (h - 30) / (AspectRatio * 2) - 20
        step = 360 / MAXPTS
        angle = 0.0
        for i in range(MAXPTS):
            rads = angle * math.pi / 180.0
            p = PTS()
            p.x = xcenter + int(math.cos(rads) * radius)
            p.y = ycenter - int(math.sin(rads) * radius * AspectRatio)
            angle += step
            points.append(p)
        canvas.create_oval(xcenter - radius,ycenter - radius,
                           xcenter + radius,ycenter + radius)
        for i in range(MAXPTS):
            for j in range(i,MAXPTS):
                canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y)
    
        canvas.pack()
        mainloop()
    if __name__ == '__main__':
        LineToDemo()