# Python Operators

> Learn how Python operators work: assignment and arithmetic operators, comparison and boolean operators, bitwise operators, and the is and in keywords.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-12-02 | Updated: 2026-10-28 | Topics: [Python](https://flaviocopes.com/tags/python/) | Canonical: https://flaviocopes.com/python-operators/

[Python](https://flaviocopes.com/python-introduction/) operators are symbols that we use to run operations upon values and variables.

We can divide operators based on the kind of operation they perform:

- assignment operator
- arithmetic operators
- comparison operators
- logical operators
- bitwise operators

plus some interesting ones like `is` and `in`.

### Assignment operator

The assignment operator is used to assign a value to a variable:

```python
age = 8
```

Or to assign a variable value to another variable:

```python
age = 8
anotherVariable = age
```

Since Python 3.8, the `:=` _walrus operator_ assigns a value as part of another operation. You use it when you need the value and also want to test it:

```python
items = ['Roger', 'Syd', 'Luna']
if (n := len(items)) > 2:
    print(n) #3
```

### Arithmetic operators

Python has a number of arithmetic operators: `+`, `-`, `*`, `/` (division), `%` (remainder), `**` (exponentiation) and `//` (floor division). There's also `@` for matrix multiplication, used by NumPy. I cover the number types in [Python numbers](https://flaviocopes.com/python-numbers/).

```python
1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2.0
4 % 3 #1
4 ** 2 #16
4 // 2 #2
```

`/` always returns a float in Python 3. `//` is floor division. With int operands it returns an int.

> Note that you don't need a space between the operands, but it's good for readability.

`-` also works as a unary minus operator:

```python
print(-4) #-4
```

`+` is also used to concatenate String values:

```python
"Roger" + " is a good dog"
#Roger is a good dog
```

We can combine the assignment operator with arithmetic operators:

- `+=`
- `-=`
- `*=`
- `/=`
- `%=`
- ..and so on

Example:

```python
age = 8
age += 1
# age is now 9
```

### Comparison operators

Python defines a few comparison operators:

- `==`
- `!=`
- `>`
- `<`
- `>=`
- `<=`

You can use those operators to get a boolean value (`True` or `False`) depending on the result. See [Python Booleans](https://flaviocopes.com/python-booleans/) for more on that:

```python
a = 1
b = 2

a == b #False
a != b #True
a > b #False
a <= b #True
```

### Boolean operators

Python gives us the following boolean operators:

- `not`
- `and`
- `or`

When working with `True` or `False` attributes, those work like logical AND, OR and NOT, and are often used in the `if` conditional expression evaluation:

```python
condition1 = True
condition2 = False

not condition1 #False
condition1 and condition2 #False
condition1 or condition2 #True
```

Otherwise, pay attention to a possible source of confusion.

`or` used in an expression returns the value of the first operand that is not a falsy value (`False`, `0`, `''`, `[]`..). Otherwise it returns the last operand.

```python
print(0 or 1) ## 1
print(False or 'hey') ## 'hey'
print('hi' or 'hey') ## 'hi'
print([] or False) ## False
print(False or []) ## []
```

The Python docs describe it as `if x is false, then y, else x`.

`and` only evaluates the second argument if the first one is true. So if the first argument is falsy (`False`, `0`, `''`, `[]`..), it returns that argument. Otherwise it evaluates the second argument:

```python
print(0 and 1) ## 0
print(1 and 0) ## 0
print(False and 'hey') ## False
print('hi' and 'hey') ## 'hey'
print([] and False ) ## []
print(False and [] ) ## False
```

The Python docs describe it as `if x is false, then x, else y`.

### Bitwise operators

Some operators are used to work on bits and binary numbers:

- `&` performs binary AND
- `|` performs binary OR
- `^` performs a binary XOR operation
- `~` performs a binary NOT operation
- `<<` shift left operation
- `>>` shift right operation

Bitwise operators are rarely used, only in very specific situations, but they are worth mentioning.

### is and in

`is` is the **identity operator**. It is `True` when both names point at the same object. `==` compares values:

```python
a = [1, 2]
b = [1, 2]
a == b #True
a is b #False
```

`a` and `b` have the same items, but they are two lists. The usual check for `None` is `is`:

```python
name = None
name is None #True
```

`in` is the **membership operator**. It tells you if a value sits in a list, a string, or another sequence:

```python
'dog' in ['cat', 'dog'] #True
'og' in 'dog' #True
```
