Python 100例(下)
如果你坚持到这了,哪就为自己鼓掌吧!坚持,你一定可以。
实例51:
题目:学习使用按位与&。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:学习使用按位与 & 。
思路:0&0=0; 0&1=0; 1&0=0; 1&1=1。
'''
if __name__ == '__main__':
a = 077
b = a & 3
print 'a & b = %d' % b
b &= 7
print 'a & b = %d' % b
# --*--coding:utf-8 --*--
'''
需求:学习使用按位与 & 。
思路:0&0=0; 0&1=0; 1&0=0; 1&1=1。
'''
if __name__ == '__main__':
a = 077
b = a & 3
print 'a & b = %d' % b
b &= 7
print 'a & b = %d' % b
输出结果:
a & b = 3
a & b = 3
a & b = 3
实例52:
题目:学习使用按位或|。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:学习使用按位或|
思路:0|0=0; 0|1=1; 1|0=1; 1|1=1
'''
if __name__ == '__main__':
a = 077
b = a | 3
print 'a | b is %d' % b
b |= 7
print 'a | b is %d' % b
# --*--coding:utf-8 --*--
'''
需求:学习使用按位或|
思路:0|0=0; 0|1=1; 1|0=1; 1|1=1
'''
if __name__ == '__main__':
a = 077
b = a | 3
print 'a | b is %d' % b
b |= 7
print 'a | b is %d' % b
输出结果:
a | b is 63
a | b is 63
a | b is 63
实例53:
题目:学习使用按位异或^.
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:学习使用按位异或 ^ 。
思路:0^0=0; 0^1=1; 1^0=1; 1^1=0
'''
if __name__ == '__main__':
a = 077
b = a ^ 3
print 'The a ^ 3 = %d' % b
b ^= 7
print 'The a ^ b = %d' % b
# --*--coding:utf-8 --*--
'''
需求:学习使用按位异或 ^ 。
思路:0^0=0; 0^1=1; 1^0=1; 1^1=0
'''
if __name__ == '__main__':
a = 077
b = a ^ 3
print 'The a ^ 3 = %d' % b
b ^= 7
print 'The a ^ b = %d' % b
输出结果:
The a ^ 3 = 60
The a ^ b = 59
The a ^ b = 59
实例54:
题目:取一个整数a从右端开始的4~7位。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:取一个整数a从右端开始的4〜7位。
思路:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
'''
if __name__ == '__main__':
a = int(raw_input('input a number:\n'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print '%o\t%o' %(a,d)
# --*--coding:utf-8 --*--
'''
需求:取一个整数a从右端开始的4〜7位。
思路:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
'''
if __name__ == '__main__':
a = int(raw_input('input a number:\n'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print '%o\t%o' %(a,d)
输出结果:
input a number:
45
55 2
45
55 2
实例55:
题目:学习使用按位取反~。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:学习使用按位取反~。
思路:~0=1; ~1=0;
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
'''
if __name__ == '__main__':
a = 234
b = ~a
print 'The a\'s 1 complement is %d' % b
a = ~a
print 'The a\'s 2 complement is %d' % a
# --*--coding:utf-8 --*--
'''
需求:学习使用按位取反~。
思路:~0=1; ~1=0;
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
'''
if __name__ == '__main__':
a = 234
b = ~a
print 'The a\'s 1 complement is %d' % b
a = ~a
print 'The a\'s 2 complement is %d' % a
输出结果:
The a's 1 complement is -235
The a's 2 complement is -235
The a's 2 complement is -235
实例56:
题目:画图,学用circle画圆形。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:画图,学用circle画圆形。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=800, height=600, bg='yellow')
canvas.pack(expand=YES, fill=BOTH)
k = 1
j = 1
for i in range(0,40):
canvas.create_oval(310 - k,250 - k,310 + k,250 + k, width=0.5)
k += j
j += 0.3
mainloop()
# --*--coding:utf-8 --*--
'''
需求:画图,学用circle画圆形。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=800, height=600, bg='yellow')
canvas.pack(expand=YES, fill=BOTH)
k = 1
j = 1
for i in range(0,40):
canvas.create_oval(310 - k,250 - k,310 + k,250 + k, width=0.5)
k += j
j += 0.3
mainloop()
输出结果:
实例57:
题目:画图,学用line画直线。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:画图,学用line画直线。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 + 5
y1 = y1 + 5
x0 = 263
y1 = 275
y0 = 263
for i in range(21):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 += 5
y0 += 5
y1 += 5
mainloop()
# --*--coding:utf-8 --*--
'''
需求:画图,学用line画直线。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=300, height=300, bg='green')
canvas.pack(expand=YES, fill=BOTH)
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
x0 = x0 - 5
y0 = y0 - 5
x1 = x1 + 5
y1 = y1 + 5
x0 = 263
y1 = 275
y0 = 263
for i in range(21):
canvas.create_line(x0,y0,x0,y1,fill = 'red')
x0 += 5
y0 += 5
y1 += 5
mainloop()
输出结果:
实例58:
题目:画图,学用rectangle画方形。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:画图,学用rectangle画方形。
思路:利用for循环控制100-999个数,每个数分解出个位,十位,百位。。
'''
if __name__ == '__main__':
from Tkinter import *
root = Tk()
root.title('Canvas')
canvas = Canvas(root,width = 400,height = 400,bg = 'yellow')
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_rectangle(x0,y0,x1,y1)
x0 -= 5
y0 -= 5
x1 += 5
y1 += 5
canvas.pack()
root.mainloop()
# --*--coding:utf-8 --*--
'''
需求:画图,学用rectangle画方形。
思路:利用for循环控制100-999个数,每个数分解出个位,十位,百位。。
'''
if __name__ == '__main__':
from Tkinter import *
root = Tk()
root.title('Canvas')
canvas = Canvas(root,width = 400,height = 400,bg = 'yellow')
x0 = 263
y0 = 263
y1 = 275
x1 = 275
for i in range(19):
canvas.create_rectangle(x0,y0,x1,y1)
x0 -= 5
y0 -= 5
x1 += 5
y1 += 5
canvas.pack()
root.mainloop()
输出结果:
实例59:
题目:画图,综合例子。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:画图,综合例子。
思路:利用for循环控制100-999个数,每个数分解出个位,十位,百位。。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width = 300,height = 300,bg = 'green')
canvas.pack(expand = YES,fill = BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10)
canvas.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20)
canvas.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50)
import math
B = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
canvas.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)
for k in range(501):
for i in range(17):
a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 + math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
for j in range(51):
a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
mainloop()
# --*--coding:utf-8 --*--
'''
需求:画图,综合例子。
思路:利用for循环控制100-999个数,每个数分解出个位,十位,百位。。
'''
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width = 300,height = 300,bg = 'green')
canvas.pack(expand = YES,fill = BOTH)
x0 = 150
y0 = 100
canvas.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10)
canvas.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20)
canvas.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50)
import math
B = 0.809
for i in range(16):
a = 2 * math.pi / 16 * i
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
canvas.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)
for k in range(501):
for i in range(17):
a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 + math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
for j in range(51):
a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
x = math.ceil(x0 + 48 * math.cos(a))
y = math.ceil(y0 + 48 * math.sin(a) * B)
canvas.create_line(x0,y0,x,y,fill = 'red')
mainloop()
输出结果:
实例60:
题目:计算字符串长度。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:计算字符串长度
'''
sStr1 = raw_input('plase in put:')
print len(sStr1)
# --*--coding:utf-8 --*--
'''
需求:计算字符串长度
'''
sStr1 = raw_input('plase in put:')
print len(sStr1)
输出结果:
plase in put:dfiaoerjf56
11
11
实例61:
题目:打印出杨辉三角(要求打印出10行)
#!/usr/bin/env 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
# --*--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(' ')
输出结果:
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
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
实例62:
题目:查找字符串。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:查找字符串。
'''
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)
'''
注:找到是2,未找到是-1
'''
# --*--coding:utf-8 --*--
'''
需求:查找字符串。
'''
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)
'''
注:找到是2,未找到是-1
'''
实例63:
题目:画椭圆ellipse。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:画椭圆ellipse。
'''
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()
# --*--coding:utf-8 --*--
'''
需求:画椭圆ellipse。
'''
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()
输出结果:
实例64:
题目:利用ellipse和rectangle画图。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:利用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()
# --*--coding:utf-8 --*--
'''
需求:利用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()
输出结果:
实例65:
题目:一个最优美的团。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:一个最优美的图案。
'''
import math
class PTS:
def __init__(self):
self.x = 0
self.y = 0
points = []
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()
# --*--coding:utf-8 --*--
'''
需求:一个最优美的图案。
'''
import math
class PTS:
def __init__(self):
self.x = 0
self.y = 0
points = []
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()
输出结果:
实例66:
题目:输入3个数a,b,c,按大小顺从输出。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:输入3个数a,b,c,按大小顺序输出。
'''
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
# --*--coding:utf-8 --*--
'''
需求:输入3个数a,b,c,按大小顺序输出。
'''
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 = :
56
n2 = :
89
n3 = :
23
23 56 89
56
n2 = :
89
n3 = :
23
23 56 89
实例67:
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
'''
def inp(numbers):
for i in range(9):
numbers.append(int(raw_input('input a number:\n')))
numbers.append(int(raw_input('input a number:\n')))
p = 0
def max_min(array):
max = min = 0
for i in range(1,len(array) - 1):
p = i
if array[p] > array[max] : max = p
elif array[p] < array[min] : min = p
k = max
l = min
array[0],array[l] = array[l],array[0]
array[9],array[k] = array[k],array[9]
def outp(numbers):
for i in range(len(numbers)):
print numbers[i]
if __name__ == '__main__':
array = []
inp(array)
max_min(array)
outp(array)
# --*--coding:utf-8 --*--
'''
需求:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
'''
def inp(numbers):
for i in range(9):
numbers.append(int(raw_input('input a number:\n')))
numbers.append(int(raw_input('input a number:\n')))
p = 0
def max_min(array):
max = min = 0
for i in range(1,len(array) - 1):
p = i
if array[p] > array[max] : max = p
elif array[p] < array[min] : min = p
k = max
l = min
array[0],array[l] = array[l],array[0]
array[9],array[k] = array[k],array[9]
def outp(numbers):
for i in range(len(numbers)):
print numbers[i]
if __name__ == '__main__':
array = []
inp(array)
max_min(array)
outp(array)
输出结果:
input a number:
45
input a number:
59
input a number:
34
input a number:
26
input a number:
98
input a number:
2
input a number:
36
input a number:
49
input a number:
28
input a number:
65
2
59
34
26
65
45
36
49
28
98
45
input a number:
59
input a number:
34
input a number:
26
input a number:
98
input a number:
2
input a number:
36
input a number:
49
input a number:
28
input a number:
65
2
59
34
26
65
45
36
49
28
98
实例68:
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。
#!/usr/bin/env python
# --*--coding:utf-8 --*--
'''
需求:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
'''
if __name__ == '__main__':
n = int(raw_input('the total number is:\n'))
m = int(raw_input('back 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('input a number:\n')))
print 'orignal number:',number
move(number,n,m)
print 'after moved:',number
# --*--coding:utf-8 --*--
'''
需求:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
'''
if __name__ == '__main__':
n = int(raw_input('the total number is:\n'))
m = int(raw_input('back 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('input a number:\n')))
print 'orignal number:',number
move(number,n,m)
print 'after moved:',number
输出结果:
the total number is:
3
back m:
26
input a number:
46
input a number:
35
input a number:
16
orignal number: [46, 35, 16]
after moved: [35, 16, 46]
3
back m:
26
input a number:
46
input a number:
35
input a number:
16
orignal number: [46, 35, 16]
after moved: [35, 16, 46]
实例69:
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下来的原来是第几号的那位。