1 '''
2 【程序38】
3 题目:求一个3*3矩阵对角线元素之和
4 '''
5 l = []
6 for i in range(3):
7 for j in range(3):
8 l.append(int(raw_input('Input a number:')))
9 s = 0
10 for i in range(3):
11 s += l[3*i+i]
12 print s
1 '''
2 【程序39】
3 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
4 '''
5 l = [0,10,20,30,40,50]
6
7 print 'The sorted list is:',l
8 cnt = len(l)
9 n = int(raw_input('Input a number:'))
10 l.append(n)
11 for i in range(cnt):
12 if n<l[i]:
13 for j in range(cnt,i,-1):
14 l[j] = l[j-1]
15 l[i] = n
16 break
17 print 'The new sorted list is:',l
1 '''
2 【程序40】
3 题目:将一个数组逆序输出。
4 '''
5 a = [1,2,3,4,5,6,7,8,9]
6 l = len(a)
7 print a
8 for i in range(l/2):
9 a[i],a[l-i-1] = a[l-i-1],a[i] #注意此句
10 print a
1 '''
2 【程序41】
3 题目:学习static定义静态变量的用法
4 题目:学习使用auto定义变量的用法
5 题目:学习使用external的用法
6 题目:学习使用register定义变量的方法
7 题目:宏#define命令练习
8 '''
9 #Python没有这些功能
1 '''
2 演示lambda的使用。
3 '''
4 MAXIMUM = lambda x,y : (x > y) * x + (x < y) * y
5 MINIMUM = lambda x,y : (x > y) * y + (x < y) * x
6
7 if __name__ == '__main__':
8 a = 10
9 b = 20
10 print 'The largar one is %d' % MAXIMUM(a,b)
11 print 'The lower one is %d' % MINIMUM(a,b)
1 '''
2 【程序51】
3 题目:学习使用按位与 & 。
4 程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
5
6 题目:学习使用按位或 | 。
7 程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
8
9 题目:学习使用按位异或 ^ 。
10 程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
11
12 题目:学习使用按位取反~。
13 程序分析:~0=1; ~1=0
14
15 '''
16 a = 077#8进制
17 b = a&3
18 print '077&3=%d'%b
19 b&=7
20 print '077&3&7=%d'%b
21
22 a = 077
23 b = a | 3
24 print '077|3=%d' % b
25 b |= 7
26 print '077|3|7=%d' % b
27
28 a = 077
29 b = a ^ 3
30 print '077^3=%d' % b
31 b ^= 7
32 print '077^3^7=%d' % b
1 '''
2 【程序54】
3 题目:取一个整数a从右端开始的4~7位。
4 '''
5 a = 100
6 print 100&0x00F0
1 '''
2 【程序56】
3 题目:画圆【Tkinter模块】
4 '''
5 if __name__ == '__main__':
6 from Tkinter import *
7
8 canvas = Canvas(width=800, height=600, bg='red')
9 canvas.pack(expand=YES, fill=BOTH)
10 k = 1
11 j = 1
12 for i in range(0,26):
13 canvas.create_oval(310 - k,250 - k,310 + k,250 + k, width=1)
14 k += j
15 j += 0.3
16
17 mainloop()
1 '''
2 【程序57】
3 题目:画直线。
4 1.程序分析:
5 2.程序源代码:
6 '''
7 if __name__ == '__main__':
8 from Tkinter import *
9
10 canvas = Canvas(width=300, height=300, bg='green')
11 canvas.pack(expand=YES, fill=BOTH)
12 x0 = 263
13 y0 = 263
14 y1 = 275
15 x1 = 275
16 for i in range(19):
17 canvas.create_line(x0,y0,x0,y1, width=1, fill='red')
18 x0 = x0 - 5
19 y0 = y0 - 5
20 x1 = x1 + 5
21 y1 = y1 + 5
22
23 x0 = 263
24 y1 = 275
25 y0 = 263
26 for i in range(21):
27 canvas.create_line(x0,y0,x0,y1,fill = 'red')
28 x0 += 5
29 y0 += 5
30 y1 += 5
31
32 mainloop()
1 '''
2 【程序58】
3 题目:画方形。
4 '''
5 if __name__ == '__main__':
6 from Tkinter import *
7 root = Tk()
8 root.title('Canvas')
9 canvas = Canvas(root,width = 400,height = 400,bg = 'yellow')
10 x0 = 263
11 y0 = 263
12 y1 = 275
13 x1 = 275
14 for i in range(19):
15 canvas.create_rectangle(x0,y0,x1,y1)
16 x0 -= 5
17 y0 -= 5
18 x1 += 5
19 y1 += 5
20
21 canvas.pack()
22 root.mainloop()
1 '''
2 题目:画图,综合例子。
3 1.程序分析:
4 2.程序源代码:
5 '''
6 if __name__ == '__main__':
7 from Tkinter import *
8 canvas = Canvas(width = 300,height = 300,bg = 'green')
9 canvas.pack(expand = YES,fill = BOTH)
10 x0 = 150
11 y0 = 100
12 canvas.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10)
13 canvas.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20)
14 canvas.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50)
15 import math
16 B = 0.809
17 for i in range(16):
18 a = 2 * math.pi / 16 * i
19 x = math.ceil(x0 + 48 * math.cos(a))
20 y = math.ceil(y0 + 48 * math.sin(a) * B)
21 canvas.create_line(x0,y0,x,y,fill = 'red')
22 canvas.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)
23
24
25 for k in range(501):
26 for i in range(17):
27 a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k
28 x = math.ceil(x0 + 48 * math.cos(a))
29 y = math.ceil(y0 + 48 + math.sin(a) * B)
30 canvas.create_line(x0,y0,x,y,fill = 'red')
31 for j in range(51):
32 a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1
33 x = math.ceil(x0 + 48 * math.cos(a))
34 y = math.ceil(y0 + 48 * math.sin(a) * B)
35 canvas.create_line(x0,y0,x,y,fill = 'red')
36 mainloop()