Python练手例子(11)

61、打印出杨辉三角形。

#python3.7
from sys import stdout

if __name__ == ‘__main__‘:
    a = []
    for i in range(10):
        a.append([])
        for j in range(10):
            a[i].append(0)
    for i in range(10):
        a[i][0] = 1
        a[i][i] = 1
    for i in range(2,10):
        for j in range(1,i):
            a[i][j] = a[i - 1][j-1] + a[i - 1][j]
    for i in range(10):
        for j in range(i + 1):
            stdout.write(str(a[i][j]))
            stdout.write(‘ ‘)
        print()

62、查找字符串。

#python3.7

sStr1 = ‘language‘
sStr2 = ‘age‘
print(sStr1.find(sStr2))

结果:
5

63、使用Tkinter画椭圆。

#python3.7
from tkinter import *

if __name__ == ‘__main__‘:
    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()

64、利用ellipse 和 rectangle 画图。

#python3.7
from tkinter import *

if __name__ == ‘__main__‘:
    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_oval(20 - 2 * i, 20 - 2 * i, 10 * (i + 2), 10 * (i + 2))
        right += 5
        left += 5
        top += 10

    canvas.pack()
    mainloop()

65、一个最优美的图案。

#python3.7
from tkinter import *
import math

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()

66、输入3个数a,b,c,按大小顺序输出。

#python3.7

if __name__ == ‘__main__‘:
    n1 = int(input(‘n1 = \n‘))
    n2 = int(input(‘n2 = \n‘))
    n3 = int(input(‘n3 = \n‘))

    def swap(p1, p2):
        return p2, p1

    if n1 > n2: n1, n2 = swap(n1, n2)
    if n1 > n2: n1, n3 = swap(n1, n3)
    if n2 > n3: n2, n3 = swap(n2, n3)

    print(n1, n2, n3)

参考资料:

Python 100例

原文地址:https://www.cnblogs.com/finsomway/p/10407062.html

时间: 2024-08-30 12:18:02

Python练手例子(11)的相关文章

Python练手例子(7)

37.对10个数进行排序. 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换. #python 3.7 if __name__ == '__main__': N = 10 print('请输入10个数字:\n') l = [] print for i in range(N): l.append(int(input('请输入一个数字:\n'))) print for i in range(N): print(l[i

Python练手例子(5)

25.求1+2!+3!+...+20!的和. 程序分析:此程序只是把累加变成了累乘. #python3.7 n = 0 s = 0 t = 1 for n in range(1, 21): t *= n s += t print('1! + 2! + 3! + ... + 3! + 20! = %d' % s) 结果: 1! + 2! + 3! + ... + 3! + 20! = 2561327494111820313 26.利用递归方法求5!. 程序分析:递归公式:fn=fn_1*4! #p

Python练手例子(8)

43.模仿静态变量(static)另一案例. 程序分析:演示一个python作用域使用方法. #python3.7 class Num: nNum = 1 def inc(self): self.nNum += 1 print('nNum = %d' % self.nNum) if __name__ == '__main__': nNum = 2 inst = Num() for i in range(3): nNum += 1 print('The num = %d' % nNum) inst

Python练手例子(16)

91.时间函数举例1. #!/usr/bin/python #coding=utf-8 import time if __name__ == '__main__': #time.time()返回当前的时间戳(1970纪元后经过的浮点秒数) print(time.time()) #time.ctime()把时间戳转化为time.asctime()的形式 print(time.ctime(time.time())) #time.asctime()返回"Tue Feb 26 09:12:37 2019

Python练手项目:20行爬取全王者全英雄皮肤

引言 ? ?王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. ? ?文中源代码在文章末尾,可自行复制粘贴. 准备工作 ? ?爬取皮肤本身并不难,难点在于分析,我们首先得得到皮肤图片的url地址,话不多说,我们马上来到王者荣耀的官网: ? ?我们点击英雄资料,然后随意地选择一位英雄,接着F12打开调试台,找到英雄原皮肤的图片地址: ? ?接着,我们切换一下英雄的

70个Python练手项目

前言: 不管学习那门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说大家都知道学编程语言一定要做项目才行. 这里整理了70个Python实战项目列表,都有完整且详细的教程,你可以从中选择自己想做的项目进行参考学习练手,你也可以从中寻找灵感去做自己的项目. 70个Python项目列表: 1.[Python 图片转字符画]2.[200行Python代码实现2048]3.[Python3 实现火车票查询工具]4.[高德API+Python解决租房问题 ]5.[Python3 色情图

【转载】【python】python练手项目

入门篇 1.Python - Python 图片转字符画 50 行 Python 代码完成图片转字符画小工具. 2.Python - 200行Python代码实现2048 仅用200行的python代码完成2048小游戏的编写. 3.Python - pygame开发打飞机游戏 使用Python快速开发一款PC端玩耍的微信打飞机游戏,基于pygame实现. 4. Python 实现简单画板 要利用 Pygame 模块来自己实现一个功能更加简单的画板. 5.Python - 全面解析PythonC

python练手项目

文本操作 逆转字符串--输入一个字符串,将其逆转并输出. 拉丁猪文字游戏--这是一个英语语言游戏.基本规则是将一个英语单词的第一个辅音音素的字母移动到词尾并且加上后缀-ay(譬如"banana"会变成"anana-bay").可以在维基百科上了解更多内容. 统计元音字母--输入一个字符串,统计处其中元音字母的数量.更复杂点的话统计出每个元音字母的数量. 判断是否为回文--判断用户输入的字符串是否为回文.回文是指正反拼写形式都是一样的词,譬如"racecar

Python练手,封装日志模块,v2

前面第1版写好后,很凌乱,主要的问题在于,Python不支持方法重载,想要灵活创建对象,当时的变通办法是,先链式地有选择地设置属性(方法重载的本质就是有选择地设置属性),再做实例化,这样导致后面创建对象的时候就很凌乱. 然后才知道,Python可以缺省参数,变相做到方法重载 代码:Python3 # -*- coding: utf-8 -*- '''     --封装了logging模块,舍弃了繁琐了设置,仅保留关键设置,美化了输出格式 ''' import sys,random,time  i