Python Operators

An operator , in software programing, is a symbol that usually represents an action or process, as for example "+" is an arithmetic operator that represents addition. These symbols were adapted from mathematics and logic. Programming languages typically support a set of operators. These operators are the backbone of any program and they are used for everything from very simple functions like counting to complex algorithms like security encryption.
How to Use the Python or Operators
In Python, operators are special symbols that designate that some sort of computation should be performed. Python includes operators in the following categories:
  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Boolean Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity operators

Python Arithmetic Operators

Arithmetic operators are used in mathematical expressions . These operators take numerical values (either literals or variables) as their operands and return a single numerical value.
Operator Description Syntax
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

Python Addition (+) Operator

The addition is taking two or more numbers and adding them together, that is, it is the total sum of 2 or more numbers.

>>>5+4 9

Python Subtraction (-) Operator

Subtracts the value on the right from the one on the left.

>>>5-4 1

Python Multiplication (*) Operator

Multiplies the values on either side of the operator.

>>>5*4 20

Python Division (/) Operator

The division is a method of distributing a group of things into equal parts.

>>>5/4 1.25

Python Modulus (%) Operator

The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.

>>>5%4 1

Python Floor division (//) Operator

Divides and returns the integer value of the quotient. It dumps the digits after the decimal.

>>>5//4 1

Python Exponentiation (**) Operator

Raises the first number to the power of the second.

>>>5%%4 625

Python Assignment Operators

An assignment operator assigns a value to its left operand based on the value of its right operand . These operators do not produce values. The value of an assignment expression is the value assigned.
Operator Description Syntax
= x = 1 x = 1
+= x += 1 x = x + 1
-= x -= 1 x = x - 1
*= x *= 1 x = x * 1
/= x /= 1 x = x / 1
%= x %= 1 x = x % 1
//= x //= 1 x = x // 1
**= x **= 1 x = x ** 1
&= x &= 1 x = x & 1
|= x |= 1 x = x | 1
^= x ^= 1 x = x ^ 1
>>= x >>= 1 x = x >> 1
<<= x <<= 1 x = x << 1

Python Comparison Operators

Comparison operators, as their name implies, allow you to compare two values . These operators compare the contents in a field to either the contents in another field or a constant . They may be used alone or in combination with other operators and functions in both record expressions and target field expressions .
Operator Description Syntax
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Python Equal to (==) Operator

If the values of two operands are equal, then the condition becomes true.

>>> 5==5 True
>>> 5==5.5 False
>>> 1==True True
>>> 0==False True

Python Not equal to (!=) Operator

If values of two operands are not equal, then condition becomes true.

>>> 5!=6 True
>>> 5!=5.0 False

Python Greater than (>) Operator

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

>>> 5>2 True
>>> 5>7 False

Python Less than (<) Operator

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

>>> 5<7 True
>>> 5<2 False

Python Greater than or equal to (>=) Operator

If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.

>>> 5>=5 True
>>> 5>=6 False

Python Less than or equal to (<=) Operator

If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

>>> 5<=6 True
>>> 5<=4 False

Python Logical Boolean Operators

Boolean Logic is a form of algebra which is centred around three simple words known as Boolean Operators: "Or" , "And" , and "Not" . At the heart of Boolean Logic is the idea that all values are either true or false . When they are, they return a Boolean value
Operator Description Syntax
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false not x

Python Bitwise Operators

Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal , or octal numbers. It works efficiently and are used to perform bitwise operations on binary patterns. All the binary operators are in-fix except for the not operator. Typically, bitwise operations are substantially faster than division , several times faster than multiplication, and sometimes significantly faster than addition .
Operator Description Syntax
& Bitwise AND x & y
| Bitwise OR x | y
^ Bitwise NOT x ^ y
~ Bitwise XOR ~x
<< Bitwise right shift x>>
>> Bitwise left shift x<<


Operators and Expressions in Python

Python Membership Operators

Membership operators are operators used to validate the membership of a value . This operator is used to test memberships in variables such as strings, integers as well as tuples. These operator returns either True or False , if a value/variable found in the list, its returns True otherwise it returns False.
Operator Description Syntax
in True if value is found in the sequence x in y
not in True if value is not found in the sequence x not in y

Python Identity operators

Identity operators are used to compare the objects , not if they are equal, but if they are actually the same object, with the same memory location . They are usually used to determine the type of data a certain variable contains .
Operator Description Syntax
is True if the operands are identical x is y
is not True if the operands are not identical x is not y