operators in python

Operators In Python

Spread the love

Operators in python is a symbol that perform certain operation on one or more variable or a value.

There are 7 types of operators in python

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

1. Arithmetic operators

Arithmetic operators are used to solve arithmetic problems like Addition(+), Subtraction(-), Multiplication(*), Division(/) and so on.

NameSignExample
Addition + a + b
Subtraction a – b
Multiplication * a * b
Division / a / b
Modulus % a % b
Exponentiation ** a ** b
Floor Division // a // b

You are familiar with Addition, Subtraction, Multiplication and Division. But maybe you are not familiar with remaining 3 arithmetic operators. Modulus, Exponentiation and Floor Division.

So, let me explain

Modulus : This operator will give us the reminder

Operators In Python

Exponentiation : Raises the first number to the power of the second.

Operators In Python

Floor Division : Division that results into whole number

arithmetic Operators In Python

These are the arithmetic operators in python.

Comparison Operators

Comparison operators are used to compare two values in python.

NameSignExample
Equal to=a = b
Not equal to!=a != b
Greater than>a > b
Less than<a < b
Greater than or equal to>=a >= b
Less than or equal to<=a <= b

These are the comparison operators in python.

Assignment operators

In short assignment operators are used to assign values to variable.

OperatorExampleSame as
=a = 10a =10
+=a += 10a = a + 10
-=a -= 10a = a – 10
*=a *= 10a = a * 10
/=a /= 10a = a / 10
%=a %= 10a = a % 10
**=a **= 10a = a ** 10
//=a //= 10a = a // 10

These are the most basic assignment operators in python.

Logical operators

These are conjunctions that we can use to combine more than one condition. There are three logical operators in python. AND, OR and NOT

OperatorDescriptionExample
andIt returns True if both condition is true 5 > 2 and 3 != 30
orReturn True if one of these condition is true5 > 50 or 8 = 8
notReturn true if condition is false10 >= 30

We have studied this in our school time. “Logical Gates” same like that.

Identity operators

This will return True if identity of both operands is True. Otherwise it return false. There are 2 identity operators in python

OperatorDescriptionExample
isReturn True if the operands are identicala is b
is notReturn True if the operands are not identicala is not b
Operators In Python

Membership operators

Membership Operators are used to testing whether a value or variable is found in a sequence. See in and not in are the membership operators in python.

OperatorDescriptionExample
inReturn True if value/variable is found in the sequencea in b
not inReturn True if value/variable is not found in the sequencea in not b
Operators In Python

Bitwise Operators In Python

Bitwise operators act on operands as if they were strings of binary digits In python. Here are some examples NOT, AND, OR, XOR, and so on. Have a look at this table.

OperatorMeaningExample
&Bitwise ANDSets each bit to 1 if both bits
are 1
|Bitwise ORSets each bit to 1 if one of two bits is 1
^Bitwise XORSets each bit to 1 if only one of two bits is 1
~Bitwise NOTReverse all the bits
<<Bitwise right shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Bitwise left shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

This is all about operators in python.

If you have any doubt you can ask me on Instagram

Posts You may Like : Data Types In Python


Spread the love
3 comments
Leave a Reply

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

You May Also Like

If-else In Python

Spread the love

Spread the love If-else in python used to check conditions. These statements are also used to make decisions. before we move further have a quick look at comparison operators, which…
Spread the love
View Post

Comments In Python

Spread the love

Spread the love In programming, comments are the readable texts in the source code which are completely ignored by compiler or interpreter. It helps the programmer to remember what tricky…
Spread the love
View Post

Loops In Python

Spread the love

Spread the love There are 2 type of loops in python. for loop while loop Let’s have detailed information about loops in python. For loops in Python for loops in…
Spread the love
View Post