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.
Contents
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.
Arithmetic
These operators perform mathematical tasks on the two values(operand), like +,-,/,*, etc. Let’s see each one of them in detail.
Operator | Name | Example | Explanation |
---|---|---|---|
+ | Addition | 1+1 = 2 | adds two numbers |
– | Subtraction | 1-1 = 0 | performs subtraction |
* | Multiplication | 1*3 = 3 | multiplies two numbers |
/ | Division | 4/2 = 2 | divides one number with the other |
// | Floor Division | 5//2 = 2 | gives the answer in integer after dividing |
% | Modulus | 5%2 = 1 | gives the remainder after dividing |
** | Exponentiation | 2**2 = 4 | raises 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.
Operator | Name | Example | Twin to | Explanation |
---|---|---|---|---|
= | Equals to | a= 10 | a=10 | This is used to give a value to any variable |
+= | Plus equals to | a= 10 | a=a+10 | This is used to add numbers in a pythonic way. |
-= | Minus equals to | a= 10 | a=a-10 | This is used to subtract in a pythonic way. |
*= | Multiply equals to | a= 10 | a=a*10 | This is used to multiply in a pythonic way |
/= | Divide equals to | a= 15 | a=a/10 | This is used to divide in a pythonic way |
//= | Floor division equals to | a= 15 | a=//10 | This is used to floor divide in a pythonic way |
**= | Exponentiation equals to | a= 2 | a=a**2 | This is used to use exponentiation in a pythonic way |
%= | Modulus equals to | a= 15 | a=a%10 | This 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.
Operator | Explanation | Example |
---|---|---|
== | 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.😊
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?.
This is an article from the official website that might be helpful. https://support.mozilla.org/en-US/kb/uninstall-firefox-from-your-computer
You can also checkout this article. https://support.mozilla.org/en-US/questions/973277
Hope this helps.
Answer is B: 6
No the correct answer is A: 4. Check explanation here