Python Operators: Arithmetic, Comparison, Logical & Assignment Operators

Python Operators: Arithmetic, Comparison, Logical & Assignment Operators

Python operators are special symbols and keywords used to perform operations on values and variables. They allow us to perform calculations, compare values, combine conditions, assign values, and perform many other operations in Python.

For example:

a = 10
b = 5

print(a + b)

Output:

15

Here, + is an arithmetic operator used to add two values.

In this tutorial, we will learn the most commonly used Python operators, including:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators

We will also look at practical examples and common mistakes beginners should avoid.


What Are Operators in Python?

An operator is a symbol or keyword that tells Python to perform a particular operation.

For example:

x = 10
y = 20

result = x + y

print(result)

Output:

30

In this example:

  • x and y are operands.
  • + is the operator.
  • x + y is an expression.
  • 30 is the result.

Basic Structure

Operand → Operator → Operand

Example:

10 + 20

Here:

10 → Operand
+  → Operator
20 → Operand

Types of Operators in Python

Python provides several types of operators.

Operator TypePurpose
Arithmetic OperatorsPerform mathematical calculations
Comparison OperatorsCompare two values
Logical OperatorsCombine multiple conditions
Assignment OperatorsAssign and update values
Identity OperatorsCompare object identity
Membership OperatorsCheck whether a value exists
Bitwise OperatorsPerform operations at bit level

In this tutorial, we will focus mainly on the four fundamental types:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Assignment Operators

1. Arithmetic Operators in Python

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.

Python provides the following arithmetic operators:

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52.0
%Modulus10 % 31
**Exponentiation2 ** 38
//Floor Division10 // 33

Let’s understand each one.


Addition Operator +

The + operator is used to add two values.

a = 10
b = 20

result = a + b

print(result)

Output:

30

The + operator can also be used with strings.

first_name = "Himanshu"
last_name = "Sharma"

name = first_name + " " + last_name

print(name)

Output:

Himanshu Sharma

Subtraction Operator -

The - operator is used to subtract one value from another.

a = 20
b = 8

print(a - b)

Output:

12

Multiplication Operator *

The * operator is used for multiplication.

price = 100
quantity = 5

total = price * quantity

print(total)

Output:

500

It can also repeat strings.

print("Python " * 3)

Output:

Python Python Python

Division Operator /

The / operator performs division.

a = 10
b = 2

result = a / b

print(result)

Output:

5.0

Notice that Python returns a float when using /, even when the result is mathematically a whole number.

print(10 / 2)

Output:

5.0

Modulus Operator %

The % operator returns the remainder after division.

print(10 % 3)

Output:

1

Because:

10 ÷ 3 = 3 remainder 1

Practical Example

The modulus operator is commonly used to check whether a number is even or odd.

number = 10

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

Output:

Even

Exponentiation Operator **

The ** operator is used to calculate powers.

print(2 ** 3)

Output:

8

This means:

2 × 2 × 2 = 8

Another example:

print(5 ** 2)

Output:

25

Floor Division Operator //

The // operator performs division and returns the floor value.

