Python - Operators

11-Jan-2024

Explore the various operators in Python, from arithmetic and comparison to logical and bitwise operators. Gain a comprehensive understanding of how operators function in Python programming.

OverView


Operators in Python are symbols or keywords that perform operations on operands. Operands can be variables, literals, or expressions. Python supports a variety of operators, categorized into different types. This documentation provides an overview of the main types of operators in Python.



1. Arithmetic Operators


Arithmetic operators perform basic mathematical operations :


  • + (Addition): Adds two operands.
  • - (Subtraction): Subtracts the right operand from the left operand.
  • * (Multiplication): Multiplies two operands.
  • / (Division): Divides the left operand by the right operand.
  • % (Modulus): Returns the remainder of the division.
  • // (Floor Division): Returns the floor value after division.
  • ** (Exponentiation): Raises the left operand to the power of the right operand.


Arithmetic Operators Example
Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b
** Exponentiation a ** b
// Floor division a // b



2. Comparison Operators


Comparison operators compare two values and return a Boolean result :


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


Comparison Operators Example
Operator Name Example Result
== Equal a == b True if a is equal to b
!= Not Equal a != b True if a is not equal to b
< Less Than a < b True if a is less than b
> Greater Than a > b True if a is greater than b
<= Less Than or Equal To a <= b True if a is less than or equal to b
>= Greater Than or Equal To a >= b True if a is greater than or equal to b



3. Logical
Operators


Logical operators perform logical operations and return a Boolean result :

  • and : True if both operands are true.
  • or : True if at least one operand is true.
  • not : True if the operand is false (logical negation).


Logical Operators Example
Operator Name Example Result
and Logical AND a and b True if both a and b are true
or Logical OR a or b True if at least one of a or b is true
not Logical NOT not a True if a is false; False if a is true



4. Assignment Operators


Assignment operators assign values to variables :


  • = (Assignment): Assigns the value on the right to the variable on the left.
  • += (Addition Assignment): Adds the right operand to the left operand and assigns the result to the left operand.
  • -= (Subtraction Assignment): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • *= (Multiplication Assignment): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • /= (Division Assignment): Divides the left operand by the right operand and assigns the result to the left operand.


Assignment Operators Example
Operator Name Example Equivalent
= Assignment a = b Assigns the value of b to a
+= Addition Assignment a += b Equivalent to a = a + b
-= Subtraction Assignment a -= b Equivalent to a = a - b
*= Multiplication Assignment a *= b Equivalent to a = a * b
/= Division Assignment a /= b Equivalent to a = a / b
%= Modulus Assignment a %= b Equivalent to a = a % b
**= Exponentiation Assignment a **= b Equivalent to a = a ** b
//= Floor Division Assignment a //= b Equivalent to a = a // b



5. Membership Operators


Membership operators test if a value is a member of a sequence:

  • in : True if a value is found in the sequence.
  • not in : True if a value is not found in the sequence.


Membership Operators Example
Operator Name Example Result
in Membership - True if value is found in the sequence a in b True if a is found in b
not in Membership - True if value is not found in the sequence a not in b True if a is not found in b



6. Identity Operators


Identity operators test the identity of objects :


  • is : True if both operands refer to the same object.
  • is not : True if both operands do not refer to the same object.


Identity Operators Example
Operator Name Example Result
is Identity - True if both variables are the same object a is b True if a and b refer to the same object
is not Identity - True if both variables are not the same object a is not b True if a and b do not refer to the same object


 

7. Bitwise Operators


Bitwise operators perform bit-level operations :


  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left Shift)
  • >> (Right Shift)


Bitwise Operators Example
Operator Name Example Result
& Bitwise AND a & b Each bit position in the result is the logical AND of the bits in the corresponding positions of a and b
| Bitwise OR a | b Each bit position in the result is the logical OR of the bits in the corresponding positions of a and b
^ Bitwise XOR a ^ b Each bit position in the result is the logical XOR of the bits in the corresponding positions of a and b
~ Bitwise NOT ~a The bitwise NOT of a is the same as -(a+1)
<< Left Shift a << b Shifts the bits of a to the left by b positions
>> Right Shift a >> b Shifts the bits of a to the right by b positions


Understanding and using these operators is essential for writing efficient and expressive Python code. Operators play a crucial role in manipulating data and controlling program flow.

Comments