觉得分着发比较麻烦了,所以索性一个文章里发,与之前的格式相同
其五十一
这里是学习与或非,比较基础,就是0和1会得到什么的问题
学习使用按位与 &
a = 1 b = 0 print(a&b,a&a,b&b)
结果为
0 1 0
其五十二
学习使用按位或 |
a = 1 b = 0 print(a|b,a|a,b|b)
结果为
1 1 0
其五十三
学习使用按位异或 ^
a = 1 b = 0 print(a^b,a^a,b^b)
结果为
1 0 0
其五十四
取一个整数a从右端开始的4〜7位
我觉得这道题的题目就很奇怪,第四到七位至少也是4个数啊,它的输出只有两个数字,完全不明白什么意思
所以我就按照字面意思做吧,一个大整数的千到百万的数值
i = int(input("输入一个整数:")) if i/1000 < 0: print("4-7位都是0") else: a = (i//1000)%10000 b = a // 1000 c = (a-b*1000)//100 d = (a-b*1000-c*100)//10 e = a-b*1000-c*100-d*10 print("4位是",e,"5位是",d,"6位是",c,"7位是",b)
结果为
输入一个整数:1234567
4位是 4 5位是 3 6位是 2 7位是 1
其五十五
学习使用按位取反~
-6的补码是+6(0000 0110)取反后再+1,为(1111 1001)+(0000 0001)=(1111 1010), ,也就是计算机中-6是用(1111 1010)来存储的,(1111 1010) 按位取反得到(0000 0101)这就是答案5
注意取反后第一位为1是负数
a = 0 b = 1 c = -7 d = 7 print(~a,~b,~c,~d)
结果为
-1 -2 6 -8
其五十六
画图,学用circle画圆形
https://docs.python.org/zh-cn/3/library/turtle.html
import turtle pen = turtle.pen() turtle.down() turtle.speed(1) turtle.circle(100) turtle.mainloop()
结果为
其五十七
画图,学用line画直线
import turtle pen = turtle.pen() turtle.down() turtle.color("blue") turtle.speed(1) turtle.forward(100) turtle.mainloop()
结果为
其五十八
画图,学用rectangle画方形
import turtle pen = turtle.pen() turtle.down() turtle.color("blue") turtle.speed(1) turtle.pensize(3) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.mainloop()
结果为
其五十九
画图,综合例子
import turtle from turtle import * import random import time n = 100.0 speed("fastest") screensize(bg='seashell') left(90) forward(3*n) color("orange", "yellow") begin_fill() left(126) for i in range(5): forward(n/5) right(144) forward(n/5) left(72) end_fill() right(126) color("dark green") backward(n*4.8) def tree(d, s): if d <= 0: return forward(s) tree(d-1, s*.8) right(120) tree(d-3, s*.5) right(120) tree(d-3, s*.5) right(120) backward(s) tree(15, n) backward(n/2) for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() up() forward(b) left(90) forward(a) down() if random.randint(0, 1) == 0: color('tomato') else: color('wheat') circle(2) up() backward(a) right(90) backward(b) turtle.mainloop()
结果为
其六十
计算字符串长度
a = input("输入字符串:") b = len(a) print(b)
结果为
输入字符串:sdfghjk
7