Python Operators Explained
Here’s an easy way to explain Python operators with examples. I'll use information inspired by
W3Schools, structured for clarity. Python operators are categorized into groups based on their
functionality:
1. Arithmetic Operators
Used to perform mathematical operations.
Operator Description Example Code Output
`+` Addition `x = 5; y = 3; print(x + y)` 8
`-` Subtraction `x = 5; y = 3; print(x - y)` 2
`*` Multiplication `x = 5; y = 3; print(x * y)` 15
`/` Division `x = 5; y = 3; print(x / y)` 1.6667
`%` Modulus (Remainder) `x = 5; y = 3; print(x % y)` 2
`**` Exponentiation `x = 2; y = 3; print(x ** y)` 8
`//` Floor division `x = 5; y = 3; print(x // y)` 1
2. Assignment Operators
Used to assign values to variables.
Operator Example Equivalent To Example Code Output
`=` `x = 5` Assign value `x = 5; print(x)` 5
`+=` `x += 3` `x = x + 3` `x = 5; x += 3; print(x)` 8
`-=` `x -= 3` `x = x - 3` `x = 5; x -= 3; print(x)` 2
`*=` `x *= 3` `x = x * 3` `x = 5; x *= 3; print(x)` 15
`/=` `x /= 3` `x = x / 3` `x = 5; x /= 3; print(x)` 1.6667
`%=` `x %= 3` `x = x % 3` `x = 5; x %= 3; print(x)` 2
`//=` `x //= 3` `x = x // 3` `x = 5; x //= 3; print(x)` 1
`**=` `x **= 3` `x = x ** 3` `x = 2; x **= 3; print(x)` 8
3. Comparison Operators
Used to compare two values.
Operator Description Example Code Output
`==` Equal to `x = 5; y = 5; print(x == y)` True
`!=` Not equal to `x = 5; y = 3; print(x != y)` True
`>` Greater than `x = 5; y = 3; print(x > y)` True
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/3
Operator Description Example Code Output
`<` Less than `x = 5; y = 3; print(x < y)` False
`>=` Greater than or equal to `x = 5; y = 5; print(x >= y)` True
`<=` Less than or equal to `x = 5; y = 8; print(x <= y)` True
4. Logical Operators
Used to combine conditional statements.
Operator Description Example Code Output
Returns `True` if both statements are
`and` `x = 5; print(x > 3 and x < 10)` True
true
Returns `True` if one of the statements is
`or` `x = 5; print(x > 3 or x < 4)` True
true
`x = 5; print(not(x > 3 and x <
`not` Reverse the result False
10))`
5. Identity Operators
Used to compare objects’ memory locations.
Operator Description Example Code Output
Returns `True` if both variables point to `x = [1, 2]; y = x; print(x is
`is` True
the same object y)`
`is Returns `True` if they point to different `x = [1, 2]; y = [1, 2]; print(x
True
not` objects is not y)`
6. Membership Operators
Used to test if a sequence is present in an object.
Operator Description Example Code Output
Returns `True` if value is present in
`in` `x = [1, 2, 3]; print(2 in x)` True
sequence
`not `x = [1, 2, 3]; print(4 not in
Returns `True` if value is not present True
in` x)`
7. Bitwise Operators
Operate on binary numbers.
Operator Description Example Code Output
`&` AND `x = 5; y = 3; print(x & y)` 1
` ` OR `x = 5; y = 3; print(x
`^` XOR `x = 5; y = 3; print(x ^ y)` 6
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/3
Operator Description Example Code Output
`~` NOT (negation) `x = 5; print(~x)` -6
`<<` Left shift `x = 5; print(x << 1)` 10
`>>` Right shift `x = 5; print(x >> 1)` 2
These simple examples demonstrate how Python operators work. For more, visit W3Schools - Python
Operators.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/3