What Are Operators, Their 6 Types, And Precedence In Python

Have you ever heard of the word operation, in medical terms, say, heart operation?

Can it happen without a knife🔪 and other tools💉💊🩺? Well, I am not a doctor but still, I can say, no, that’s not possible.

Similarly, in python, we need to do some operations, don’t worry you don’t need to be a doctor👩‍⚕️👨‍⚕️ for them, I mean operations on numbers or other data types using tools called operators.

There are mainly 6 types of operators in python.

Operators are the special symbols in python used to perform logical🤨 or arithmetic🔢 operations.

Hello Pythonistas😊, welcome back, today in this post we are going to explore what are operators, and their types, using operators on strings, and operator precedence.

Don’t worry if these terms and the definition above sound difficult😱 to you, I will explain these in the easiest possible way.

Previous post’s challenge’s solution

Before we get into operators here is the solution for the challenge in the previous post.

The sum of divisors of a number is the summation of all the divisors of that no. (as simple as it may sound).

print(1+2+4+8+16)

Output:-

31

These are all arithmetic operators.

Operators in python

Operators are simple symbols or keywords(reserved by python) that perform some task between two given values.

These values can be string, integer, float, or any data type and even variable.

See simple😇, isn’t it? Let’s in detail find out about all of its 6 types.

Types of operators

Arithmetic

These operators perform mathematical tasks on the two values(operand), like +,-,/,*, etc. Let’s see each one of them in detail.

OperatorNameExampleExplanation
+Addition1+1 = 2adds two numbers
Subtraction1-1 = 0performs subtraction
*Multiplication1*3 = 3multiplies two numbers
/Division4/2 = 2divides one number with the other
//Floor Division5//2 = 2gives the answer in integer after dividing
%Modulus5%2 = 1gives the remainder after dividing
**Exponentiation2**2 = 4raises first to the power of second

Assignment

Has your teacher👩‍🏫 ever assigned you homework?

She might have assigned it to you using blackboard and chalk.

Assignment operators assign values to variables, they work similar to chalk and blackboard, that is, act as a tool for assignment.

For example, a = 10 here, '=' is an assignment operator it gives the variable 'a' which is on the left the value '10' which is on right.

Let’s look at these operators in detail.

OperatorNameExampleTwin toExplanation
=Equals toa= 10a=10This is used to give a value to any variable
+=Plus equals toa= 10
a+= 10
print(a) #20
a=a+10This is used to add numbers in a pythonic way.
-=Minus equals toa= 10
a-= 10
print(a) #0
a=a-10This is used to subtract in a pythonic way.
*=Multiply equals toa= 10
a*= 10
print(a) #100
a=a*10This is used to multiply in a pythonic way
/=Divide equals toa= 15
a/= 10
print(a) #1.5
a=a/10This is used to divide in a pythonic way
//=Floor division equals toa= 15
a//= 10
print(a) #1
a=//10This is used to floor divide in a pythonic way
**=Exponentiation equals toa= 2
a**= 2
print(a) #4
a=a**2This is used to use exponentiation in a pythonic way
%=Modulus equals toa= 15
a%= 10
print(a) #5
a=a%10This is used to get the remainder in a pythonic way

Comparison

Tell me when was the last time you shopped online🌐?

Did you have options to choose from?

How did you choose the best from the available options?

Maybe you did it by comparing the prices💵, quality✅, or brand🏆 of the product.

Comparison operators are used for the same purpose.

No, they won’t help you compare products you shop online, they help you out with comparing numbers or strings or other data types.

Like, say you want to know if this is 41919.21524 greater than this 119749.14877 or not.

You can simply know it by typing in idle 41919.21524>119749.14877 and pressing enter and getting the answer as False which means it’s not greater.

Let’s look at these operators in detail.

OperatorExplanationExample
==This checks if LHS equals RHS.print(10==10) #True
!=This checks if LHS is not equal to RHS.print(10!=11) #True
<=This checks if LHS is less than or equal to RHS.print(12<=10) #False
>=This checks if LHS is greater than or equal to RHS.print(10>=10) #True
<This checks if LHS is less than RHS.print(100<10) #False
>This checks if LHS is greater than RHS.print(100>10) #True

Conclusion

Operators in python are tools🛠 that help you with logical🧠 and mathematical🔢 functions in python.

There are mainly 6 types of operators in python:- arithmetic, assignment, identity, membership, comparison, and logic.

We discussed 3 of them here. Rest along with their precedence will be discussed in the next article.

Challenge🧗‍♀️

Your challenge for the article will be super simple. Just solve this:

What will be the output of the following code:

x = 10
y = 3
x += y * 2
x //= 4

Select an option:🤔🧐

A: 4, B: 6, C: 7, D:8

Go fast. I am waiting. Comment your answers below.

This was it for the post. Comment below suggestions if there and tell us whether you liked it or not. Do consider sharing this with your friends.

See you in the next post till then have a great time.😊

Leave a Reply

This Post Has 4 Comments

  1. Informacje

    The mozzila firefox is removed from the add remove programs but still it is not uninstalled. I would like to uninstall it is there any way to uninstall it using command promt or from registry?.

  2. Parekh Khushi

    Answer is B: 6

  3. Maitry

    No the correct answer is A: 4. Check explanation here