練習51:
題目:
學習使用 按位與(&) 。
分析:
0&0=0; 0&1=0; 1&0=0; 1&1=1。
程式:
if __name__ == '__main__':
a = 8
b = a & 4
print('a & b = %d' % b)
b &= 2
print('a & b = %d' % b)
輸出結果:
a & b = 0
a & b = 0
練習52:
題目:
學習使用 按位或(|) 。
分析:
0|0=0; 0|1=1; 1|0=1; 1|1=1
程式:
if __name__ == '__main__':
a = 12
b = a | 3
print('a | b is %d' % b)
b |= 7
print('a | b is %d' % b)
輸出結果:
a | b is 15
a | b is 15
練習53:
題目:
學習使用 按位異或(^) 。
分析:
0^0=0; 0^1=1; 1^0=1; 1^1=0
程式:
if __name__ == '__main__':
a = 12
b = a ^ 3
print('The a ^ 3 = %d' % b)
b ^= 7
print('The a ^ b = %d' % b)
輸出結果:
The a ^ 3 = 15
The a ^ b = 8
練習54:
題目:
取一個整數 a 從右端開始的 4~7位。
分析:
可以這樣考慮:
(1) 先使a右移4位。
(2) 設定一個低4位全為1,其餘全為0的數。可用(0<<4)
(3) 將上面二者進行 與運算(&)。
程式:
if __name__ == '__main__':
a = int(input('input a number:\n'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print('%o\t%o' % (a, d))
輸出結果:
input a number:
23
27 1
練習55:
題目:
學習使用 按位取反(~)。
分析:
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
練習56:
題目:
畫圖,學用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, 26):
canvas.create_oval(310 - k, 250 - k, 310 + k, 250 + k, width=1)
k += j
j += 0.3
mainloop()
練習57:
題目:
畫圖,學用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畫方形。
程式:
rectangle(int left, int top, int right, int bottom)。
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:
題目:
畫圖,綜合例子。
分析:
利用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:
題目:
計算字串長度。
程式:
s = 'strlen'
print(len(s))
輸出結果:
6