Types of Operators in Python Programming

Types of Operators in Python Programming

In this section, we cover the different types of operators used in Python Programming. Each type we discuss in this section with example in an easy way.

What are Operators

Operators are constructions that can manipulate the value of the operand.

Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + are called the operator.


Operator types


The Python language supports the different types of operators. The list of these operators is given below:


  1. arithmetic operators
  2. Comparison of Relational Operators
  3. Assignment operators
  4. logical operators
  5. Bitwise operators
  6. Membership operator
  7. Identity Operators


Let's look at all the operators one by one.


1. Arithmetic operators


Assuming the variable holds the value 10 and the variable b as 20, then -


Addition (+) -:  adds value to either side Operator.

a + b = 30 is

Subtraction (-) -: subtracts left to right-hand operands Handwork.

a - b = -10

Multiplication(*) -: Multiplies the values on either side of the operator

a * b = 200 is

Division(/) -: divisions with right hand / left-hand operand

b / a = 2

Modulus(%) -: divider operates with the left-hand right hand
An operand and return balance

b% a = 0

Exponential(** ) -: Exponential power calculates at an exponent
Operators

a ** b = 10 power up to 20

Floor division(// ) - division of operands
Where the result is the quotient Digits after decimal point removed.

9 // 2 = 4 and 9.0 // 2.0 = 4.0


2. Comparative operator


These operators compare the values on both sides and determine the relationship between them. They are also called relational operators.

Assuming the variable holds the value 10 and the variable b as 20, then -


(==) equal to

If the value of two operands is equal,
Then the condition becomes true.

a == b is not true.

= = If the value of two operands is not equal,
Then the condition becomes true.

<> If the value of two operands is not equal,
Then the condition becomes true.

a <> b is true. 

(>)Greater than

 If the value of the left operand is greater than the right operand, the condition is true.

a> b is not true.


(<) Less than

If the left operand value is less than the right operand value, the condition is true.

a <b is true.

(> =) Greater than or equal to

If the value of the left operand is greater than
Or equal to the value of the other operand, the condition is true.

a> = b is not true.

(<=) Less than or equal to

If the value of the left operand is less than or
Equal to other operands, then the condition becomes true.

a <= b is true.


3. Assignment operators

Assuming the variable holds the value 10 and the variable b as 20, then -


= Assigns a value to right side operand Left operand

c = a + b gives the value of a + b in c

(+ =) Add

This adds the right operand to the left operand.
And assign the result to the left operand

c + = a is equal to c = c + a

(- =) Subtract

This subtracts the operand from left to right.
Operate and assign the result to the left
Operand

c - = a is equal to c = c - a

(* =) Multiplication

It multiplies the right operand with the left.
Operate and assign the result to the left Operand

c * = a is equal to c = c * a

(/ =) Split

This divides the left operand from the right.
Operate and assign the result to the left Operand

c / = a is equal to c = c / ac / = a
equal to c = c / a

(% =) Modulus

It takes the modulus using two operands and
Assign the result to the left operand

c% = a is equal to c = c% a

(** =) Exponent

Calculates exponential power at Operator and specify the value to the left

c ** = a is equal to c = c ** a

(// =) floor

the division - It splits floors between operators
And assign a value to the left operand

c // = a a is equal c = c // a


4. logical operators


The use of logical operators in Python is true or false for conditional statements. Python has logical operators AND, OR and NOT. The following conditions are applied to logical operators.

AND operator - It Returns TRUE if both operands (right and left) are true

OR operator - It returns TRUE if either of the operands (right or left) is true.


NOT operator- It returns TRUE if the operand is not true

5. Bitwise operators

The bitwise operator works on bit and bit by bit operation. Suppose a = 60; And B = 13;

Now in binary format, they will be as follows -
a = 0011 1100
b = 0000 1101
------------------------
a & b = 0000 1100
a | b = 0011 1101
A ^ B = 0011 0001
~ a = 1100 0011
------------------------

The following are the bitwise operators supported by the Python language.


6. Identity operators

Identity operators are used to comparing objects, not if they are the same, but if they are in fact the same object, with the same location:

is:- If both variables are the same object, this is true.

a is b

is not:- If both variables are not equal then the return is not true.

a is not b 


7. Membership operator

Membership operators are tested to see if a sequence is presented in an object:

in:- Returns true if a sequence with the specified value exists in object 

a in b

not in:- Returns true if not present in a sequence object with the specified value.

 a not in b