Python Operator


Python Operator

Operators are mainly those that can manipulate or work differently with the value of operands.

The types of operators are:

  • Arithmetic type of Operators
  • Relational or comparison type of Operators
  • Assignment type of Operators
  • Logical type Operators
  • Bitwise type Operators
  • Membership type Operators
  • Identity type Operators

Arithmetic type of operators:

Arithmetic operators are used to carry out mathematical operations. They are mainly used to perform operations with numeric values.

  • Addition (+): Used to perform addition between two operands.
  • Subtraction (-): It is used to subtract the right operand from the left operand.
  • Multiplication (*): It is used to perform multiplication between two operands.
  • Division (/): It is used to divide the left operand by the right one. And always gives the result in float value.
  • Modulus (%): It is used to divide the left-hand operand by the right-hand one and always returns the remainder.
  • Floor division (/): It is used to divide the first operand by the second one and results in a whole number adjusted to the left in the number line.
  • Exponentiation (**): Used to perform exponential calculations (power) on operands.

Comparison or relational type of operators:

It is used to make the comparison between two values. According to the given condition, it returns true or false.

  • (>) Greater than: It returns true if the left operand is greater than the right one.
  • (<) Less than: It returns true if the left operand is less than the right one.
  • (==) Equal to: It returns true if both operands are equal.
  • (!=) Not equal to: It returns True if operands are not equal.
  • (>=) Greater than or equal to: It returns True if the left operand is greater than or equal to the right one.
  • (<=) Less than or equal to: It returns True if the left operand is less than or equal to the right one.

Assignment operators:

They are usually used to assign values to the variables.

  • (=):Used to assign values from right side operands to left side one.
  • (+= Add AND): Used to add the right operand to the left one and attach the result to the left one.
  • (-= Subtract AND): Used to subtract the right operand from the left one and allot the result to the left one.
  • (*= Multiply AND): Used to multiply the right operand with the left one and attach the result to the left one.
  • (/= Divide AND): Used to divide the left operand with the right one and allot the result to the left one.
  • (%= Modulus AND): )Used to take modulus using two operands and allow the result to the left one.
  • (**= Exponent AND): Used to calculate exponential (power) on operators and attach value to the left one.
  • (//= Floor Division): used to calculate floor division on operators and allot value to the left one.

Logical operators:

It performs operations like AND, OR and NOT.

  • And Logical AND: It returns true if both the operands are true.
  • Or Logical OR: It returns true if either one of the operands is true.
  • Not Logical NOT: It returns a true value if the operand is false.

Bitwise Operator:

It acts on a bit and performs operation bit by bit.

  • (& Binary AND): The operator copies a bit if it remains in either operand.
  • (| Binary OR): The operator copies the bit if it is arranged in one operand but not on both.
  • (~ Binary One's Complement): The operator is unary and has the impact of 'flipping' bits.
  • (<< Binary Left Shift): The left operand's value is moved left by the number of bits defined by the right one.
  • (>> Binary Right Shift): The left operand's value is moved right by the number of bits defined by the right one.

Membership Operators in python:

The membership operators analyze for membership in a sequence, like strings, lists, or tuples. There two membership operators are:-

  • (in): It evaluates to true if it detects a variable in the detailed sequence and false unless.
  • (not in): It evaluates to true if it does not detect a variable in the detailed sequence and false unless.

Identity Operators in python:

It is used to compare the memory locations of more than one object.

  • (is):It decides to be true if the variables on one side of the operator point to a similar object and false unless.
  • (is not): It decides to false if the variables on one side of the operator point to a similar object and true unless.