print(10 // 3)

Output:

3

Compare it with normal division:

print(10 / 3)

Output:

3.3333333333333335

Whereas:

print(10 // 3)

Output:

3

2. Comparison Operators in Python

Comparison operators are used to compare two values.

The result of a comparison is always a Boolean value:

True

or

False

Python provides these comparison operators:

OperatorMeaningExample
==Equal to10 == 10
!=Not equal to10 != 5
>Greater than10 > 5
<Less than5 < 10
>=Greater than or equal to10 >= 10
<=Less than or equal to5 <= 10

Equal To ==

The == operator checks whether two values are equal.

a = 10
b = 10

print(a == b)

Output:

True

If the values are different:

print(10 == 20)

Output:

False

Important: = vs ==

This is one of the most common mistakes beginners make.

x = 10

= assigns a value.

x == 10

== compares two values.


Not Equal !=

The != operator checks whether two values are different.

a = 10
b = 20

print(a != b)

Output:

True

Greater Than >

The > operator checks whether the left value is greater than the right value.

print(20 > 10)

Output:

True

Less Than <

The < operator checks whether the left value is smaller than the right value.

print(10 < 20)

Output:

True

Greater Than or Equal To >=

This checks whether a value is greater than or equal to another value.

age = 18

print(age >= 18)

Output:

True

This is commonly used in conditions such as age validation.


Less Than or Equal To <=

This checks whether a value is less than or equal to another value.

marks = 35

print(marks <= 40)

Output:

True

3. Logical Operators in Python

Logical operators are used to combine multiple conditions.

Python provides three logical operators:

OperatorMeaning
andReturns true when all conditions are true
orReturns true when at least one condition is true
notReverses the result

and Operator

The and operator returns True only when both conditions are true.

age = 25
has_license = True

print(age >= 18 and has_license)

Output:

True

Both conditions are true:

age >= 18       → True
has_license     → True

Therefore:

True and True → True

Example

username = "admin"
password = "12345"

if username == "admin" and password == "12345":
    print("Login successful")

Output:

Login successful

or Operator

The or operator returns True when at least one condition is true.

age = 17
has_permission = True

print(age >= 18 or has_permission)

Output:

True

The first condition is false, but the second condition is true.

False or True → True

not Operator

The not operator reverses a Boolean result.

is_logged_in = True

print(not is_logged_in)

Output:

False

Another example:

is_admin = False

if not is_admin:
    print("Access denied")

Output:

Access denied

Logical Operators Truth Table

Understanding truth tables makes logical operators much easier.

AND

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

OR

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

NOT

Anot A
TrueFalse
FalseTrue

4. Assignment Operators in Python

Assignment operators are used to assign values to variables.

The basic assignment operator is:

=

Example:

name = "Himanshu"
age = 28

Python also provides compound assignment operators.

OperatorExampleEquivalent To
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
**=x **= 2x = x ** 2
//=x //= 2x = x // 2

Basic Assignment =

name = "Himanshu"
age = 28

Here Python stores the values in the respective variables.


Addition Assignment +=

Instead of:

x = x + 5

we can write:

x += 5

Example:

x = 10

x += 5

print(x)

Output:

15

Subtraction Assignment -=

x = 10

x -= 3

print(x)

Output:

7

This is equivalent to:

x = x - 3

Multiplication Assignment *=

x = 5

x *= 3

print(x)

Output:

15

Division Assignment /=

x = 10

x /= 2

print(x)

Output:

5.0

Modulus Assignment %=

x = 10

x %= 3

print(x)

Output:

1

Exponentiation Assignment **=

x = 2

x **= 3

print(x)

Output:

8

Practical Example Using Multiple Operators

Let’s combine arithmetic, comparison, logical, and assignment operators in one example.

price = 1000
discount = 100

price -= discount

print("Final Price:", price)

if price > 500 and price <= 1000:
    print("Discounted product available")

Output:

Final Price: 900
Discounted product available

In this example:

  • -= is an assignment operator.
  • > and <= are comparison operators.
  • and is a logical operator.

Operator Precedence in Python

When multiple operators are used in the same expression, Python follows a specific order of precedence.

For example:

result = 10 + 5 * 2

print(result)

Output:

20

Why not 30?

Because multiplication is performed before addition.

Python effectively evaluates:

10 + (5 * 2)

So:

10 + 10 = 20

Common Precedence Order

A simplified order is:

  1. () Parentheses
  2. ** Exponentiation
  3. *, /, //, %
  4. +, -
  5. Comparison operators
  6. not
  7. and
  8. or

Use Parentheses for Clarity

Instead of relying on precedence:

result = 10 + 5 * 2

you can make the intention clear:

result = 10 + (5 * 2)

Common Mistakes Beginners Make

1. Confusing = and ==

Incorrect understanding:

if age = 18:

Correct:

if age == 18:

Use:

=

for assignment and:

==

for comparison.


2. Confusing / and //

10 / 3

returns:

3.3333333333333335

while:

10 // 3

returns:

3

3. Forgetting Operator Precedence

Consider:

result = 10 + 2 * 5

The answer is:

20

not:

60

Use parentheses when the calculation needs to be explicit.


4. Using and When or Is Required

These two operators have different meanings.

age >= 18 and age <= 60

means both conditions must be true.

Whereas:

age < 18 or age > 60

means at least one condition must be true.

Understanding the difference is very important when writing conditions.


Python Operators Quick Reference

CategoryOperators
Arithmetic+, -, *, /, %, **, //
Comparison==, !=, >, <, >=, <=
Logicaland, or, not
Assignment=, +=, -=, *=, /=, %=, **=, //=

Practice Questions

Try solving these examples yourself.

Question 1

What will be the output?

x = 10
y = 3

print(x + y)
print(x % y)
print(x // y)

Question 2

What will this return?

print(10 > 5 and 20 > 10)

Question 3

What will be the final value of x?

x = 10

x += 5
x *= 2

Question 4

Check whether a number is even:

number = 24

Hint:

number % 2

Question 5

Write a condition that checks whether a person’s age is between 18 and 60.


Frequently Asked Questions

What are operators in Python?

Operators are symbols or keywords used to perform operations on values and variables, such as calculations, comparisons, logical operations, and assignments.

What are the main types of Python operators?

Python has several types of operators, including arithmetic, comparison, logical, assignment, identity, membership, and bitwise operators.

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

= is used to assign a value to a variable, while == is used to compare two values.

What does % mean in Python?

The % operator is called the modulus operator. It returns the remainder after division.

For example:

10 % 3

returns:

1

What is the difference between / and //?

/ performs normal division and returns a float, while // performs floor division.

10 / 3
# 3.3333333333333335

10 // 3
# 3

What does and do in Python?

The and operator returns true when all the specified conditions are true.

What does or do in Python?

The or operator returns true when at least one of the specified conditions is true.


Conclusion

Python operators are one of the most important concepts to understand before moving to conditions, loops, functions, and more advanced Python programming.

The four operators covered in this tutorial are especially important for beginners:

  • Arithmetic operators help perform calculations.
  • Comparison operators help compare values.
  • Logical operators help combine multiple conditions.
  • Assignment operators help assign and update variable values.

Once you understand these operators, you will be able to write much more useful Python programs and understand conditional statements more easily.

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *