Python Operators: Part-1

What are operators in Python?

In Python, operators are symbols or special keywords that represent different operations, such as addition, subtraction, and comparison. Operators are used to perform operations on variables and values, and they are an essential part of any programming language.

Python has several type of operators. They are:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical operators
  5. Bitwise Operators
  6. Identity Operators
  7. Membership Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and division. The arithmetic operators in Python are:


  • +- Adds 2 values
  • -- Subtracts one value from another
  • *- Multiplies 2 values
  • /- Divides one value or variable by another.
  • %- Returns the remainder of a division operation.
  • **- Raises one value or variable to the power of another.
  • //- Divides one value or variable by another and rounds down to the nearest integer.

For example-

a=10
b=3

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

Output:

13
7
30
3.3333333333333335
3
1
1000

2. Assignment Operators

Assignment operators are used to assign values to variables. The different types of assignment operators are given below:

  • =- Assigns the value on the right to the variable on the left.
  • +=- Adds the value on the right to the variable on the left and assigns the result to the variable on the left.
  • -=- Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.
  • *=- Multiplies the variable on the left by the value on the right and assigns the result to the variable on the left.
  • /=- Divides the variable on the left by the value on the right and assigns the result to the variable on the left.
  • %=- Calculates the modulus of the variable on the left with the value on the right and assigns the result to the variable on the left.
  • **=- Raises the variable on the left to the power of the value on the right and assigns the result to the variable on the left.
  • //=- Divides the variable on the left by the value on the right and rounds down to the nearest integer, and assigns the result to the variable on the left.

Here's an example to make it clear-

a=5

a+=2
print(a)

a-=2
print(a)

a*=3
print(a)

a**=2
print(a)

a/=4
print(a)

a//=4
print(a)

a%=3
print(a)

The output-

7
5
15
225
56.25
14.0
2.0

3. Comparison Operators

Comparison operators are used to compare two values or variables and return a Boolean value (True or False) based on the comparison result.

Read more about Boolean values here- Booleans in Python.

The different types of comparison operators are:

  • ==- Returns True if the values or variables on both sides of the operator are equal; otherwise, returns False.
  • !=- Returns True if the values or variables on both sides of the operator are not equal; otherwise, returns False.
  • >- Returns True if the value or variable on the left side of the operator is greater than the value or variable on the right side; otherwise, returns False.
  • <- Returns True if the value or variable on the left side of the operator is less than the value or variable on the right side; otherwise, returns False.
  • >=- Returns True if the value or variable on the left side of the operator is greater than or equal to the value or variable on the right side; otherwise, returns False.
  • <=- Returns True if the value or variable on the left side of the operator is less than or equal to the value or variable on the right side; otherwise, returns False.

For example-

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

False
True
False
True
False
True


That is all for this post. Hope it was useful to you. if you have any reviews or suggestions, you can contact us by filling the contact form or dropping a comment below.

You can read about more operators in this post- Python Operators: Part-2.

Happy coding,
Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I love drawing and am quite interested in aeronautics and astrophysics too. My favourite pastimes are reading books, blogging and skywatching with my telescope.

Post a Comment

Next Post Previous Post