DEV Community

Kailas Warade
Kailas Warade

Posted on

Python Operators Explained with Simple Examples

Python Operators Explained with Simple Examples

Python operators are symbols or keywords used to perform operations on values and variables.

For example:

a = 10
b = 3

print(a + b)
print(a > b)

Here, "+" adds two values and ">" compares them.

If you are learning Python or preparing for a Python interview, understanding operators is important because you will use them in almost every Python program.

*Main Types of Python Operators
*

Operator Type| Common Operators| Used For
Arithmetic| "+ - * / // % **"| Mathematical calculations
Comparison| "== != > < >= <="| Comparing values
Logical| "and or not"| Combining conditions
Assignment| "= += -= *= /="| Assigning and updating values
Membership| "in", "not in"| Checking whether a value exists
Identity| "is", "is not"| Checking object identity

Let's look at each type with simple examples.

1. Arithmetic Operators

Arithmetic operators are used for calculations.

a = 10
b = 3

print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000

Some operators need special attention.

"/" Division

"/" returns the normal division result.

print(10 / 3)

Output:

3.3333333333333335

"//" Floor Division

"//" returns the floor value of the division.

print(10 // 3)

Output:

3

"%" Modulus

"%" returns the remainder.

print(10 % 3)

Output:

1

It is often useful for checking whether a number is even or odd.

number = 10

if number % 2 == 0:
print("Even")

"**" Exponentiation

"**" is used to raise a number to a power.

print(2 ** 3)

Output:

8

2. Comparison Operators

Comparison operators compare two values.

The result is always "True" or "False".

a = 10
b = 20

print(a == b) # False
print(a != b) # True
print(a > b) # False
print(a < b) # True
print(a >= b) # False
print(a <= b) # True

One common mistake is confusing "=" and "==".

"=" vs "=="

"=" is used to assign a value.

age = 25

"==" is used to compare two values.

age == 25

This difference is very important in Python.

*3. Logical Operators
*

Logical operators are mainly used when working with multiple conditions.

Python has three logical operators:

  • "and"
  • "or"
  • "not"

"and"

Both conditions must be "True".

age = 25
salary = 50000

if age >= 18 and salary >= 30000:
print("Eligible")

"or"

At least one condition must be "True".

age = 25
salary = 20000

if age >= 18 or salary >= 30000:
print("Eligible")

"not"

"not" reverses the result.

is_active = True

print(not is_active)

Output:

False

*4. Assignment Operators
*

Assignment operators are used to assign or update values.

salary = 50000

salary += 5000
print(salary)

Output:

55000

Here are some common assignment operators:

Operator| Example| Meaning
"="| "x = 10"| Assign 10 to x
"+="| "x += 5"| Add 5 to x
"-="| "x -= 5"| Subtract 5 from x
"*="| "x *= 5"| Multiply x by 5
"/="| "x /= 5"| Divide x by 5

For example:

x = 10

x += 5
print(x) # 15

x -= 3
print(x) # 12

*5. Membership Operators
*

Membership operators check whether a value exists inside a sequence such as a list or string.

The two membership operators are:

  • "in"
  • "not in"

Example:

languages = ["Python", "Java", "SQL"]

print("Python" in languages)
print("C++" in languages)

Output:

True
False

You can also use them with strings:

name = "Python Programming"

print("Python" in name)

Output:

True

*6. Identity Operators
*

Python has two identity operators:

  • "is"
  • "is not"

They check whether two variables refer to the same object.

a = [1, 2, 3]
b = a

print(a is b)

Output:

True

Here, "b" refers to the same list object as "a".

"==" vs "is"

This is an important Python interview question.

"==" checks whether two values are equal.

"is" checks whether two variables refer to the same object.

For example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)
print(a is b)

Output:

True
False

The values are equal, but they are different list objects.

*7. Operator Precedence
*

When an expression contains multiple operators, Python follows operator precedence to decide which operation happens first.

For example:

result = 10 + 5 * 2

print(result)

Output:

20

Multiplication happens before addition.

You can use parentheses when you want to make the order clear:

result = (10 + 5) * 2

print(result)

Output:

30

Using parentheses also makes the code easier to read.

A Small Practical Example

Suppose we have an employee salary and want to calculate a bonus.

salary = 50000
bonus = 5000

total_salary = salary + bonus

if total_salary >= 55000:
print("Salary target reached")

This small example uses different operators together:

  • "+" calculates the total salary.
  • ">=" compares the salary.
  • "=" assigns values.
  • "if" uses the result of the comparison.

This is how operators are normally used in real Python programs.

Quick Revision
**
Operator| Example| Purpose
"+"| "a + b"| Addition
"-"| "a - b"| Subtraction
"*"| "a * b"| Multiplication
"/"| "a / b"| Division
"//"| "a // b"| Floor division
"%"| "a % b"| Remainder
"
"| "a ** b"| Power
"=="| "a == b"| Equal comparison
"!="| "a != b"| Not equal
">"| "a > b"| Greater than
"<"| "a < b"| Less than
"and"| "a and b"| Both conditions
"or"| "a or b"| Either condition
"not"| "not a"| Reverse condition
"in"| "x in list"| Check membership
"is"| "a is b"| Check object identity

Frequently Asked Questions
**
**What is the difference between "/" and "//" in Python
?

"/" performs normal division and returns a division result.

"//" performs floor division and returns the floor value.

print(10 / 3) # 3.3333333333333335
print(10 // 3) # 3

What is the difference between "==" and "is" in Python?

"==" checks whether two values are equal.

"is" checks whether two variables refer to the same object.

This is an important difference to understand when working with Python objects.

*More Python Operators Interview Questions
*

If you are preparing for a Python interview, you can find more operator-based interview questions and answers here:

*"Python Operators Interview Questions and Answers" (https://www.sankalandtech.com/Tutorials/Python/interview-questions/python-operators-overview.html)
*

The examples cover the important Python operators and explain where they are used.

Conclusion

Python operators are simple, but they are used everywhere in Python programming.

The most important ones to understand first are arithmetic, comparison, logical, assignment, membership, and identity operators.

Once these operators are clear, writing conditions and calculations in Python becomes much easier..

Top comments (0)