x

Python Operators

PREV     NEXT

  • Operators are very familiar with basic mathematics.
  • Operator is nothing but performing the specific operation between operands. It is called the operator.

Operator classification:

  • Operators can be classified based on the operations that they perform or based on the number of operands it operates on.
  • Based on the number of operands, we can classify operators as given below.
  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator

1. Unary Operators

The unary operators operate on a single operand. The addition, subtraction and multiplication operators are used as unary operator in python. We will discuss more about this operator below.


2. Binary Operators

A binary operator is an operator that operates on two operands.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Membership operators
  6. Identity operators
  7. Bitwise operators

3. Ternary Operators

The ternary operator is an operator that operates on three operands. The first argument is an expression, If expression is true then second argument will execute else third argument will execute.

Conditional Operators:

Now, we are going in detail of all operators one by one.

1. Arithmetic Operators:

Operator / Meaning    example
 +   (To perform Addition) 23+33=56
 –    (To perform subtraction) 33-23=10
 *    (To perform Multiplication) 10*20=200
 /     (To perform Division (To get the co-efficient)) 20/10=2
 %     (Modulus (To get the reminder)) 5%4=1
 **     (exponent operator(power of)) 2**3=8

2. Assignment Operators:

  • Assignment operator is used to assign value to a variable (memory location).  It evaluates expression on right side of = symbol and assigns evaluated value to left side the variable.
  • The RHS of assignment operator must be a constant, expression or variable. Whereas LHS must be a variable (valid memory location).

Example:

Shorthand assignment operator:

Python supports a short variant of assignment operator called compound assignment or shorthand assignment operator. Shorthand operator combines one of the Arithmetic operator or bitwise operator

There are many ways of using the short hand operator. They are listed below.

Shorthand assignment operator / Example Meaning
+=       Example: a+=5 a=a+5
-=        Example: a-=5 a=a-5
*=       /       a*=5 a=a*5
/=       /       a/=5 a=a/5
%=      /       a%=5 a=a%5
&=      /        a&=5 a=a&5
|=       /        a|=5 a=a|5
^=      /        a^=5 a=a^5
~=      /        a~=5 a=a~5
<<=    /       a<<=5 a=a<<5
>>=    /       a>>=5 a=a>>5

3. Relational Operators:

Relational operator is nothing but an operator which defines some kind of relationship between two entities or two objects or two variables.

Operator Description
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
== Equal to
!= Not equal to
<> Not equal to

Example:


 4. Logical Operators:

  • Logical operators are mainly used to control program flow
  • The concept of logical operators is simple.
  • In logical AND operator, if any one  operand is false then result is false else result is true
  • In logical OR operator, if any one operand is true then result is true else result is false
  • In logical NOT operator, it will work on single operand, if operand is true result is false else vice versa

Logical operator is divided as below.

Operators Meaning
and Logical AND, if both operands are true then result is true
or Logical OR, if  one operand is true then result is true
not if operand true, then result is false

Truth table for Logical AND

Operand1  /  Operand2 Output
False    /     False False
False    /     True False
True     /     False False
True     /     True True

Truth table for Logical OR

Operand1 /  Operand2   Output
False      /       False False
False      /       True False
True       /       False False
True       /       True True

Truth table for Logical NOT

Operand1 Output
False True

5. Membership Operator:

  • The membership operator is a special operator introduced in python programming language
  • It is used to validate the membership of value
  • It is used to test the membership in sequence data types like list, tuples, and strings
Operator Meaning
In The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false otherwise.
not in This operand is used to find the variable which are not existing other than the values given in the sequence.

6. Identity operators:

This operator is used identify type of the object, variable etc. There are different identity operators such as,

Opearator Meaning
         Is Evaluate true if identity of two operands are same, else false
        is not Evaluate false if identity of two operands are same, else true

Example:

Output:

both are  same identity
both are different  identity

7. Bitwise operators:

Bitwise operator is working on 0 and 1’s and manipulate the bits, it will give the performance wise very well. Bitwise operation scripting is very helpful in hardware level programming.

Consider inputs are a=10 ( 1010 ) and b=6  (0110)

Operator Symbol / Name / Desciption Example  /  Output
&      ( Binary AND   –    If all bits are 1,then output 1 ) (a & b) (1010 & 0110)  /  0010
|        ( Binary OR    –   If any one bits is 1,then output 1 ) (a | b) (1010 | 0110)      /  1110
^       ( Binary XOR      –   If all bits are 1 or 0,then output 1 ) (a ^ b) (1010 ^ 0110)    /  0001
~       ( Binary  Complement   –  It compliment operator,like logical not ) (~a ) (~(1010))                 /   0101
<<    ( Binary Left Shift      –   It will shift to left no.of bits ) a << 1 ((1010) << 1)      /  0100
>>    ( Binary Right Shift     –  It will shift to right no.of bits ) a >> 1 ((1010) >> 1)       /  0101

PREV     NEXT



Like it? Please Spread the word!