python 3.7 及jupyter notebook实现菜鸟教程100例习题40-60,共20道常用例题
41、题目:模仿静态变量的用法。
def varfun():
var = 0
print('var = %d'%var)
var += 1
if __name__ == '__main__':
for i in range(3):
varfun()
class Static:
StaticVar = 5
def varfun(self):
self.StaticVar += 1
print(self.StaticVar)
#输出StaticVar=5
print(Static.StaticVar)
a = Static()
for i in range(3):
a.varfun()
42、题目:学习使用auto定义变量的用法。
num = 2
def autofunc():
num = 1
print('internal block num = %d'%num)
num += 1#此句不执行
for i in range(3):
print('the num = %d'%num)
num += 1
autofunc()
43、题目:模仿静态变量(static)另一案例。
class Num:
nNum = 1
def inc(self):
self.nNum += 1
print('nNum = %d'%self.nNum)
if __name__ == '__main__':
nNum = 2
inst = Num()#调用Num类
for i in range(4):
nNum += 1
print('The num = %d'%nNum)
inst.inc()#调用类里面的Inc函数
44、两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
x = [
[1,2,3],
[2,4,5],
[2,6,5]
]
y = [
[2,3,4],
[1,3,4],
[5,6,7]
]
for i in range(3):
for j in range(3):
x[i][j] += y[i][j]
print(x)
45、题目:统计 1 到 100 之和。
sum =0
for i in range(1,101):
sum += i
print(sum)
46、题目:求输入数字的平方,如果平方运算后小于 50 则退出。
num = int(input('请输入一个数字:'))
num = num ** 2
flag = 1
while flag:
if num < 50:
flag = 0
print(num)
else:
flag =1
47、题目:两个变量值互换。
a = 5
b = 8
a,b = b,a
print('a = ',a)
print('b = ',b)
48、题目:数字比较。
a = 10
b = 20
if a>b:
print('%d大于%d'%(a,b))
elif a == b:
print('%d等于%d'%(a,b))
elif a < b:
print('%d小于%d'%(a,b))
49、题目:使用lambda来创建匿名函数。
#lambda 函数 x,y表示两个未知量,冒号后面为表达式
maxmum = lambda x,y: (x > y)*x +(x < y)*y
minmum = lambda x,y:(x > y)* y+(x < y)*x
if __name__ == '__main__':
a = 10
b = 20
print('the larger one is %d'%maxmum(a,b))
print('the lower one is %d'%minmum(a,b))
50、题目:输出一个随机数。
import random
a = random.uniform(1,5)#随机生成指定范围内的浮点数
b = random.random()#随机生成0-1之间的浮点数
c = random.randint(2,8)#随机生成指定范围内的整型
print(a,b,c)
51、题目:学习使用按位与 & 。
#3(11) 2(10) 5(101)
a = 3
b = a &2
c = b &5
print(b,c)
52、题目:学习使用按位或 |
a = 5
b = a|3
c |= 5
print(b,c)
53、题目:学习使用按位异或 ^ 。
a = 8
b = a^2
c ^= 3
print(b,c)
54、题目:取一个整数a从右端开始的4〜7位。
a = int(input('请输入一个数字:'))
b = a >> 4
c = ~(~0 << 4)
d = b&c
print(d)
55、题目:学习使用按位取反~。
a = int(input('请输入一个数字:'))
b = ~a
print(b)
56、题目:画图,学用circle画圆形。
python 3版本之后的Tkinter 库为小写
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画方形。
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、题目:画图,综合例子。
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、题目:计算字符串长度。
string = 'tom','peter','alice','pam','pamile'
print(len(string))