Python经典基础编程练习题(六)——每日10题

1.统计 1 到 100 之和

sum=0
for i in range(1,101):
    sum+=i

print(sum)

2.求输入数字的平方,如果平方运算后小于 50 则退出

n= int(input('请输入一个数:'))
n=n*n
if n< 50:
    print('平方和小于50,退出')
else:
    print(n)

3.两个变量值互换

a= int(input('请输入第一个数:'))
b= int(input('请输入第二个数:'))
print(a,b)
a,b=b,a
print(a,b)

4.数字比较

a= int(input('请输入第一个数:'))
b= int(input('请输入第二个数:'))
if a > b:
    print('a比b大')
else:
    print('b比a大')

5.了解 lambda匿名函数

MAX = lambda x, y: (x > y) * x + (x < y) * y
MIN = lambda x, y: (x > y) * y + (x < y) * x
a = 10
b = 20
print('The largar one is %d' % MAX(a, b))
print ('The lower one is %d' % MIN(a, b))

6.生成随机数

import random
print(random.uniform(1,100))

7.按位与&,|,^(变成二进制,分别按位与,或,异或)

a=3
b=7
print('a & b = %d' % b)
b &= 7
print('a & b = %d' % b)

b = a | 3
print ('a | b is %d' % b)
b |= 7
print ('a | b is %d' % b)

b = a ^ 3
print ('The a ^ 3 = %d' % b)
b ^= 7
print ('The a ^ b = %d' % b)

8.学会用tkinter画圆

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()

9.学会用tkinter画直线

rom 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()

10.学会用tkinter画正方形

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()

 

你可能感兴趣的:(Python编程练习)