1.运算符
1.1 比较运算符
<,小于 >,大于 <=,小于等于 >=, 大于等于 !=, 不等于 ==,等于 比较结果为True,Flase
举例:从键盘输入三个数,输出最大值
num_1=int(input("please input first number>>:"))
num_2=int(input("please input second number>>:"))
num_3=int(input("please input third number>>:"))
num_max=num_1
if num_1>=num_2:
if num_1<=num_3:
num_max =num_3
elif num_2>=num_3:
num_max=num_2
else:
num_max=num_3
print(num_max)
1.2 赋值运算符
=
复合赋值运算符:+=, -=, *=, /=, //=, %=, **= 如:num+=1等价于num=num+1
1.3逻辑运算符
not,逻辑非运算符 and,逻辑与运算符 or,逻辑或运算符
做先级:not>>and>>or
逻辑运算符短路原则:1 or x 都等于1,x的值不需计算;0 and x 都等于0,x的值不需计算
2.运算符优先级
以下表格列出了从最低到最高优先级的所有运算符:
序号 | 运算符 | 说明 |
1 | or | 逻辑或运算符 |
2 | and | 逻辑与运算符 |
3 | not | 逻辑非运算符 |
4 | in , not in |
成员测试 |
5 | is , is not |
成员测试 |
6 | <,< = ,>,> = ,! = , = = |
比较运算符 |
7 | | | 按位或 |
8 | ^ | 按位异或 |
9 | & | 按位与 |
10 | << ,>> | 移位运算符 |
11 | + , - |
加法与减法 |
12 | * , / ,//, % |
乘法、除法、取整与取余 |
13 | ~,+ , -
|
按位到反、正负号 |
14 | ** |
乘方 |
3.循环语句
3.1for循环
当我们知道循环次数的时候使用for循环
for循环的语法格式如下:
for iterating_var in sequence: statements(s)
3.2while循环
当我们知道循环条件的时候使用while循环
例如:输出九九乘法表
1 num_1=1 2 while num_1<=9: 3 num_2=1 4 while num_2<=num_1: 5 print(num_1,"*",num_2,"=",num_1*num_2,end="\t") 6 num_2+=1 7 num_1+=1 8 print(end="\n") #相当于print() 9 else: 10 print("----END----")
3.3do……while循环
当循环运行直到条件不满足的时候用do……while循环
3.4break、continue