In this article, we’ve gone in-depth to cover all of the python operators you need to know in 2023, including code examples to show you how to use them.

As one of the top 3 programming languages, Python is often the go-to for beginners and experienced pros in data science, web development, and more. And when it comes to Python programming, knowing how to use python operators is an essential skill.

Whether you’re new to learning Python or an experienced pro looking to boost your skills with python operators, we’ve covered everything from basic arithmetic and comparison operators to complex bitwise operators and the walrus operator!

Side note, if you’re new to Python, we’ve just released our own Python course to help you truly learn the fundamentals of Python and to think like a pro.

So if you’re ready, let’s dive in and explore these python operators!

Download Hackr.io's Python Operators In-Depth Guide PDF

**What Are Python Operators?**

Python operators are special symbols and keywords that perform specific operations on one or more operands, making them essential tools in programming for manipulating data and making decisions in our code.

Whether you’re just starting out with Python or an experienced dev that’s in the middle of building Python projects, operators will show up in almost every Python program you create.

These operations can range from basic arithmetic to complex logic and data manipulation. Operators are fundamental to almost every programming language, not just Python, and serve as the building blocks for all software.

Operators in Python also play a role in optimizing and refining code. Bitwise operators, for instance, offer efficient data manipulation at the binary level, which is essential when performance is critical. Think graphics rendering or real-time data processing. These are the types of detail you need to know if you want to pursue a Python certification.

In essence, operators are the syntax that bridges human logic with machine processing. They're like the grammar of programming, which, when combined, allow us to express intricate operations and logic within our Python code. Whichever real-world Python application applies to your use case, python operators will show up everywhere.

Let’s now dive into the various python operators, ranging from common arithmetic and comparison operators to specialized operators like bitwise operators and the hilariously-named walrus operator!

By the time you’ve finished, you’ll be ready to level up your personal Python cheat sheet.

**Arithmetic Operators**

Python arithmetic operators are fundamental tools to perform mathematical operations, including symbols for addition, subtraction, multiplication, and others.

The next time your fire up your Python IDE and write a program that needs to use math, you’ll find arithmetic operators are indispensable.

**Addition (+) **

The addition operator is used to sum two values. When used with numbers, it performs arithmetic addition. When used with strings, it concatenates them.

In fact, the addition operator is probably the first one that you will learn to use when taking a Python course, as it’s super easy to understand for all skill levels and it’s easy to use.

```
'''
Python Addition Operator Example
'''
x = 5
y = 3
print(x + y) # Outputs: 8
# For strings
str1 = "Hello"
str2 = "World"
print(str1 + str2) # Outputs: HelloWorld
```

**Subtraction(-)**

This Python operator deducts the value on its right from the value on its left. It's primarily used for arithmetic operations involving numbers.

```
'''
Python Subtraction Operator Example
'''
x = 8
y = 3
print(x - y) # Outputs: 5
```

**Multiplication (*)**

This Python operator multiplies the two operands. With numbers, it gives the product, but when used with a string and a number, it repeats the string that number of times.

```
'''
Python Multiplication Operator Example
'''
x = 7
y = 4
print(x * y) # Outputs: 28
# For strings
str1 = "Hello"
print(str1 * 3) #: HelloHelloHello
```

**Division (/)**

This Python operator divides the left operand by the right operand. The result provides a floating-point division.

```
'''
Python Division Operator Example
'''
x = 20
y = 4
print(x / y) # Outputs: 5.0
```

**Modulus (%)**

As one of the more unusual arithmetics operators, you’ve likely asked what is % in Python if you’re new to programming.

Well, % in Python is a way to return the remainder of a division operation. This is particularly useful when we want to find out the remainder, such as determining if a number is even or odd.

```
'''
Python Modulus Operator Example
'''
x = 29
y = 4
print(x % y) # Outputs: 1
```

**Exponentiation (**)**

This Python exponent operator raises the left operand to the power of the right operand.

```
'''
Python Exponentiation Operator Example
'''
x = 3
y = 4
print(x ** y) # Outputs: 81
```

