数学运算
数学运算 加减乘除,整除,取余 、赋值+= -= *= /= //= %=
**乘方 (a **2)a的2次方
/ 真除法(除尽为止)
// 整除(只取整数部分)
% 取余 (只取余数部分)
#数学运算 加减乘除,整除,取余,结果是一个新的数值
a=15
b=7
print("a =",a)
print("b =",b)
print("a + b =",a+b)
print("a - b =",a-b)
print("a * b =",a*b)
print("a / b =",a/b)
#整除
print("a // b =",a//b)
#取余
print("a % b =",a%b)
#赋值+= -= *= /= //= %=
print()
print("a =",a)
print("b =",b)
b -= 2 ### b -= 2 等于 b = b-2
print("b -=2 b=",b,type(b))
b*=2
print("b *=2 b=",b,type(b))
b/=2
print("b /=2 b=",b,type(b))
b//=2
print("b //=2 b=",b,type(b))
b%=2
print("b %=2 b=",b,type(b))
逻辑运算
在python中的逻辑运算,有与运算(and)、或运算(or)、非运算(not)
and和not都是二元运算符,而not是一元运算。
逻辑运算(结果只有真和假)True与False 或者参与运算的两个值之一
逻辑运算(结果只有真和假)True与False 或者参与运算的两个值之一
逻辑运算(结果只有真和假)True与False 或者参与运算的两个值之一
#与运算 and
print("True and True","---->",True and True)
print("True and False","---->",True and False)
print("False and True","---->",False and True)
print("False and False","---->",False and False)
#或运算 or
print("True or True","---->",True or True)
print("True or False","---->",True or False)
print("False or True","---->",False or True)
print("False or False","---->",False or False)
#非运算 not
print("not True","---->",not True)
print("not False","---->",not False)
两个值相与,如果第一个值为真,那么返回第二个值;如果第一值为假,那么返回第一个值
两个值相或,如果第一个值为真,那么返回第一个值;如果第一值为假,那么返回第二个值
a = 20
b = 5
print("\na =",a," b =",b)
print("\nb > 10 and a ",b > 10 and a)
print("b > 10 or a ",b > 10 or a)
print("b < 10 and a ",b < 10 and a)
print("b < 10 or a ",b < 10 or a)
print("\nb > 10 or a >10 ",b > 10 or a > 10)
关系运算
关系运算的结果只有True或False
关系运算有:
判断相等 ( == )
is运算
大于(>)、小于(<)、大于等于(>=)、小于等于(<=)、不等于(!=)
in 或not in
== 和is的区别在后面的文章将会有
如果两个变量值相等,只要是int\float\tuple\str,那么is的结果就是真,即是同一内存
只有int\float\tuple\str才能作为字典的key
在文件中运行,如果在交互模式下,每一行就是一个编译环境
如果两个变量独立赋值,值相等,只要是list\dict\set,那么is的结果就是假
#判断相等
a=10.0
b=5+5
c=5.0+5
print(a==b)
#is运算
#> < >= <= !=
#in,not in
print('\nis判断')
print("\n整数")
a = 2
b = 2
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nfloat")
a = 3.2
b = 3.2
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\ntuple")
a = (1,2.3)
b = (1,2.3)
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\ntuple")
a = "abcde"
b = "abcde"
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nlist")
a = [3,4,5]
b = [3,4,5]
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\ndict")
a = {1:100,2:200}
b = {1:100,2:200}
print("a =",a," b =",b)
print ("a is b ",a is b)
print("\nset")
a = {100,200}
b = {100,200}
print("a =",a," b =",b)
print ("a is b ",a is b)
print()
a = 10
b = 12
print('大小判断')
print("a =",a," b =",b)
print("a < b ",a b ",a>b)
print("a <= b ",a<=b)
print("a <= b ",a<=b)
print("a != b ",a!=b)
a =3
b =5
c ={1,2,3}
print()
print('in或not in 判断')
print("a =",a," b =",b)
print("c =",c)
print("a in c ",a in c)
print("b in c ",b in c)
print("a not in c ",a not in c)
print("b not in c ",b not in c)
输出结果
True
is判断
整数
a = 2 b = 2
a is b True
float
a = 3.2 b = 3.2
a is b True
tuple
a = (1, 2.3) b = (1, 2.3)
a is b True
tuple
a = abcde b = abcde
a is b True
list
a = [3, 4, 5] b = [3, 4, 5]
a is b False
dict
a = {1: 100, 2: 200} b = {1: 100, 2: 200}
a is b False
set
a = {200, 100} b = {200, 100}
a is b False
大小判断
a = 10 b = 12
a < b True
a > b False
a <= b True
a <= b True
a != b True
in或not in 判断
a = 3 b = 5
c = {1, 2, 3}
a in c True
b in c False
a not in c False
b not in c True
位运算
位运算只用在整数中
位运算,得到的结果是一个整数
位运算种类:
按位与 &
按位或 |
按位求反 ~
异或 ^
右移位>>
左移位 <<
#按位与&
a=18
b=20
print("a=18")
print("b=20")
print("a的二进制形式是 ",bin(a))
print("b的二进制形式是",bin(b))
print("a&b =",a&b)
c =65
d =98
print ("c&d =",c&d)
#按位或|
print()
print("a|b =",a|b,bin(a|b))
##########################1、把参与运算的数转为二进制形式
##########################2、按照二进制的形式进行与或非运算
##########################3、把结果转换回10进制
#按位求反~
#######################按10进制形式看是正负互换,再减1
print()
print("~b =",~b)
print("~87 =",~87)
#按10进制形式看是正负互换,再减1
#异或^
print()
print("a^b =",a^b)
print("8^87 =",8^87)
######################右移位>>,相当于整除2的N次方
print()
print("a>>2 =",a>>2)
print("43>>3 =",43>>3)
#######################左移位<<,相当于乘以2的N次方
print()
print("a<<2 =",a<<2)
print("7<<3 =",7<<3)