Python3入门经典100例(60
一:计算字符串长度。
#!/usr/bin/python# -*- coding: UTF-8 -*- sStr1 = ‘strlen’print len(sStr1)
以上实例输出结果为:
6
二:打印出杨辉三角形(要求打印出10行如下图)。
#!/usr/bin/python# -*- coding: UTF-8 -*- 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] from sys import stdout for i in range(10): for j in range(i + 1): stdout.write(str(a[i][j])) stdout.write(‘ ‘) print
以上实例输出结果为:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
三:查找字符串。
#!/usr/bin/python# -*- coding: UTF-8 -*-sStr1 = ‘abcdefg’sStr2 = ‘cde’print sStr1.find(sStr2)
以上实例输出结果为:
2
四:画椭圆。
程序分析:使用 Tkinter。
#!/usr/bin/python# -*- coding: UTF-8 -*- 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 画图。
#!/usr/bin/python# -*- coding: UTF-8 -*-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()
以上实例输出结果为:
六:一个最优美的图案。
#!/usr/bin/python# -*- coding: UTF-8 -*-import mathclass PTS: def __init__(self): self.x = 0 self.y = 0points = []def LineToDemo(): from Tkinter import * 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()
以上实例输出结果为:
七:输入3个数a,b,c,按大小顺序输出。
#!/usr/bin/python# -*- coding: UTF-8 -*- if __name__ == ‘__main__’: n1 = int(raw_input(‘n1 = :\n’)) n2 = int(raw_input(‘n2 = :\n’)) n3 = int(raw_input(‘n3 = :\n’)) def swap(p1,p2): return p2,p1 if n1 > n2 : n1,n2 = swap(n1,n2) if n1 > n3 : n1,n3 = swap(n1,n3) if n2 > n3 : n2,n3 = swap(n2,n3) print n1,n2,n3
以上实例输出结果为:
n1 = :123n2 = :456n3 = :789123 456 789
八:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
#!/usr/bin/python# -*- coding: UTF-8 -*- def inp(numbers): for i in range(6): numbers.append(int(raw_input(‘输入一个数字:\n’)))p = 0 def arr_max(array): max = 0 for i in range(1,len(array) – 1): p = i if array[p] > array[max] : max = p k = max array[0],array[k] = array[k],array[0]def arr_min(array): min = 0 for i in range(1,len(array) – 1): p = i if array[p] < array[min] : min = p l = min array[5],array[l] = array[l],array[5] def outp(numbers): for i in range(len(numbers)): print numbers[i] if __name__ == '__main__': array = [] inp(array) # 输入 6 个数字并放入数组 arr_max(array) # 获取最大元素并与第一个元素交换 arr_min(array) # 获取最小元素并与最后一个元素交换 print '计算结果:' outp(array)
以上实例输出结果为:
输入一个数字:1输入一个数字:2输入一个数字:3输入一个数字:7输入一个数字:9输入一个数字:8计算结果:923781
九:有 n 个整数,使其前面各数顺序向后移 m 个位置,最后 m 个数变成最前面的 m 个数
#!/usr/bin/python# -*- coding: UTF-8 -*- if __name__ == ‘__main__’: n = int(raw_input(‘整数 n 为:\n’)) m = int(raw_input(‘向后移 m 个位置为:\n’)) def move(array,n,m): array_end = array[n – 1] for i in range(n – 1,-1,- 1): array[i] = array[i – 1] array[0] = array_end m -= 1 if m > 0:move(array,n,m) number = [] for i in range(n): number.append(int(raw_input(‘输入一个数字:\n’))) print ‘原始列表:’,number move(number,n,m) print ‘移动之后:’,number
以上实例输出结果为:
整数 n 为:8向后移 m 个位置为:5输入一个数字:2输入一个数字:8输入一个数字:6输入一个数字:1输入一个数字:78输入一个数字:45输入一个数字:34输入一个数字:2原始列表: [2, 8, 6, 1, 78, 45, 34, 2]移动之后: [1, 78, 45, 34, 2, 2, 8, 6]
十:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
#!/usr/bin/python# -*- coding: UTF-8 -*- if __name__ == ‘__main__’: nmax = 50 n = int(raw_input(‘请输入总人数:’)) num = [] for i in range(n): num.append(i + 1) i = 0 k = 0 m = 0 while m < n - 1: if num[i] != 0 : k += 1 if k == 3: num[i] = 0 k = 0 m += 1 i += 1 if i == n : i = 0 i = 0 while num[i] == 0: i += 1 print num[i]
执行以上代码,输出结果:
$ python test.py 请输入总人数:3410