- 运算
- 算术运算
- +,-,*,/四则运算
- %取模取余
- **,幂运算,返回x的y次幂;// ,取商的整数部分,整除
print(2**6) # 64
print(16//6)# 2
- 比较运算
- >,<,>&#61;,<&#61;,!&#61;,<>(py3中已去除)
- &#61;&#61; 是否相等
- 赋值运算
- &#61;
- &#43;&#61;&#xff0c;-&#61;&#xff0c;*&#61;&#xff0c;/&#61;&#xff0c;%&#61;&#xff0c;**&#61;&#xff0c;//&#61;
- 逻辑运算
- 与(and)&#xff0c;或(or)&#xff0c;非(not)
- 成员身份运算
- in、not in 是否存在指定序列中
- is、is not 是否引用同一对象
- 位运算
- 按位与 &
- 按位或 |
- 按位异或 ^
- 按位取反 ~
- 左移 <<
- 右移 >>
#!/usr/bin/python
a &#61; 60 # 60 &#61; 0011 1100
b &#61; 13 # 13 &#61; 0000 1101
c &#61; 0c &#61; a & b; # 12 &#61; 0000 1100
print "Line 1 - Value of c is ", cc &#61; a | b; # 61 &#61; 0011 1101
print "Line 2 - Value of c is ", cc &#61; a ^ b; # 49 &#61; 0011 0001 #相同为0&#xff0c;不同为1
print "Line 3 - Value of c is ", cc &#61; ~a; # -61 &#61; 1100 0011
print "Line 4 - Value of c is ", cc &#61; a <<2; # 240 &#61; 1111 0000
print "Line 5 - Value of c is ", cc &#61; a >> 2; # 15 &#61; 0000 1111
print "Line 6 - Value of c is ", c
- 运算优先级
- 算术运算
- 分支结构
- 单分支与多分支
#单分支
if True:print(&#39;条件成立了&#39;)a &#61; 3
b &#61; 5
#多分支
if a>b:print(a,&#39;>&#39;,b)
elif a&#61;&#61;b:print(a,&#39;&#61;&#39;,b)
else:print(a,&#39;<&#39;,b)print(&#39;殊途同归&#39;)
- 单分支与多分支
- 循环结构
- while
#死循环
while True:print(&#39;条件成立&#xff0c;开始循环了&#39;)a &#61; 3
while a<&#61;3:print(a)a&#43;&#61;1
else:print(&#39;循环结束了&#39;) - for
for i in range(10):print("loop:", i )
- continue和break
#遇到小于5的循环次数就不走了&#xff0c;直接跳入下一次循环
for i in range(10):if i<5:continue #不往下走了,直接进入下一次loopprint("loop:", i )#遇到大于5的循环次数就不走了&#xff0c;直接退出
for i in range(10):if i>5:break #不往下走了,直接跳出整个loopprint("loop:", i )
- while