python小白——进阶之路——day3天-———运算符

(1)算数运算符:  + - * / // % **

(2)比较运算符:  > < >= <= == !=  

(3)赋值运算符:  = += -= *= /= //= %= **=

(4)成员运算符:  in 和 not in (针对于容器型数据)

(5)身份运算符:  is 和 is not (检测两个数据在内存当中是否是同一个值) 

(6)逻辑运算符:  and or not

(7)位运算符:    & | ~ ^ << >>

 

 

# 1. 算数运算符:  + - * / // (地板除) % **(幂运算)

 

var1 = 7

var2 = 4

 

# +

res = var1 + var2

print(res)

 

# -

res = var1 -var2

print(res)

 

# *

res = var1 * var2

print(res)

 

# / (结果是一个小数)

res = var1 / var2

res = 8 / 8

print(res)

 

# // (取整数) 两个数在进行地板除的时候,有一个数是小数,就在最后的结果上加.0

res = var1 // var2

res = 8 // 8

res = 28.7 // 5

print("<===>")

res = 28 // 5.3

print(res)

 

# %

res = var1 % var2

res = 99 % 7

# -1 + 7 = 6 (余数)

res = -99 % 7

# 1 + (-7) = -6 (余数)

res = 99 % -7

print(res)

 

# **   var1 * var1 * var1  7*7*7  **是所有运算符 优先级最高的一个

res = var1 ** 3

print(res)

 

 

# ###(2)比较运算符:  > < >= <= == !=

'''比较运算符只会产生2个结果  要么True 要么False'''

 

# >

var1 = 15

var2 = 18

res = var1 > var2

print(res)

# <

res = var1 < var2

print(res)

# >=  只要满足一个条件即为真

res = var1 >= 15

print(res)

# <=

res = var1 <= 15

print(res)

# == *****

res = var1 == var2

print(res)

 

# != *****

res = var1 != var2

print(res)

 

 

# ### 3.赋值运算符  = += -= *= /= //= %= **=

 

# = 所有运算符当中 等号的优先级最低

var1 = 9

var2 = 5

res = var1

# print(res)

 

# +=

# var1 += var2

'''var1 = var1 + var2'''

# print(var1)

 

# -=

# var1 -= var2

'''var1 = var1 - var2'''

# print(var1)

 

# *=

# var1 *= var2

'''var1 = var1 * var2'''

# print(var1)

 

# /=

# var1 /= var2

'''var1 = var1 / var2'''

# print(var1)

 

# //=

# var1 //= var2

'''var1 = var1 // var2'''

# print(var1)

 

# %=

# var1 %= var2

'''var1 = var1 % var2'''

# print(var1)

 

# **=

var1 **= var2

'''var1 = var1 ** var2'''

print(var1)

 

 

 

# ###4.成员运算符  in    not in 针对于容器类型数据

# str  字符串需要是一个连续的片段

 

strvar = "如果遇到你是一种错,我宁愿一错再错"

res = "你" in strvar

res = "遇到" not in strvar

res = "我一" in strvar

print(res)

 

 

# list tuple set

listvar = ["张胜男","张三","李四"]

res = "达到" in listvar

print(res)

 

tuplevar = "张三","李四","王五"

res = "王大" not in tuplevar

print(res)

 

setvar = {'王五','张三"'}

res =  "王五" in  setvar

print(res)

 

# dict  注意 : 判断的是键 不是值

dictvar = {"zdx":"王五","gss":"张三","mlh":"李四"}

res = "李四" in dictvar

res = "mlh" not in dictvar

print(res)

 

 

# ### 身份运算符  is  is not

"""

var1 = 56

var2 = 56

res = var1 == var2  (判断两个值是否相等)

res = var1 is var2  (判断两个值得地址是否相等)

"""

# Number

# int -5 ~ 正无穷

var1 = 56

var2 = 56

res = var1 is var2

print(res)

 

# float 非负数

var1 = -99

var2 = -99

print(var1 is not var2)

 

# bool

var1 = True

var2 = False

print(var1 is var2)

 

# complex

var1 = 5+6j

var2 = 5+6j

print(var1 is not var2)

 

# 容器类型数据  () 相同的字符串 剩下所有的容器类型数据地址都不一样

var1 = ()

var2 = ()

print(var1 is  var2)

var1 = "机"

var2 = "机"

print(var1 is not var2)

 

 

