题目51:学习使用按位与 & 。
if __name__ == '__main__':
a = 1
b = 3
c=a&b
print ('a & b = %d' % c)
b &= 0
print ('a & b = %d' % b)
a & b = 1
a & b = 0
Press any key to continue . . .
题目52:学习使用按位或 | 。
a | b = 3
a | b = 0
Press any key to continue . . .
题目53:学习使用按位异或 ^ 。
if __name__ == '__main__':
a = 1
b = a ^ 3
print( 'The a ^ 3 = %d' % b)
b ^= 7
print ('The a ^ b = %d' % b)
The a ^ 3 = 2
The a ^ b = 5
Press any key to continue . . .
题目54:取一个整数a从右端开始的4〜7位。
if __name__ == '__main__':
a = int(input('input a number:\n'))
b = a >> 4
c = ~(~0 << 4)
d = b & c
print( '%o\t%o' %(a,d))
input a number:
9
11 0
Press any key to continue . . .
题目55:学习使用按位取反~。
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)
The a's 1 complement is -235
The a's 2 complement is -235
Press any key to continue . . .
题目56:画图,学用circle画圆形。
f __name__ == '__main__':
import turtle
turtle.title("画圆")
turtle.setup(800,600,1,2)
pen=turtle.Turtle()
pen.color("red")
pen.width(1)
pen.shape("turtle")
pen.speed(5)
pen.circle(100)
题目57:画图,学用画方块。
import turtle
def drawline(n):
t=turtle.Pen()
t.color(0.3,0.8,0.6) #设置颜色,在0--1之间
t.begin_fill() #开始填充颜色
for i in range(n): #任意边形
t.forward(50)
t.left(360/n)
t.end_fill() #结束填充颜色
drawline(4)
题目58:计算字符串长度。
sStr1 = 'strlen'
print (len(sStr1))
6
题目59:打印出杨辉三角形
l=[1]
for i in range(10):
for k in l:
print(k,end='\t')
print('\n ')
l1=l[:]
l1.insert(0,0)
l1.append(0)
l2=[]
for i in range(len(l1)-1):
l2.append(l1[i]+l1[i+1])
l=l2[:]
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Press any key to continue . . .
题目60:查找字符串。
sStr1 = 'abcdefg'
sStr2 = 'cde'
print (sStr1.find(sStr2))
2
Press any key to continue . . .