Categories: python

Python operators

What are Python operators?

Python operators are used to perform operations on values and variables in general. These are standard symbols used in logical and arithmetic operations. In this article, we will look at various Python operators.

Note: OPERAND is the value on which the operator is applied.

Types of Operators:

Python language supports the following types of operators.

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators
Arithmetic Operators:

Arithmetic operators are used to performing operations on two operands. It contains the + (addition), – (subtraction), * (multiplication), / (divide), % (remainder), /(floor division), and exponent (**) operators.

OperatorDescriptionExample
a=10 b=20
+ AdditionAdds values on either side of the operator.a + b = 30
– SubtractionSubtracts right operand from the left operand.a – b = -10
* MultiplicationMultiplies values on either side of the operatora * b = 200
/ DivisionDivides left operand by right operandb / a = 2
% ModulusReturns the remainder after dividing the left operand by the right operand.b % a = 0
** Exponent An exponential (power) calculation is performed on the operandsa**b =10 to the power 20
//The division of operands in which the result is the quotient with the digits after the decimal point is removed. If one of the operands is negative, the result is floored, that is, it is rounded away from zero (towards negative infinity).9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
Comparison Operators:

Comparison operators compare the values of two operands and return a Boolean true or false result.

OperatorDescriptionExample
a=10 b=20
==If the values of two operands are equal, the condition is satisfied.(a == b) is not true.
!=If the values of two operands are not equal, the condition is met and returns true.(a != b) is true.
<>If the values of two operands are not equal, then the condition becomes true.(a <> b) is true. This is similar to != operator.
>If the value of the left operand exceeds the value of the right operand, the condition is satisfied.(a > b) is not true.
<If the value of the left operand is less than the value of the right operand, then the condition is met.(a < b) is true.
>=The condition is satisfied if the value of the left operand is greater than or equal to the value of the right operand.(a >= b) is not true.
<=The condition is satisfied if the value of the left operand is less than or equal to the value of the right operand.(a <= b) is true.
Assignment Operators:

The assignment operators assign the right expression’s value to the left operand.

OperatorDescriptionExample
=Values are assigned from the right operands to the left operands.c = a + b assigns value of a + b into c
+= Add ANDIt multiplies the right operand by the left operand and assigns the result to the left operand.c += a is equivalent to c = c + a
-= Subtract ANDIt takes the right operand and subtracts it from the left operand, then assigns the result to the left operand.c -= a is equivalent to c = c – a
*= Multiply ANDIt multiplies the right operand by the left operand and assigns the resulting value to the left operand.c *= a is equivalent to c = c * a
/= Divide ANDIt divides the left operand by the right operand and returns the result to the left operand.c /= a is equivalent to c = c / a
%= Modulus ANDIt performs modulus with two operands and assigns the result to the left operand.c %= a is equivalent to c = c % a
**= Exponent ANDCalculates the exponential (power) of operators and assigns a value to the left operand.c **= a is equivalent to c = c ** a
//= Floor DivisionIt divides operators by their floors and assigns a value to the left operand.c //= a is equivalent to c = c // a
Bitwise Operators:

The bitwise operators operate on the values of the two operands bit by bit.

OperatorDescriptionExample
a=60 b=13
& Binary ANDIf a bit exists in both operands, the operator copies it to the result.(a & b) (means 0000 1100)
| Binary ORIf a bit exists in either operand, the operator copies it to the result.(a | b) = 61 (means 0011 1101)
^ Binary XORIt copies the bit if it is set in one operand but not both.(a ^ b) = 49 (means 0011 0001)
~ Binary One’s ComplementIt is unary and has the effect of ‘flipping’ bits.(~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left ShiftThe value of the left operand is shifted left by the number of bits specified by the right operand.a << 2 = 240 (means 1111 0000)
>> Binary Right ShiftThe value of the left operand is shifted right by the number of bits specified by the right operand.a >> 2 = 15 (means 0000 1111)
Logical Operators:

To make a decision, logical operators are primarily used in expression evaluation.

OperatorDescriptionExample
a=10 b=10
and Logical ANDIf both operands are true, the condition is true.(a and b) is true.
or Logical ORIf either of the two operands is non-zero, the condition is satisfied.(a or b) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(a and b) is false.
Membership Operators:

The membership operators in Python look for members in a sequence, such as strings, lists, or tuples.

OperatorDescriptionExample
inIf it finds a variable in the specified sequence, it returns true; otherwise, it returns false.x in y, here in results in a 1 if x is a member of sequence y.
not inIf it does not find a variable in the specified sequence, it returns true; otherwise, it returns false.x not in y, here not in results in a 1 if x is not a member of sequence y.
Identity Operators:

The memory locations of two objects are compared using identity operators.

OperatorDescriptionExample
isIf the variables on either side of the operator point to the same object, true; otherwise, false.x is y, here is results in 1 if id(x) equals id(y).
is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwise.x is not y, here is not results in 1 if id(x) is not equal to id(y).

Note: also read about Math Functions in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago