运算符
逻辑运算符
Python逻辑运算符
Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:
运算符 |
逻辑表达式 |
描述 |
实例 |
and |
x and y |
布尔”与” - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 |
(a and b) 返回 20 |
or |
x or y |
布尔”或” - 如果 x 是 True,它返回 True,否则它返回 y 的计算值。 |
(a or b) 返回 10 |
not |
not x |
布尔”非” - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 |
not(a and b) 返回 False |
关系运算符
Python比较运算符
以下假设变量a为10,变量b为20:
运算符 |
描述 |
实例 |
== |
等于 - 比较对象是否相等 |
(a == b) 返回 False |
!= |
不等于 - 比较两个对象是否不相等 |
(a != b) 返回 True |
<> |
不等于 - 比较两个对象是否不相等 |
(a <> b) 返回 true,这个运算符类似 != |
> |
大于 - 返回x是否大于y |
(a > b) 返回 False |
< |
小于 - 返回x是否小于y |
所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 (a < b) 返回 true。 |
> = |
大于等于 - 返回x是否大于等于y |
(a >= b) 返回 False |
<= |
小于等于 - 返回x是否小于等于y |
(a <= b) 返回 true |
a>b>c
等价a>b and b>c
字符串
1 2 3 4 5 6 7 8 9 10 11
| bt1="ab备胎" bt2="ac备胎" bt3="aa备胎" print(bt1!=bt2) print(bt1==bt2) print(bt1 <bt2) print(bt2 >bt1) print(ord("0")) print(ord('a')) print(ord('b')) print(ord('z'))
|
身份运算符
Python身份运算符
身份运算符用于比较两个对象的存储单元
运算符 |
描述 |
实例 |
is |
is 是判断两个标识符是不是引用自一个对象 |
x is y , 如果 id(x) 等于 id(y) , is 返回结果 1 |
is not |
is not 是判断两个标识符是不是引用自不同对象 |
x is not y , 如果 id(x) 不等于 id(y). is not 返回结果 1 |
运算符优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| print(1+2*2/3>2*(3-2)+1)
print( (1+2*2/3) > (2*(3-2)+1 ) )
print(1<2) print(1<2>0) print(1<2<0) print(5>3) print(5>3>2) print(5>3<2)
|
习题小结
round()
四舍五入
默认四舍五入到整数
round(data,n)
四舍五入到小数点后n位
timetime
生成随机数字,字符的方法
分支语句
if
语句
if
会自动转化Bool
类型
1 2 3 4 5 6 7 8 9 10 11
| print(int(True)) print(int(False)) print(bool(4)) print(bool(1)) print(bool(0)) print(bool(-1)) print("str",bool("")) print("str",bool("1"))
if None: print("hello python")
|
if else
语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| name=input("please input name") age= eval(input("please input age")) tall=eval(input("please input tall")) star=input("please input star") pos=input("please input pos")
if pos!="北京": print("地址不符合,淘汰") else: if age <=40: print("年龄不和,淘汰") else: if tall<=170: print("身高不行,淘汰") else: if star!="天蝎座": print("星座不符合,淘汰") else: print("符合要求",name)
|
if elif else
语句
顺序注意隐含条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| if score>750 and score<0: print("error") elif score>700: print("清北") elif score>600: print("985") elif score>580: print("211") elif score >550: print("1本") elif score> 500: print("二本") elif score>400: print("三本") elif score>300: print("大专") else: print("山东蓝翔")
|
逆序必须限定范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| if score>750 and score<0: print("error") elif score>300 and score<=400: print("大专") elif score>400 and score<=500: print("三本") elif score> 500 and score<=550: print("二本") elif score >550 and score<=580: print("1本") elif score>580 and score<=600: print("211") elif score>600 and score<=700: print("985") elif score>700: print("清北") else: print("山东蓝翔")
|
if
嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| name=input("please input name") age= eval(input("please input age")) tall=eval(input("please input tall")) star=input("please input star") pos=input("please input pos")
if pos!="北京": print("地址不符合,淘汰") else: if age <=40: print("年龄不和,淘汰") else: if tall<=170: print("身高不行,淘汰") else: if star!="天蝎座": print("星座不符合,淘汰") else: print("符合要求",name)
|
绘图实战
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import turtle x1,y1=eval(input("circle center")) R=eval(input("input R")) x2,y2=eval(input("pos x,y")) turtle.write("0,0")
turtle.goto(x1,y1) turtle.write("center")
turtle.penup() turtle.goto(x1,y1-R) turtle.pendown() turtle.circle(R)
turtle.penup() turtle.goto(x2,y2) turtle.pendown() turtle.pensize(3) turtle.goto(x1,y1)
Rt= ((x1-x2)**2 +(y1-y2)**2)**0.5 if Rt==R : turtle.write("在圆形上") elif Rt<R : turtle.write("在圆形内") else: turtle.write("在圆形外") turtle.done()
|
空格用途
诸如表达语句层次感
1 2 3
| 王涛="天才" if 王涛=="天才": print("天生的唇彩")
|
代码中很多错误都是缩进不对成
空语句作用
pass
数学库math
random
随机数
1 2 3
| a<=randomint(a,b) <=b num2=random.randint(0,100) num1=random.randrange(0,10)
|