Python practice example (10)

55. Learn to use bitwise inversion ~.

Program analysis: ~ 0 = 1; ~ 1 = 0
(1) first shift a to the right by 4 digits.  
(2) set a number with the lower 4 bits all being 1 and the rest all being 0. Available ~ (~ 0 < < 4)
(3) the above two are calculated.

#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. Draw a picture and learn to draw a 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. Draw a picture and learn to draw a straight line with 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. Draw a picture. Learn to draw a square with rectangle.    

Program analysis: rectangle(int left, int top, int right, int bottom)

Parameter Description: (left, top) is the upper left coordinate of the rectangle, (right,bottom) is the lower right coordinate of the rectangle, both of which can determine the size of a rectangle.

#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. Drawing and comprehensive examples.   

Program analysis: use for loop to control 100-999 numbers. Each number is decomposed into bits, tens and hundreds.

#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. Calculate the string length.

#python3.7

sStr1 = 'strlen'
print(len(sStr1))

 

 

reference material:

1. 100 Python cases

2. tkinter: https://blog.csdn.net/lyhdream/article/details/17514431

Tags: Python REST

Posted on Mon, 02 Dec 2019 12:23:28 -0500 by TAViX