Python Operators
Python Operators
Python Operators
❮ Previous Next ❯
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
print(10 + 5)
Run example »
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
https://www.w3schools.com/python/python_operators.asp 1/10
4/9/24, 2:26 AM Python Operators
Python
Menu Arithmetic
Operators
Spaces Get Certified Sign Up Log in
** Exponentiation x ** y Try it »
+= x += 3 x=x+3 Try it »
-= x -= 3 x=x-3 Try it »
*= x *= 3 x=x*3 Try it »
/= x /= 3 x=x/3 Try it »
%= x %= 3 x=x%3 Try it »
https://www.w3schools.com/python/python_operators.asp 2/10
4/9/24, 2:26 AM Python Operators
**= Menu
x **= 3
Spaces
x = x ** 3
Get Certified Sign Up
Try it »
Log in
|= x |= 3 x=x|3 Try it »
^= x ^= 3 x=x^3 Try it »
== Equal x == y Try it »
not Reverse the result, returns not(x < 5 and x < 10) Try it »
False if the result is true
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
https://www.w3schools.com/python/python_operators.asp 4/10
4/9/24, 2:26 AM Python Operators
Python
Menu BitwiseOperators
Spaces Get Certified Sign Up Log in
HTML
Bitwise CSS JAVASCRIPT
operators SQL
are used to comparePYTHON JAVA
(binary) numbers: PHP HOW TO W3.CSS C
Operator Precedence
Operator precedence describes the order in which operations are performed.
Example
Parentheses has the highest precedence, meaning that expressions inside parentheses
must be evaluated first:
print((6 + 3) - (6 + 3))
https://www.w3schools.com/python/python_operators.asp 5/10
4/9/24, 2:26 AM Python Operators
Run example
Menu
»
Spaces Get Certified Sign Up Log in
Example
Multiplication * has higher precedence than addition + , and therefor multiplications are
evaluated before additions:
print(100 + 5 * 3)
Run example »
The precedence order is described in the table below, starting with the highest
precedence at the top:
() Parentheses Try it »
** Exponentiation Try it »
| Bitwise OR Try it »
https://www.w3schools.com/python/python_operators.asp 6/10
4/9/24, 2:26 AM Python Operators
or OR Try it »
If two operators have the same precedence, the expression is evaluated from left to
right.
Example
Addition + and subtraction - has the same precedence, and therefor we evaluate the
expression from left to right:
print(5 + 4 - 7 + 3)
Run example »
Exercise:
Multiply 10 with 5 , and print the result.
print(10 5)
Submit Answer »
https://www.w3schools.com/python/python_operators.asp 7/10
4/9/24, 2:26 AM Python Operators
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free!
Sign Up Log in
COLOR PICKER
https://www.w3schools.com/python/python_operators.asp 8/10
4/9/24, 2:26 AM Python Operators
Menu Spaces
Get Certified Sign Up Log in
https://www.w3schools.com/python/python_operators.asp 9/10
4/9/24, 2:26 AM Python Operators
FORUM ABOUT
Menu Spaces Get Certified Sign Up
W3Schools is optimized for learning and training. Examples might be simplified to
Log in
https://www.w3schools.com/python/python_operators.asp 10/10