# Operators

In Python, **operators** are special symbols used to perform operations on variables and values. They are essential for writing any logic or calculations in your programs.

Let's look at types of operators:

## Arithmetic Operators

Arithmetic operators are used for basic mathematical operations.&#x20;

| Operator | Description              | Example  | Result |
| -------- | ------------------------ | -------- | ------ |
| `+`      | Addition                 | `5 + 2`  | `7`    |
| `-`      | Subtraction              | `5 - 2`  | `3`    |
| `*`      | Multiplication           | `5 * 2`  | `10`   |
| `/`      | Division (float result)  | `5 / 2`  | `2.5`  |
| `//`     | Floor division (integer) | `5 // 2` | `2`    |
| `%`      | Modulus (remainder)      | `5 % 2`  | `1`    |
| `**`     | Exponentiation (power)   | `2 ** 3` | `8`    |

Example:

```python
print(5 + 2)    # 7
print(5 - 2)    # 3
print(5 * 2)    # 10
print(5 / 2)    # 2.5
print(5 // 2)   # 2
print(5 % 2)    # 1
print(2 ** 3)   # 8
```

## Comparison Operators

Comparison operators are used to compare two values. They return either `True` or `False`.

| Operator | Description              | Example  | Result  |
| -------- | ------------------------ | -------- | ------- |
| `==`     | Equal to                 | `5 == 5` | `True`  |
| `!=`     | Not equal to             | `5 != 3` | `True`  |
| `>`      | Greater than             | `5 > 3`  | `True`  |
| `<`      | Less than                | `5 < 3`  | `False` |
| `>=`     | Greater than or equal to | `5 >= 5` | `True`  |
| `<=`     | Less than or equal to    | `5 <= 4` | `False` |

Example:

```python
print(5 == 5)     # True
print(5 != 3)     # True
print(5 > 3)      # True
print(5 < 3)      # False
print(5 >= 5)     # True
print(5 <= 4)     # False
```

## Assignment Operators

Assignment operators are used to assign values to variables. Some of them also perform operations.

| Operator | Description             | Example   | Equivalent to |
| -------- | ----------------------- | --------- | ------------- |
| `=`      | Assign                  | `x = 5`   | `x = 5`       |
| `+=`     | Add and assign          | `x += 2`  | `x = x + 2`   |
| `-=`     | Subtract and assign     | `x -= 1`  | `x = x - 1`   |
| `*=`     | Multiply and assign     | `x *= 3`  | `x = x * 3`   |
| `/=`     | Divide and assign       | `x /= 2`  | `x = x / 2`   |
| `//=`    | Floor divide and assign | `x //= 2` | `x = x // 2`  |
| `%=`     | Modulus and assign      | `x %= 2`  | `x = x % 2`   |
| `**=`    | Power and assign        | `x **= 2` | `x = x ** 2`  |

Example:

```python
x = 5
x += 2
print(x)    # 7

x *= 3
print(x)    # 21

x //= 2
print(x)    # 10
```

## Logical Operators

Logical operators are used to combine conditional statements.

| Operator | Description                     | Example          | Result  |
| -------- | ------------------------------- | ---------------- | ------- |
| `and`    | Returns `True` if both are true | `True and False` | `False` |
| `or`     | Returns `True` if one is true   | `True or False`  | `True`  |
| `not`    | Reverses the result             | `not True`       | `False` |

```python
a = True
b = False

print(a and b)    # False
print(a or b)     # True
print(not a)      # False
```

## Identity Operators

Identity operators are used to compare the memory location of two objects.

| Operator | Description                              | Example      | Result       |
| -------- | ---------------------------------------- | ------------ | ------------ |
| `is`     | True if both refer to same object        | `a is b`     | `True/False` |
| `is not` | True if they do not refer to same object | `a is not b` | `True/False` |

Example:

```python
x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)     # True (same object)
print(x is z)     # False (same content, different object)
print(x is not z) # True
```

## Membership Operators

Used to test if a value exists in a sequence like a list, tuple, or string.

| Operator | Description                  | Example              | Result |
| -------- | ---------------------------- | -------------------- | ------ |
| `in`     | True if value is present     | `3 in [1, 2, 3]`     | `True` |
| `not in` | True if value is not present | `4 not in [1, 2, 3]` | `True` |

Example:

```python
nums = [1, 2, 3]

print(3 in nums)      # True
print(4 not in nums)  # True
```

## Bitwise Operators

Bitwise operators operate on the **binary representation** of integers. Each number is stored as bits (0s and 1s), and these operators allow you to manipulate the bits directly.

| Operator | Description       | Example (binary)        | Result |
| -------- | ----------------- | ----------------------- | ------ |
| `&`      | AND               | `5 & 3` → `101 & 011`   | `1`    |
| `\|`     | OR                | `4 \| 7` → `100 \| 011` | `7`    |
| `^`      | XOR               | `5 ^ 3` → `101 ^ 011`   | `6`    |
| `~`      | NOT (invert bits) | `~5`                    | `-6`   |
| `<<`     | Left shift        | `5 << 1`                | `10`   |
| `>>`     | Right shift       | `5 >> 1`                | `2`    |

Example:

```python
print(5 & 3)    # 1 → 101 & 011 = 001
print(5 | 3)    # 7 → 101 | 011 = 111
print(5 ^ 3)    # 6 → 101 ^ 011 = 110
print(~5)       # -6 → flips all bits of 5
print(5 << 1)   # 10 → 00000101 becomes 00001010
print(5 >> 1)   # 2  → 00000101 becomes 00000010
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.ncateam.xyz/fundamentals/python/basics-of-python/operators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