# ### 6. 逻辑运算符 (and or not)

# (1)and 逻辑与

'''全真则真,一假则假'''

res = True and True

res = False and True

res = False and False

res = True and False

print(res)

 

# (2)or 逻辑或

'''全假则假,一真则真'''

res = True or True

res = True or False

res = False or True

res = False or False

print(res)

 

# (3)not 逻辑非  相当于取反

res = not True

res = not False

print(res)

 

 

# (4)逻辑短路

'''

False and 布尔 在and左边如果时False 逻辑短路 右侧不执行

True or 布尔   在or左边如果是True  逻辑短路 右侧不执行

在开发当中 ,当成"开关"使用

# res = False and False

'''

False and  print(123) #逻辑短路

True or print(456)  #逻辑短路

True and print(789)

# res = print(456)

# print(res)

 

 

# 逻辑运算符的优先级

''' () > not > and > or '''

# res = 6 and 7

# res = True and  True

# print(res)

# res = 5 or 6 and 7

# res = 5 or True

# res = 5 or True

# (1)

res = 5 or 6 and 7

print(res)

# (2)

res = (5 or 6) and 7

print(res)

# (3)

res = not(5 or 6) and 7

print(res) # False

'''

5 or 6 => 5

not 5 and 7

False and 7

'''

# (4)

res = 3>1 or 4<5 and 6>9 or 8==10

# True or True and False or False

# True or False or False

# True or False

print(res)

 

# 数据类型的判断 isinstance

'''

isinstance(要判断的值,(数据类型1,数据类型2,数据类型3,.....))

最终的返回值要么是True 要么是False

int float bool complex str list set tuple dict

'''

var1 = 6

res = isinstance(var1,int)

print(res)

var2 = "你好"

# 只要类型元组里面含有这个数据类型,即为真

res = isinstance(var2,(set,list,tuple))

print(res)

 

 

 

# ### 7.位运算符 (& |  ^ << >> ~)

 

# 按位与 &

var1 = 19

var2 = 15

res = var1 & var2

print(res)

"""

    000000010011

    000000001111

    000000000011

"""

# 按位或 |

res = var1 | var2

print(res)

'''

    000000010011

    000000001111

    000000011111

'''

 

# 按位异或 ^ "如果两者不相同,返回真,两者相同返回假

res = var1 ^ var2

print(res)

'''

    000000010011

    000000001111 

    000000011100

'''

 

# <<  相当于 5 乘以 2的2次幂

res = 5 << 2

res = 4 << 3

print(res) # 20

"""

000000101

000010100

"""

 

# >>  相当于 5 除以 2的1次幂

res = 5 >> 1

print(res)

"""

0000101

0000010

"""

 

# ~ 按位非 (针对于补码来进行操作 ,连符号位都要改变) 公式 -(n+1)

res = ~19

print(res)

"""

000000000010011 (补码)

111111111101100 (按位非)

 

111111111101100  (给补码求原码)

100000000010011  (反码)

100000000010100  (原码)  => -20

"""

 

res = ~(-19)

print(res)

'''

100000000010011  (原码)

111111111101100  (反码)

111111111101101  (补码)

 

按位非操作的是补码

111111111101101  (补码)

000000000010010  (按位非得值)

 

000000000010010  (原码 == 反码 == 补码)

'''

 

"""

所有运算符的优先级:

(1) 小括号的优先级最高 ()     => (3+3)*5

(2)

一元运算符:同一时间操作一个数的 ( ~ , - ) -6

二元运算符: 同一时间操作二个数的

 

** 符号优先级最高

符号优先级最低

 

整体来说,一元运算符优先级大于二元运算符 , ** 除外

乘,除 > 加减

() > not > and > or

(<< >> ) > & > ^ > |

 

 

除此之外剩下的运算符参考以下:

算术运算符 > 位运算符 > 比较运算符 > 身份运算符 > 成员运算符 > 逻辑运算符

赋值运算符单独拿出来 , 用来把最后的结果进行赋值的

"""

 

"""

res = 5+5 << 6 // 3 is 40 and True

res = 5+5 << 2 is 40 and True

res = 10 << 2 is 40 and True

res = 40 is 40 and True

res = True and True

res = True

print(res)

"""

转载于:https://www.cnblogs.com/-sch-593057927/p/10787160.html

你可能感兴趣的:(python小白——进阶之路——day3天-———运算符)