Python Programming - mathematical, comparison, logical, relationships, assignments and other computing operations

Various conventional operator that:

  • Math - (+, -, *, /, //,% **)
  • Comparison operation - (==, =,>, <,> =, <=!)
  • Logical operations - (and, or, not)
  • Relational operators - (in, not in, is, is not)
  • Assignment operator - (=, + =, - =, * =, / =,% = = //, ** =)
  • Bit computing - (&, |, ^, ~, >>, <<)

1, math (+, -, *, /, //,% **):

num1=input("请输入第一个数:")
num2=input("请输入第二个数:")
num1=int(num1)
num2=int (num2)
print()
#数学运算   +,-,*,/,//,%,**
print ("数学运算")
print("两个数之和,",num1+num2)
print("两个数之差,",num1-num2)
print("两个数之积,",num1*num2)
#商
print("两个数之商,",num1/num2)
#整除
print("两个数之整除,",num1//num2)
#取余
print("两个数之商取余,",num1%num2)
#幂次方,即num1的num2的次方
print("两个数之乘方,",num1**num2)
 
 
结果如下:
请输入第一个数:10
请输入第二个数:6
 
数学运算
两个数之和, 16
两个数之差, 4
两个数之积, 60
两个数之商, 1.6666666666666667
两个数之整除, 1
两个数之商取余, 4
两个数之乘方, 1000000

2, the comparison operation (==, =,>, <,> =, <=!):

num1=input("请输入第一个数:")
num2=input("请输入第二个数:")
num1=int(num1)
num2=int (num2)
# 比较运算   ==,!=,>,<,>=,<=
print("比较运算")
print("num1==num2",num1==num2)
print("num1!=num2,",num1!=num2)
print("num1>num2 ,",num1>num2)
print("num1<num2,",num1<num2)
print("num1>=num2,",num1>=num2)
print("num1<=num2,",num1<=num2)
 
结果如下:
请输入第一个数:10
请输入第二个数:6
比较运算
num1==num2 False
num1!=num2, True
num1>num2 , True
num1<num2, False
num1>=num2, True
num1<=num2, False

3, logical operations (and, or, not):

  • and: the first is true, returns the second value;
  • or: the first is true, returns a value; the first is false, or a second value;
num1=input("请输入第一个数:")
num2=input("请输入第二个数:")
num1=int(num1)
num2=int (num2)
print("逻辑运算")
#and ,or ,not
print("num1 and num2=",num1 and num2)
print("num1 or num2=",num1 or num2)
print("not num2=",not num2)
 
 
结果如下:
请输入第一个数:10
请输入第二个数:6
逻辑运算
num1 and num2= 6
num1 or num2= 10
not num2= False

4, relational operators

  • in, not in: determining whether the absence. Returns a Boolean type;
  • It is, is not: to determine whether congruent. Not only values ​​are equal, memory addresses are equal, returns a Boolean type;
print("关系运算")
#in ,not in,is,is not
print("1 in (1,2,3)",1 in (1,2,3))
print("1 not in (1,2,3)",1 not in (1,2,3))
#is
print("a=1,b=1")
a=1
b=1
print("a = b",a==b)
print("a is b",a is b)
# is not
print("a=[1,2],b=[1,2]")
a=[1,2]
b=[1,2]
print("a = b",a==b)
print("a is b",a is b)
 
 
结果如下:
关系运算
1 in (1,2,3) True
1 not in (1,2,3) False
a=1,b=1
a = b True
a is b True
a=[1,2],b=[1,2]
a = b True
a is b False

5, the assignment operator (=, + =, - =, * =, / =,% = = //, ** =):

Note: a + = b -> a = a + b, is similar to the usage of other

num1=input("请输入第一个数:")
num2=input("请输入第二个数:")
num1=int(num1)
num2=int (num2)
print("赋值运算")
#=,+=,-=,*=,/=,%=,//=,**=
num1=num2
print("num1=num2=",num1)
num1+=num2      
print("num1+=num2,num1=",num1)
num1-=num2
print("num1-=num2,num1=",num1)
num1*=num2
print("num1*=num2,num1=",num1)
num1/=num2
print("num1/=num2,num1=",num1)
num1%=num2
print("num1%=num2,num1=",num1)
num1//=num2
print("num1//=num2,num1=",num1)
 
 
结果如下:
请输入第一个数:10
请输入第二个数:6
赋值运算
num1=num2= 6
num1+=num2,num1= 12
num1-=num2,num1= 6
num1*=num2,num1= 36
num1/=num2,num1= 6.0
num1%=num2,num1= 0.0
num1//=num2,num1= 0.0

6, the operator (&, |, ^, ~, >>, <<):

  • &: Binary Bitwise AND
  • |: Binary bit or
  • ^: Binary bit XOR. Participate in computing two objects, if two bits corresponding to "exclusive" (different values), the result is 1 bit, 0 is the same otherwise.
  • -: negated, adding a previous value minus, minus 1 (after you first negated, then in addition to the sign bit, you then negated, the end plus 1)
  • >>: the right, for example: 10 to the right is equal to 10 divided by a 2, i.e., two shift equal to 10 divided by 4;
  • <<: Left, for example: 10 to 10 is equal to a left multiplied by 2, i.e. shifted 3 equal to 10 multiplied by 8;
print("位运算")
#&,|,^,~,>>,<<
num1=int(input("请输入第一个数:"))
num2=int(input("请输入第二个数:"))
 
#二进制按位与
print("num1&num2=",num1&num2)
#二进制按位或
print("num1|num2=",num1|num2)
#二进制按位异或
print("num1^num2=",num1^num2)
#取反,之后除符号位,各位取反,末位加1
#加负号,再减一
print("~num1=",~num1)
#右移,例如 10 向右移1位和10除以2相等,移2位为10除以4
print("num1>>2=",num1>>2)
#左移,例如 10 向左移1>位和10乘2相等,移3位为10乘8
print("num1<<3=",num1<<3)
 
 
结果如下:
位运算
请输入第一个数:10
请输入第二个数:6
num1&num2= 2
num1|num2= 14
num1^num2= 12
~num1= -11
num1>>2= 2
num1<<3= 80

Guess you like

Origin blog.csdn.net/weixin_45116657/article/details/93746444