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例
Python练手例子(11)的更多相关文章
- Python练手例子(4)
16.一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. 程序分析:请参照程序Python 100例中的第14个例子 #py ...
- Python练手例子(3)
13.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- Python练手例子(10)
55.学习使用按位取反~. 程序分析:~0=1; ~1=0; (1)先使a右移4位. (2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) (3)将上面二者进行&运算. ...
- Python练手例子(7)
37.对10个数进行排序. 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换. #python 3.7 if __n ...
- Python练手例子(2)
7.将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:]. #python3.7 #适用于简单列表(即列表中都是基本的元素) a1 = [1,2] b1 = a1[:] print(b1) ...
- Python练手例子(16)
91.时间函数举例1. #!/usr/bin/python #coding=utf-8 import time if __name__ == '__main__': #time.time()返回当前的 ...
- Python练手例子(15)
85.输入一个奇数,然后判断最少几个 9 除于该数的结果为整数. 程序分析:999999 / 13 = 76923. #!/usr/bin/python #coding=utf-8 if __name ...
- Python练手例子(14)
79.字符串排序. #python3.7 if __name__ == '__main__': str1 = input('Input string:\n') str2 = input('Input ...
- Python练手例子(13)
73.反向输出一个链表. #python3.7 if __name__ == '__main__': ptr = [] for i in range(5): num = int(input('Plea ...
随机推荐
- PHP Request请求封装
/** * Request请求封装 * Class Request * @package tool */ class Request { // curl 请求错误码 protected static ...
- patch 28729262
打补丁最后出个error OPatch found the word "error" in the stderr of the make command.Please look a ...
- [转]Jupyter默认目录和默认浏览器修改
转摘于:https://blog.csdn.net/caterfreelyf/article/details/79774311 1.打开cmd,首先进入到Jupyter的安装目录,我的是在D:\Pyt ...
- python正则表达式--findall、finditer方法
findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...
- sortable.js 拖拽排序及配置项说明
// 拖动排序 $(function() { /*排序*/ //排序 // Simple list ]; new Sortable(list, { group: "name", a ...
- Linux安装docker
1.检查内核版本,必须是3.10及以上 uname ‐r 2.安装docker yum install docker 3.输入y确认安装 4.启动docker [root@localhost ~]# ...
- hibernate-第一章-基础
一,简介 hibernate是一个开源的ORM框架,它对我们的jdbc进行了轻量级的对象封装,它将我们的POJO与数据库表简历映射关系,是一个全自动的ORM框架;hibernate可以自动生成SQL语 ...
- GitHub上受欢迎的Android UI Library
GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...
- 为什么浏览器User Agent中都有个mozilla
你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ...
- 工具系列-idea破解
>>>>>>>>>>>>>>>>>>>>>>>>> ...