**Floor Division (//)**

This is another of the more unusual arithmetic operators for newcomers to programming, but what is // in Python? And how is this different from normal division?

Well, // in Python is a division method that rounds down the result to the nearest whole number, disregarding the decimal remainder. For this reason, it’s sometimes called integer division.

```
'''
Python Floor Division Operator Example
'''
x = 17
y = 4
print(x // y) # Outputs: 4
```

**Comparison Operators (Relational Operators)**

Python comparison operators are often referred to as relational operators, and they are used to compare two values, returning a boolean value of True or False.

These Python comparison operators are fundamental when the time comes to implement decision-making processes via Python’s conditional statements, which in turn allows our code to branch or loop based on specific conditions.

**Equal To (==)**

The most common of the Python equality operators, this checks if the value of two operands is the same. If they're equal, it returns True, otherwise, it returns False.

Important note: If you’re new to comparisons, it’s important to understand the difference between the Python equality and is* *operators.

TL-DR: When you need to check for equality of values, the equality operator is ideal, whereas the *is* operator is for checking if the objects themselves are equal.

```
'''
Python "Equal to" Operator Example
'''
x = 5
y = 5
z = 6
print(x == y) # Outputs: True
print(x == z) # Outputs: False
```

**Not Equal To (!=)**

This Python comparison operator checks for inequality, meaning that if the operand values are different, it returns True. Much like its opposite, the equality operator, this is ideal for constructing flow control structures like the Python while loop, along with conditional statements.

```
'''
Python "Not equal to" Operator Example
'''
x = 5
y = 7
print(x != y) # Outputs: True
```

**Less Than (<)**

This Python operator determines if the value on the left is smaller than the one on the right, and it returns True if this is the case.

```
'''
Python "Less than" Operator Example
'''
x = 3
y = 5
print(x < y) # Outputs: True
```

**Greater Than (>)**

This Python operator checks if the left value is larger than the one on the right, and it returns True if this is the case.

```
'''
Python "Greater than" Operator Example
'''
x = 8
y = 5
print(x > y) # Outputs: True
```

**Less Than Or Equal To (<=)**

This python operator checks for two conditions, namely whether the left operand value is less than *or* equal to the right operand value, and it returns true if either of these conditions is True.

```
'''
Python "Less than or equal to" Operator Example
'''
x = 7
y = 7
z = 8
print(x <= y) # Outputs: True
print(x <= z) # Outputs: True
```

**Greater Than Or Equal To (>=)**

This Python operator checks for two conditions, namely whether the left operand value is greater than *or* equal to the right operand, and it returns true if either of these conditions is True.

```
'''
Python "Greater than or equal to" Operator Example
'''
x = 9
y = 6
z = 9
print(x >= y) # Outputs: True
print(x >= z) # Outputs: True
```

**Logical Operators (Boolean Operators)**

Sometimes known as boolean operators, Python logical operators evaluate the *truthiness* or logical state of one or more conditions and return a boolean value.

This makes logical operators essential for constructing compound conditions from multiple operators and refining decision-making logic in code.

**Logical AND (and)**

The Python AND operator returns True if both the conditions on its sides are true, and it's often used in conditional statements to check multiple conditions.

```
'''
Python Logical AND Operator Example
'''
x = True
y = False
print(x and y) # Output: False
```

**Logical OR (or)**

The Python OR operator returns True if at least one of its surrounding conditions is true.

```
'''
Python Logical OR Operator Example
'''
x = True
y = False
print(x or y) # Output: True
```

**Logical NOT (not)**

This Python operator inverts the truth value of the condition it precedes. For example, if a condition is True, not will make it False. Of all the Python logical operators, this is perhaps the most confusing for beginners to boolean logic.

```
'''
Python Logical NOT Operator Example
'''
x = True
print(not x) # Output: False
```

**Bitwise Operators**

Python bitwise operators deal directly with the individual bits of a number, and they are used to perform operations like AND, OR, and XOR at the binary level.

While they might seem slightly esoteric, Python bitwise operators are incredibly powerful and frequently used in fields like cryptography, graphics processing, and high-performance applications where bit-level manipulation of data is required.

If these ideas are interesting to you, you should definitely consider picking up a Python book to level up your skills with Python bitwise operators.

**Bitwise AND (&)**

This Python logical AND operator works at the bit level to perform a bitwise operation on the binary representations of integers, and it returns the result.

```
'''
Python Bitwise AND Operator Example
'''
x = 12 # 1100 in binary
y = 10 # 1010 in binary
print(x & y) # Output: 8 (which is 1000 in binary)
```

**Bitwise OR (|)**

This Python logical OR operator performs a bitwise operation on binary digits of an integer, and it returns the result.

```
'''
Python Bitwise OR Operator Example
'''
x = 12 # 1100 in binary
y = 10 # 1010 in binary
print(x | y) # Output: 14 (which is 1110 in binary)
```

**Bitwise XOR (^)**

The Python ^ operator returns a number that is composed of 1s where the two binary numbers differ and 0s where they're the same.

```
'''
Python Bitwise XOR Operator Example
'''
x = 12 # 1100 in binary
y = 10 # 1010 in binary
print(x ^ y) # Output: 6 (which is 0110 in binary)
```

**Bitwise NOT (~)**

This Python operator flips all of the bits of a number. If you recall that a *NOT* operation inverts the truthiness of a value, this is much the same, but it focuses on the binary bits. Which, in simple terms, means that 1s become 0s and vice versa.

```
'''
Python Bitwise NOT Operator Example
'''
x = 12 # 1100 in binary
print(~x) # Output: -13
# Note: This is a two's complement representation.
# The binary result is 11110011, which translates to -13 in integer
```

**Left Shift (<<)**

This Python operator moves the bits of a number to the left by a specified amount, filling the new bits with zeros.

```
'''
Python Left Shift Operator Example
'''
x = 5 # 0101 in binary
print(x << 1) # Output: 10 (1010 binary, shifting bits one position to left)
```

**Right Shift (>>)**

This Python operator shifts the bits of a number to the right, resulting in the removal of some of the rightmost bits.

```
'''
Python Right Shift Operator Example
'''
x = 5 # 0101 in binary
print(x >> 1) # Output: 2 (0010 binary, shifting bits one position to right)
```

**Assignment Operators**

Python assignment operators are some of the most common, and they are fundamental to storing, updating, and managing data throughout the life cycle of a program.

**Assign (=)**

This Python assignment operator sets the variable on the left to the value on the right. It's fundamental in programming to store and update variable values.

```
'''
Python Assign Operator Example
'''
x = 5
print(x) # Output: 5
```

**Add And Assign (+=)**

This Python assignment operator is a shorthand way to add the right operand's value to the left operand and then assign the result to the left operand.

```
'''
Python Add and Assign Operator Example
'''
x = 5
x += 3
print(x) # Output: 8
```

**Subtract And Assign (-=)**

This Python assignment operator is a shorthand way to subtract the right operand's value from the left operand and then assign the result to the left operand.

```
'''
Python Subtract and Assign Operator Example
'''
x = 5
x -= 3
print(x) # Output: 2
```

**Multiply And Assign (*=)**

This Python assignment operator is a shorthand for multiplying the left operand's value with the right operand and then assigning the result to the left operand.

```
'''
Python Multiply and Assign Operator Example
'''
x = 5
x *= 3
print(x) # Output: 15
```

**Divide And Assign (/=)**

This Python operator is a shorthand for dividing the left operand's value with the right operand and then assigning the result to the left operand.

```
'''
Python Divide and Assign Operator Example
'''
x = 5
x /= 2
print(x) # Output: 2.5
```

**Modulus And Assign (%=)**

This Python operator is a shorthand for dividing the left operand's value with the right operand, then assigning the remainder result to the left operand.

```
'''
Python Modulus and Assign Operator Example
'''
x = 5
x %= 3
print(x) # Output: 2
```

**Floor Divide And Assign (//=)**

This Python operator is a shorthand for using the Python // operator to floor divide the left operand's value with the right operand, then assigning the result to the left operand.

```
'''
Python Floor Divide and Assign Operator Example
'''
x = 5
x //= 2
print(x) # Output: 2
```

**Exponentiate And Assign (**=)**

This Python exponent operator is a shorthand for raising the left operand's value to the power of the right operand, then assigning the result to the left operand.

```
'''
Python Exponentiate and Assign Operator Example
'''
x = 5
x **= 2
print(x) # Output: 25
```

**Bitwise Assignment Operators (&=, |=, ^=, <<=, >>=)**

These Python operators perform the associated bitwise operation and then update the variable with the result in the same way as the other assignment operators shown above.

```
'''
Python Bitwise Assignment Operators Example
'''
x = 5 # 0101 in binary
x &= 3 # 0011 in binary
print(x) # Output: 1 (0001 in binary)
y = 5 # 0101 in binary
y |= 3 # 0011 in binary
print(y) # Output: 7 (0111 in binary)
z = 5 # 0101 in binary
z ^= 3 # 0011 in binary
print(z) # Output: 6 (0110 in binary)
a = 5 # 0101 in binary
a <<= 1
print(a) # Output: 10 (1010 in binary)
b = 5 # 0101 in binary
b >>= 1
print(b) # Output: 2 (0010 in binary)
```

**Membership Operators**

Python membership operators can be used to determine whether a value is a member of a Python data structure, like a list, tuple, or string, and in general, these Python operators make it simpler for us to perform tasks like searching and filtering within a data collection.

**Contains (in)**

The Python IN operator is used to check if a certain item exists within data structures like a Python list, and it returns True or False.

Python membership operators like IN are also helpful when interacting with other data containers like tuples and strings.

```
'''
Python "in" Membership Operator Example
'''
lst1 = [1, 2, 3, 4, 5]
print(3 in lst1) # Output: True
print(6 in lst1) # Output: False
```

**Does Not Contain (not in)**

This Python operator is used to check if a certain item does not exist within a container, like a list, tuple, or string, and it returns True or False. In essence, the two membership operators are inverses of each other.

```
'''
Python "not in" Membership Operator Example
'''
lst1 = [1, 2, 3, 4, 5]
print(3 not in lst1) # Output: False
print(6 not in lst1) # Output: True
```

We should also point out that both membership operators are also helpful when learning to use the Python deque and other more exotic data collections.

**Identity Operators**

Python identity operators are a little bit nuanced, as they check if two variables refer to the exact same object in memory, not just if their contents are the same.

This distinction is really important when working with mutable data structures in Python, and it’s essential that you have a solid understanding of data referencing and memory management in applications when using identity operators.

**Identity (is)**

As one of the two Python identity operators, this is used to check if two variables refer to the same object in memory and that they’re not just equal in value. Remember, we use identity operators for checking object equality, not just value equality.

```
'''
Python "is" Identity Operator Example
'''
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) # Output: True ( z is same object as x)
print(x is y) # Output: False (same content but different objects in memory)
```

**Not Identity (is not)**

This Python operator is used to check if two variables refer to different objects in memory.

```
'''
Python "is not" Identity Operator Example
'''
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z) # Output: False
print(x is not y) # Output: True
```

**Other Useful Python Operators**

Now we’ve covered the most common Python operators, let’s look at some specialized operators that demonstrate the depth and versatility of the Python language.

Some of these might be familiar to you, while one may be new to you depending on the version of Python you have been using.

**Walrus Operator (:=)**

Introduced in Python 3.8, and officially called the assignment expression operator, this python operator is used to assign values to variables as a part of an expression.

The classic use case is within list comprehensions or while loops, where it can assign a value to a variable and check its validity in a single line. This is great for reducing redundancy and making your code cleaner.

```
'''
Python Walrus Operator Example
'''
input_text = "Hello"
if (n := len(input_text)) > 4:
print(f"Input has {n} characters.") # Output: Input has 5 characters.
```

**Subscription ([])**

This Python operator is primarily used to access elements from various data structures like lists, tuples, strings, and dictionaries. Think of it as a tool to subscribe to a particular data point within a structured collection, hence the name.

And whether you’re working on an algorithm to find the length of a Python list or you want to access a dictionary item, this is the operator to use.

```
'''
Python Subscription Operator Example
'''
lst2 = [1, 2, 3, 4, 5]
print(lst2[2]) # Output: 3
dict1 = {"a": 1, "b": 2}
print(dict1["a"]) # Output: 1
```

**Slicing ([:])**

This Python operator offers a mechanism to extract a range or segment from sequences like lists, strings, and tuples.

It is a lot like the subscription operator but supercharged for broader access. This means that when using slicing, you can obtain a consecutive series of elements from a sequence, which is handy for data processing.

```
'''
Python Slicing Operator Example
'''
text = "Python"
print(text[1:4]) # Output: yth
```

**Python Operator Precedence**

In Python, just like in mathematics, the order in which operations are performed can greatly influence the outcome. That’s why it’s essential to understand the order of operations and operator precedence if you want to write correct and efficient code.

In fact, if you’ve got your eyes set on landing a job with your Python skills, you’d better be ready for Python interview questions that test your understanding of something seemingly basic yet vitally important, like this.

**Order Of Operations Explained**

Let’s look at the general order of operations in Python, starting from the highest precedence to the lowest:

**1. Parentheses:** Anything inside parentheses () is evaluated first, making this a way to override the default precedence or changing the result when you have operations with the same precedence or if you want something to have higher precedence.

```
'''
Python Operator Precedence: Parentheses
'''
result = (2 + 3) * 4 # Here, addition happens first, making result 20.
```

**2. Exponentiation:**

```
'''
Python Operator Precedence: Exponentiation
'''
square = 4 ** 2 # square is 16
```

**3. Unary minus:** For negation.

```
'''
Python Operator Precedence: Unary minus
'''
negative_value = -5
```

**4. Multiplication, Division, Floor Division, and Modulus:** When chained together, these are evaluated from left to right.

```
'''
Python Operator Precedence: Multiplication, Division, Floor Division, and Modulus
'''
result = 5 * 4 / 2 # First multiplication, then division. result is 10.0.
```

**5. Addition and Subtraction: **These are evaluated from left to right when they appear together.

```
'''
Python Operator Precedence: Addition and Subtraction
'''
result = 3 + 4 - 2 # First addition, then subtraction. result is 5.
```

**6. Comparison Operators:** Multiple comparisons can be chained, but it's good practice to use parentheses for clarity.

```
'''
Python Operator Precedence: Comparison Operators
'''
is_true = 3 < 4 == 4 # is_true is True
```

**7. Boolean not:**

```
'''
Python Operator Precedence: Boolean 'not'
'''
is_false = not True # is_false is False
```

**8. Boolean and:**

```
'''
Python Operator Precedence: Boolean 'and'
'''
result = True and False # result is False
```

**9. Boolean or:**

```
'''
Python Operator Precedence: Boolean 'or'
'''
result = True or False # result is True
```

**10. Assignment and other operators:** Assignment operators like =, +=, -=, and others have the lowest precedence.

```
'''
Python Operator Precedence: Assignment and other operators
'''
value = 10 # Assignment example
```

**Important note: **While understanding operator precedence can help you to write concise code, clarity should always be the priority. If you think using parentheses might make your code more readable, go ahead and use them!

**Wrapping Up**

So there you have it, you now know how to use python operators for nearly every scenario! We’ve even included source code examples to show you how to use each of the python operators, which, as you can see, is a lot!

Whether you’re just starting out on your Python journey or an experienced dev that wants to expand your skills, we’ve included python operators for all skill levels.

We hope you’ve enjoyed learning about the different python operators, and if you have any interesting python operators that you’d like us to include, let us know in the comments!

**Enjoyed learning about Python operators and ready to dive deeper into Python? Check out:**

Our Python Masterclass - Python with Dr. Johns

**Frequently Asked Questions**

**1. What Are The 7 Operators In Python?**

The 7 types of Python operators are arithmetic, comparison, relational, logical, bitwise, assignment, membership, and identity operators. Look at the rest of our article for more in-depth information on individual Python operators in each category.

**2. What Are Operators In Python?**

Python operators are symbols and keywords that perform specific *operations* on one or more operands, whether that’s mathematical, logical, relational, and more. This makes them essential tools in Python programming for manipulating data and making decisions in code.

**3. What Does '%' Operator Do In Python?**

The % operator is one of the more unusual mathematical operators if you’re new to programming, but it’s used to return the remainder of an integer division operation. For example, if you carried out the operator 5 % 2, the result would be 1 because the integer division of 5 // 2 produces 2, with a remainder of 1.

**People are also reading:**