English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Python Reference Manual
What are operators in Python?
Operators are special symbols in Python used to perform arithmetic or logical calculations. The values operated on by the operators are called operands.
For example: 2+3 5
>>>+Here,2And3is the operator that performs addition.5is the operand,
Arithmetic operators
Operator | Meaning | Example |
---|---|---|
+ | Arithmetic operators are used to perform mathematical operations, such as addition, subtraction, multiplication, etc. - Addition | x + y + 2 |
- | Adds two operands or unary plus - Subtraction | x-y- 2 |
* | Subtracts the right operand from the left operand (unary minus) -Multiplication | x * y |
/ | Multiplies two operands - Division | x / y |
Divides the left operand by the right operand (the result is always a float) | % -Modulus | x % y (the remainder when the left operand is divided by the right operand) / the remainder of y) |
// | Integer division - Returns the integer part of the quotient (rounded down) | x // y |
** | Power - Returns the y-th power of x | x ** y (x to the power of y) |
x = 15 y = 4 # Output: x + y = 19 print('x + y =, x+y) # Output: x - y = 11 print('x - y =, x-y) # Output: x * y = 60 print('x * y =, x*y) # Output: x / y = 3.75 print('x / y =, x/y) # Output: x // y = 3 print('x // y =, x//y) # Output: x ** y = 50625 print('x ** y =, x**y)
When the program is run, the output is:
x + y = 19 x - y = 11 x * y = 60 x / y = 3.75 x // y = 3 x ** y = 50625
Comparison operators are used to compare values. They return True or False based on the condition.
Operator | Meaning | Example |
---|---|---|
> | Greater-Returns True if the left operand is greater than the right operand | x > y |
< | Less than-Returns True if the left operand is less than the right operand | x < y |
== | Equal-Returns True if both operands are equal | x == y |
!= | Not equal-Returns True if the operands are not equal | x != y |
>= | Greater than or equal to-Returns True if the left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to-Returns True if the left operand is less than or equal to the right | x <= y |
x = 10 y = 12 # Output: x is greater than y: False print('x is greater than y: ', x > y) # Output: x is less than y: True print('x is less than y: ', x < y) # Output: x is equal to y: False print('x is equal to y: ', x == y) # Output: x is not equal to y: True print('x is not equal to y: ', x != y) # Output: x >= y is False print('x >= y is ', x>=y) # Output: x <= y is True print('x <= y is ', x<=y)
Output result
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
Logical operators are and, or, not operators.
Operator | Meaning | Example |
---|---|---|
and | If both operands are true, it is true | x and y |
or | If any operand is true, it is true | x or y |
not | If the operand is false, it is True (complement the operand) | not x |
x = True y = False print('x and y is ', x and y) print('x or y is ', x or y) print('not x is ', not x)
Output result
x and y is False x or y is True not x is False
This is the truth table for these operators:truth table.
Bitwise operators act on operands as if they are strings of binary digits. They run one by one, hence the name.
For example,2is10binary,7is111.
be listed in the table below:Letx= 10(0000 1010binary) andy= 4(0000 0100binary)
Operator | Meaning | Example |
---|---|---|
& | bitwise AND | x&y = 0(0000 0000) |
| | bitwise OR | x | y = 14(0000 1110) |
~ | bitwise NOT | ~x = -11(1111 0101) |
^ | bitwise XOR | x ^ y = 14(0000 1110) |
>> | bitwise right shift | x >> 2 = 2(0000 0010) |
<< | bitwise left shift | x << 2 = 40(0010 10(00) |
In Python, the assignment operator is used to assign a value to a variable.
a = 5is a simple assignment operator, which assigns the value on the right5assigned to the left variablea.
There are many similar compound operators in Python, a += 5They are added to the variable and then assigned to them. Equivalent to a = a + 5.
Operator | Example | equivalent |
---|---|---|
= | x = 5 | x = 5 |
+= | x + = 5 | x = x + 5 |
-= | x-= 5 | x = x-5 |
*= | x * = 5 | x = x * 5 |
/= | x / = 5 | x = x / 5 |
%= | x%= 5 | x = x%5 |
//= | x // = 5 | x = x // 5 |
**= | x ** = 5 | x = x ** 5 |
&= | x&= 5 | x = x&5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
Python provides some special types of operators, such as identity operators or membership operators. Below, they are described through examples.
is and is not are identity operators in Python. They are used to check whether two values (or variables) are located in the same part of memory. Two equal variables do not necessarily mean they are the same.
Operator | Meaning | Example |
---|---|---|
is | If the operands are the same, then true (referring to the same object) | x is true |
is not | If the operands are not the same, then true (not referring to the same object) | x is not true |
x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [1,2,3] y3 = [1,2,3] # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3)
Output result
False True False
Here, we seex1Andy1They are integers with the same value, so they are both equal and the same.x2Andy2(string)is the same.
Butx3Andy3They are equal but not the same. This is because although they are equal, the interpreter still locates them separately in memory.
in and not in are membership operators in Python. They are used to test whether a value (or variable) exists in a sequence (String,List,Tuple,SetAndDictionary)to find the value or variable.
In the dictionary, we can only test the existence of the key, not the value.
Operator | Meaning | Example |
---|---|---|
in | If the value is found in the sequence/Variable, then true | 5 in x |
not in | If the value is not found in the sequence/Variable, then true | 5 not in x |
x = 'Hello world' y = {1: 'a',2: 'b'} # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y)
Output result
True True True False
Here, 'H' is in x, but 'hello' does not exist in x (remember that Python is case-sensitive). Similarly,1Is the key, and a is the value of the dictionary y, so y returns False for a.