python3 100例 一码人学习笔记(51-60)

题目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 . . .

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值