Python练手例子(10)
程序员文章站
2022-05-14 09:50:20
55、学习使用按位取反~。 程序分析:~0=1; ~1=0; (1)先使a右移4位。 (2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4) (3)将上面二者进行&运算。 56、画图,学用circle画圆形。 57、画图,学用line画直线。 58、画图,学用rectangle画方形。 程 ......
55、学习使用按位取反~。
程序分析:~0=1; ~1=0;
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
#python3.7 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)
56、画图,学用circle画圆形。
#python3.7 from tkinter import * if __name__ == '__main__': canvas = canvas(width = 800, height = 600, bg = 'purple') 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 += 1 j += 0.3 mainloop()
57、画图,学用line画直线。
#python3.7 from tkinter import * if __name__ == '__main__': canvas = canvas(width=300, height=300, bg='gold') canvas.pack(expand=yes, fill=both) x0 = 263 y0 = 263 x1 = 275 y1 = 275 for i in range(19): canvas.create_line(x0, y0, x1, y1, width=1, fill='red') x0 = x0 - 5 y0 = y0 - 5 x1 = x1 + 5 y1 = y1 + 5 x0 = 263 y0 = 263 y1 = 275 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)
参数说明:(left ,top )为矩形的左上坐标,(right,bottom)为矩形的右下坐标,两者可确定一个矩形的大小。
#python3.7 from tkinter import * if __name__ == '__main__': root = tk() root.title('canvas') canvas = canvas(root, width = 400, height = 400, bg = 'yellow') x0 = 263 y0 = 263 x1 = 275 y1 = 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个数,每个数分解出个位,十位,百位。
#python3.7 from tkinter import * import math if __name__ == '__main__': 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) b = 0.809 for i in range(16): a = 2 * math.pi / 16 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、计算字符串长度。
#python3.7 sstr1 = 'strlen' print(len(sstr1))
参考资料:
1、python 100例
2、python3图形界面简单案例(tkinter):https://blog.csdn.net/lyhdream/article/details/17514431
上一篇: Vue文件封装日历组件
下一篇: 短时间内难普及!折叠屏手机:概念大于实用