BASIC OPERATORS IN PYTHON


There are several basic operators in Python since it is built on logic, and a number of operations can be applied . Python operators are divided into eight categories that you can use to do certain operations:


Basic Operator: Arithmetic Operator

Just as the name goes, arithmetic operators are used to do simple calculations like addition, subtraction, division, modulo, exponential power and multiplication
basic operators in Python

Operator Function Example
+ Add two numbers or operands x + y
Subtract two operands x – y
* Multiply two operands x * y
/ Divide two operands x / y
// Divide(floor) two operands x / y
% Modulus returns the remainder of the operands divided by each other x % y
** Creates exponential power of the first operand x ** y
#Taking two numbers a and b
a = 3
b = 5
  
# Addition
addition = a + b 
# Subtraction
subtraction = a - b 
# Division
division = a / b
# Multiplication  
multiplication = a * b 
# Exponential Power
exponential = a**b
# Modulo
modulo = a % b 
  
# Printing the variables 
print(addition) 
print(subtraction) 
print(multiplication) 
print(division) 
print(exponential) 
print(modulo)

Output

8
-2
15
0.6
243
3

Basic Operator: Comparison Operator

Comparison operators are used to compare values and return the result by checking the nature of the output as True or False

Operator Function Example
== equals to sign, returns true when both the operands are equal to each other x == y
> Greater than sign, turns true when the left operand is greater than the right operand x > y
< Less than sign, turns true when the left operand is less than the right operand x < y
>= Greater than or equals to sign, turns true when the left operand is greater or equals to the right operand x >= y
<= Less than or equals to sign, turns true when the left operand is less than or equals to the right operand x <= y
!= Not equals to sign, turns true when both the operands aren't equal to each other x != y
# Comparsion operators 
x = 10
y = 4
  
# When x > y
print(x > y) 
  
# When x < y 
print(x < y) 
  
# When x == y
print(x == y) 
  
# when x != y 
print(x != y) 
  
# when x >= y
print(x >= y)

#when x <= y
print(x <= y)

Output

True
False
False
True
True
False




Assignment Operator

Assignment operators are used to assign values (operands) to the variables.  There are a lot of assignment operators used in python:

Operator Function Example
= Equals to, sets the variable equals to the operand. x = 5
+= Adds the right operand value with the left operand value and then assigns it to the left operand. x += y
x = x + y
-= Subtracts the right operand value from the left operand value and then assigns to the left operand. x -= y
x = x - y
*= Multiplies the right operand value with the left operand value and then assigns it to the left operand. x *= y
x = x * y
/= Divides the right operand value with the left operand value and then assigns it to the left operand. x /= y
x = x / y
//= Divides (floor) the right operand value with the left operand value and then assigns it to the left operand. x //= y
x = x // y
%= Takes the remainder of the right operand value from the left operand value and then assigns it to the left operand. x %= y
x = x % y
**= Calculate exponent(raise power) of left value with right operand and assign value to left operand x **= y
x = x ** y

Logical Operator

Logical operators perform AND, NOT and OR functions between operands

Operator Function Example
and Turns true if both the values are true x and y
or Turns true if one or both of the operands is true x or y
not Turns true if the operand is false not x

Bitwise Operator

As the name goes by, bitwise operators perform operations bit by bit.

Operator Function Example
& AND, sets the bit to one if both the operands are one x & y
| OR, sets the bit to one if one or both the operands are one x | y
^ XOR, sets the bit to one only if one of the 2 operands is one, if numbers are identical then there is going to be a zero x ^ y
~ Inverts all the bits and prints the compliment of the number ~x
<< Shift the bits on the left side by adding number of zeros of the right operand x << y
>> Shift the bits on the right side by adding number of zeros of the left operand x >> y

Special Operators

  • Identity operators: is and is not are the used to see if two operands are present on the same memory location or not
Operator Function Example
is Turns true if two operands are identical x is y
is not Turns true if two operands aren't identical x is not y




Membership Operators

Member Operators are used to check whether a value or operand is present in a sequence or not.

Reference here

Operator Function Example
in Turns true if operand is found in the sequence x in y
not in Turns true if operand is not found in the sequence x not in y