Operators in Python
Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 6 min read
Understand arithmetic, comparison, logical, assignment, membership, identity, and ternary-style operators in Python.
Operators are symbols or keywords used to perform actions on values. They help us calculate, compare, assign, and combine conditions. Almost every Python program uses operators because logic is built from expressions.
Arithmetic operators perform mathematical work. Addition, subtraction, multiplication, division, floor division, and remainder are used in many beginner problems. The remainder operator `%` is especially important because it helps check even or odd numbers, divisibility, cyclic movement, and last digit logic.
Comparison operators return `True` or `False`. For example, `marks >= 40` checks whether marks are at least 40. These operators are used in conditions, loops, searching, sorting comparisons, and boundary checks.
Logical operators combine conditions. Use `and` when all conditions must be true. Use `or` when any one condition is enough. Use `not` when you want to reverse a condition. These operators help create more realistic decision-making logic.
Assignment operators store or update values. For example, `count += 1` means increase the current value of count by one. This style is common in loops because counters, sums, and answers are updated repeatedly.
Students should understand operator precedence, which decides which operation happens first. Multiplication happens before addition. Comparisons happen after arithmetic. Parentheses are useful because they make the expression clear and prevent logical mistakes.
For example, when you write `a + b`, the `+` operator adds two values. When you write `a > b`, the `>` operator compares two values. When you write `marks >= 40 and attendance >= 75`, the `and` operator checks whether both conditions are true.
Operators are important because almost every Python program uses them. Whether you are calculating a sum, checking even or odd, comparing two numbers, updating a counter, or writing a condition inside `if`, you are using operators.
Arithmetic Operators
Arithmetic operators are used for mathematical calculations. They work with numeric values such as integers and floats.
- `+` addition
- `-` subtraction
- `*` multiplication
- `/` normal division
- `//` floor division
- `%` remainder
- `**` power
The `/` operator gives decimal division. For example, `7 / 2` gives `3.5`.
The `//` operator gives floor division. For example, `7 // 2` gives `3`. This is useful when you want an integer quotient.
Comparison Operators
Comparison operators compare two values and return a boolean result: either `True` or `False`.
- `>` greater than
- `<` less than
- `>=` greater than or equal to
- `<=` less than or equal to
- `==` equal to
- `!=` not equal to
For example, `marks >= 40` checks whether marks are at least 40. The result is a boolean value, not a number.
Logical Operators
Logical operators are used when one condition is not enough. They combine multiple conditions.
- `and` returns `True` when both conditions are true
- `or` returns `True` when at least one condition is true
- `not` reverses the condition
Use `and` when all conditions must be true. Use `or` when any one condition is enough. Use `not` when you want the opposite of a condition.
Assignment Operators
Assignment operators are used to store or update values in variables. The most basic assignment operator is `=`.
Python also supports shorthand assignment operators such as `+=`, `-=`, `*=`, `//=`, `/=`, and `%=`.
```python
total = 10
total += 5
```
After this code, `total` becomes `15`. In loops and DSA problems, shorthand operators are used frequently to update counters, sums, indexes, and answers.
Membership Operators
Membership operators check whether a value exists inside a sequence or collection.
- `in` checks whether a value is present
- `not in` checks whether a value is absent
```python
arr = [10, 20, 30]
print(20 in arr)
```
Output
```text
True
```
Membership checks are useful with lists, strings, sets, dictionaries, and many DSA lookup problems.
Identity Operators
Identity operators check whether two variables refer to the same object in memory.
- `is` checks same object identity
- `is not` checks different object identity
For normal value comparison, use `==`. For checking whether a variable is `None`, use `is None`.
Ternary-Style Expression
Python does not use the `? :` ternary syntax. Instead, it uses an expression like this:
```python
greater = a if a > b else b
```
If `a > b` is true, `greater` gets `a`. Otherwise, it gets `b`.
Operator Precedence
```python
result = 10 + 5 * 2
```
Here, multiplication runs first, so the result is `20`, not `30`. Use parentheses to make expressions clearer.
```python
result = (10 + 5) * 2
```
Now addition runs first, so the result is `30`.
Example Program
```python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", a + b)
print("Difference =", a - b)
print("Product =", a * b)
print("Quotient =", a // b)
print("Remainder =", a % b)
print("Greater =", a if a > b else b)
```
Sample Input
```text
10
3
```
Output
```text
Enter first number: Enter second number: Sum = 13
Difference = 7
Product = 30
Quotient = 3
Remainder = 1
Greater = 10
```
This program uses arithmetic operators for calculation, comparison for finding the greater number, and a ternary-style expression for selection.
Operators in DSA
Operators are used in almost every DSA problem. Arithmetic operators calculate sums and indexes. Comparison operators compare values. Logical operators combine conditions. Assignment operators update loop variables and answers.
When solving array, string, loop, searching, or sorting problems, operators become part of the logic. Understand where they are used instead of memorizing only the symbols.
Operators are the tools used to build expressions. An expression is a piece of code that produces a value. For example, `a + b`, `x > 10`, and `n % 2 == 0` are expressions because Python can evaluate them and produce a result.
Relational operators compare values and return `True` or `False`. These operators are commonly used inside `if` statements and loops. For example, while searching an array, `arr[i] == target` decides whether the required value has been found.
Python also has floor division `//`. Normal division `/` always gives a floating-point result, but floor division gives the quotient without the decimal part. This is useful in problems involving middle index, grouping, and integer calculations.
Practice Question
Write a Python program that takes two numbers as input and prints their sum, difference, product, quotient, remainder, and greater number. Also handle the case when the second number is zero.