此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例之61-70
Python学习100例之71-80
Python学习100例之81-90
Python学习100例之91-100
51.学习使用按位与 &
a = 6 0000 0110
b = 18 0001 0010
print(a & b, '\n') 输出2 0000 0010
52.学习使用按位或 | 。
print(a | b, '\n') 输出22 0001 0110
53.学习使用按位异或 ^
print(a ^ b, '\n') 输出22 0001 0100
54.取一个整数a从右端开始的4〜7位
number = int(input("输入一个整数:")) >> 4
refer = ~(~0 << 4) 第4位为1,其余为0,即为15 0000 1111
print(number & refer, '\n')
55.学习使用按位取反~
print(~a, '\n')
56.画图,学用circle画圆形
import tkinter
from tkinter import *
canvas = Canvas(width=600, height=600, bg='yellow')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.BOTH)
expand置1 使能fill属性
expand置0 关闭fill属性
fill=X 当GUI窗体大小发生变化时,widget在X方向跟随GUI窗体变化
fill=Y 当GUI窗体大小发生变化时,widget在Y方向跟随GUI窗体变化
fill=BOTH 当GUI窗体大小发生变化时,widget在X、Y两方向跟随GUI窗体变化
k = 1
j = 1
for _ in range(0, 20):
canvas.create_oval(310 - k, 310 - k, 310 + k, 310 + k, width=1)
k += 10
mainloop()
57.画图,学用line画直线。
import tkinter
from tkinter import *
canvas = Canvas(width=600, height=600, bg='green')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.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画方形
import tkinter
from tkinter import *
canvas = Canvas(width=600, height=600, bg='red')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.BOTH)
x0 = 240
y0 = 240
x1 = 270
y1 = 270
for _ in range(21):
canvas.create_rectangle(x0, y0, x1, y1, width=2)
x0 -= 10
y0 -= 10
x1 += 10
y1 += 10
mainloop()
59.画图,综合例子
import tkinter
from tkinter import *
canvas = Canvas(width=400, height=400, bg='orange')
canvas.pack(expand=tkinter.constants.YES, fill=tkinter.constants.BOTH)
x0 = 200
y0 = 200
k = 80
canvas.create_line(x0 - 2 * k, y0 - 2 * k, x0 + 2 * k, y0 + 2 * k)
canvas.create_line(x0 + 2 * k, y0 - 2 * k, x0 - 2 * k, y0 + 2 * k)
for _ in range(2):
canvas.create_oval(x0 - k, y0 - k, x0 + k, y0 + k)
canvas.create_rectangle(x0 - k, y0 - k, x0 + k, y0 + k)
k += 80
mainloop()
60.计算字符串长度
s = input("请输入一个字符串:")
print("字符串的长度为%d" % len(s))