python循环tuple Python循环左移
转载
占位符:%s %d %f %r
分别代表:str, int num, float num, raw str
位运算,将字符转换成二进制后进行运算
&: 与
|: 或
^: 异或
~: 按位取反
>>: 右移,移动1位等于除以2,2位等于除以4
<<: 左移,移动1位等于乘以2,2位等于乘以4
a b & | ^
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
msg = '%s %d %f %r' %('sd\nf', 123, 123.123, '\n\t')
print(msg)
# 二进制运算
# 128 64 32 16 8 4 2 1
a = 20 # 20 = 0001 0100
b = 13 # 13 = 0000 1101
c = a & b
d = a | b
e = a ^ b
f = ~a
g = a >> 2 # 0001 0100 -> 0101 0000
h = a << 2 # 0001 0100 -> 0000 0101
# 0001 0100 = 20
# 1110 1011 = 235
# 255 - 235 = -20
print(c,d,e,f,g,h)
# for循环,尝试10次猜年龄,第三次的时候选择是否要继续玩
AGE = 20
for i in range(10):
print("you have %d times left" %(10-i))
guess_age = int(input("Please guess jeff's age: "))
if guess_age == AGE:
print("You are right!")
break
elif guess_age < AGE:
print("guess smaller")
else:
print("guess lager")
if i == 2:
print("too many tries")
Yn = input("Do you want to coutine?(Yes/No)")
if Yn == 'Yes' or Yn == 'Y' or Yn == 'y':
pass
elif Yn == 'No' or Yn == 'N' or Yn == 'n':
break
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。