# Operators

> Operators are special symbols that perform operations on values and variables. Python provides a rich set of operators organized into several categories:...

Canonical: https://www.internhack.in/learn/python/python-fundamentals/operators
Last updated: 2026-11-28

Difficulty: Beginner

Operators are special symbols that perform operations on values and variables. Python provides a rich set of operators organized into several categories: arithmetic, comparison, logical, and assignment operators. Understanding these is fundamental to writing any Python program.

Arithmetic operators perform mathematical calculations. Python supports the standard operators: + (addition), - (subtraction), \* (multiplication), / (division), // (floor division), % (modulus/remainder), and (exponentiation). One important distinction is between / and //: the / operator always returns a float, while // returns the largest integer less than or equal to the result.

Comparison operators compare two values and return a boolean (True or False). They include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). These are essential for conditional logic and loops. Python also supports chained comparisons like 1 < x < 10, which is equivalent to 1 < x and x < 10.

Logical operators combine boolean expressions. Python uses the English words and, or, and not instead of symbols like && and ||. The and operator returns True only when both operands are True, or returns True when at least one operand is True, and not negates a boolean value. Python uses short-circuit evaluation: in an and expression, if the first operand is False, the second is never evaluated.

Assignment operators provide shorthand for updating variables. Beyond the basic = operator, Python supports augmented assignment operators like +=, -=, \*=, /=, //=, %=, and =. These modify a variable in place. For example, x += 5 is equivalent to x = x + 5. Note that Python does not have ++ or -- increment/decrement operators like C or Java.

## Code examples

### Arithmetic Operators

```
a = 17
b = 5

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a  b)
```

Demonstrates all 7 arithmetic operators. Note that / returns 3.4 (float) while // returns 3 (floor division). The % operator gives the remainder, and raises to a power.

### Comparison Operators

```
x = 10
y = 20

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= 10)
print(x <= 5)
```

Each comparison returns a boolean. These operators are the building blocks of conditional statements (if/else) that we will cover in later lessons.

### Logical Operators

```
a = True
b = False

print(a and b)
print(a or b)
print(not a)
print(not b)

# Chained comparison
x = 15
print(10 < x < 20)
print(20 < x < 30)
```

Logical operators combine booleans. Python's chained comparisons (10 < x < 20) are a clean alternative to writing 10 < x and x < 20.

### Assignment Operators

```
x = 10
print(x)

x += 5
print(x)

x -= 3
print(x)

x *= 2
print(x)

x //= 4
print(x)

x = 3
print(x)
```

Augmented assignment operators modify a variable in place. x += 5 is shorthand for x = x + 5. The sequence: 10 -> +5=15 -> -3=12 -> \*2=24 -> //4=6 -> 3=216.

## Key points

- Division (/) always returns a float in Python 3, even when dividing two integers: 10 / 2 gives 5.0, not 5.
- Floor division (//) rounds toward negative infinity, not toward zero: -7 // 2 gives -4, not -3.
- Python does not have ++ or -- operators. Use x += 1 or x -= 1 instead.
- Operator precedence follows standard math rules: > unary - > \*, /, //, % > +, -. Use parentheses to override.

## Interview tips

- Be prepared to explain the difference between / and // and how floor division behaves with negative numbers. -7 // 2 = -4 is a common gotcha.
- Know that Python's logical operators (and, or) return one of their operands, not necessarily True/False. For example, 0 or 'hello' returns 'hello', and '' and 'world' returns ''.

## Concepts covered

Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Operator precedence

Previous: [Variables & Data Types](https://www.internhack.in/learn/python/python-fundamentals/variables-and-data-types) · Next: [Type Casting](https://www.internhack.in/learn/python/python-fundamentals/type-casting) · [All of Python Fundamentals](https://www.internhack.in/learn/python/python-fundamentals)

## More Python Fundamentals lessons

- [Input & Output](https://www.internhack.in/learn/python/python-fundamentals/input-and-output)
- [What is Python?](https://www.internhack.in/learn/python/python-fundamentals/what-is-python)

## Related

- [Learn](https://www.internhack.in/learn)
- [Python](https://www.internhack.in/learn/python)
- [Python Fundamentals](https://www.internhack.in/learn/python/python-fundamentals)
