此Python版本为2.7,其他例子如下:
Python学习100例之1-10
Python学习100例之11-20
Python学习100例之21-30
Python学习100例之31-40
Python学习100例之41-50
Python学习100例之51-60
Python学习100例之71-80
Python学习100例之81-90
Python学习100例之91-100
61.打印出杨辉三角形
pascal = [[1]]
num = int(input("请输入行数:"))
for i in range(1, num):
pascal.append([1])
for j in range(1, i):
pascal[i].append(pascal[i - 1][j - 1] + pascal[i - 1][j])
pascal[i].append(1)
for i in pascal:
for j in range(len(i)):
if j != len(i) - 1:
print(i[j], end=' ')
else:
print(i[j])
print('\n')
62.查找字符串
str1 = 'qwer'
str2 = 'r'
print(str1.find(str2), '\n')
63.画椭圆
import tkinter
from tkinter import *
canvas = Canvas(width=600, height=600, bg='yellow')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.BOTH)
canvas.create_oval(200, 100, 480, 240)
mainloop()
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 画图
import tkinter
from tkinter import *
canvas = Canvas(width=600, height=600, bg='white')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.BOTH)
canvas.create_rectangle(200, 100, 480, 240)
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()
65.一个最优美的图案
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()
LineToDemo()
66.输入3个数a,b,c,按大小顺序输出
a = int(input("请输入数字a:"))
b = int(input("请输入数字b:"))
c = int(input("请输入数字c:"))
nums = [a, b, c]
nums.sort()
print(nums[0], ' ', nums[1], ' ', nums[2])
67.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
nums = []
for _ in range(6):
numbers = int(input("请输入一个数字:"))
nums.append(numbers)
for i in range(len(nums)):
if nums[i] == max(nums):
nums[0], nums[i] = max(nums), nums[0]
if nums[i] == min(nums):
nums[int(len(nums) - 1)], nums[i] = min(nums), nums[int(len(nums) - 1)]
print(nums, '\n')
68.有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
n = [1, 2, 3, 4, 5, 6, 7, 8, 9]
m = int(input("向后移位置数:"))
n = n[m:] + n[:m]
print(n, '\n')
69.有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
n = int(input("请输入人数:"))
class person:
def __init__(self, number):
self.number = number
persons = []
for i in range(n):
people = person(i + 1)
persons.append(people)
while True:
persons = persons[3:] + persons[:2]
if len(persons) == 2:
persons = persons[1:]
break
print(persons[0].number, '\n')
70.写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度
def strLength(string):
return print("字符串的长度%d" % len(string))
strLength(input("输入字符串:"))