The operator
- The assignment operator
x = 1 Assign 1 to the x variable
y = 2 Assign 2 to the y variable
Copy the code
- Arithmetic operator
x = 1
y = 2
z = x+y
print("The sum of 1+2 is %d."%z)
z = x-y
print("The difference between 1 and 2 is %d"%z)
z = x*y
print("1 times 2 is %d."%z)
z = x/y
print("The quotient of 1/2 is %f."%z)
z = x%y
print("The remainder of 1%%2 is %d"%z)
z = x**y
print("1**2 to the power %d"%z)
Copy the code
Output from the above code:
- The compound assignment operator
x = 1
y = 2
z = 0
z+=x# is the same thing as z is equal to z plus x
print("Z is %d"%z)Output 1 #
z-=x# is the same thing as z equals z minus x
print("Z is %d"%z)# 0
z*=x# is the same thing as z is equal to z times x
print("Z is %d"%z)Print 0(0 times anything is 0)
z/=x# is the same thing as z = z/x
print("Z is %d"%z)Print 0(0 divided by anything is 0)
z%=x# is the same thing as z = z % x
print("Z is %d"%z)The remainder of 0 divided by any number is 0.
z**=x# is the same thing as z = z ** x
print("Z is %d"%z)# 0
Copy the code
Output from the above code:
- Comparison operator
x = 1
y = 2
print(x > y)# print False which is False
print(x < y)# print True which means True
print(x >= y)
print(x <= y )
print(x == y)# Note that one = is the assignment operator, and two == are used to determine whether two values are equal. Output is Falseprint(x ! = y)# Judgement does not equal. It's obvious that x is not equal to y. The output of True
Copy the code
Output from the above code:
- Logical operator
x = True# bool type
y = False
print(x and y)If x is False, x and y return False, otherwise it returns the computed value of y. And means' and '.
print(x or y)# It returns True if x is True, otherwise it returns the computed value of y.
print(not x)# If x is True, return False. If x is False, it returns True.
Copy the code
Output from the above code: