In this lesson of **Python Programming Language**, we will learn Basic Operators in Python Programming Language their types and how we can use them in our code.

**In This Python Lesson You’ll Learn:**

Now we will discuss each of the above topics in detail to learn to understand them easily. I am sure after going through this lesson you will be able to implement these python operators in your code easily.

## Python Basic Operators

Python operators are special symbols dedicated to performing specific tasks in a programming language. These operators consist of two parts one is the **operator symbol** itself and the other is **operand** and the final result is called output. See the illustration below.

1 |
20 * 20 = 400 |

In the above illustration, both side 20 and 20 are called the operands and the symbol of multiplication( * ) is called an operator and the final result which is 400 is called output.

Now we will discuss each of the above operators in detail.

## Python Arithmetic Operators

These operators are used to perform the operation like addition, subtraction, multiplication, and division. See the table below to understand more easily.

Symbol of Arithmetic Operators | Name and Its Functions | Demo Illustration |
---|---|---|

+ | Addition: It uses to add two values. | 10 + 10 = 20 |

- | Subtraction: It uses to subtract one values from another value. | 10 - 5 = 5 |

* | Multiplication: It uses to multiply values with each other. | 5 * 5 = 25 |

/ | Division: It always perform division from left operand to right one. | 10 / 5 = 2 |

% | Modulus: It divides the left operand with right operand and return remainder in the output. | 12 / 5 = 2.4 |

** | Exponent: It multiply the right operand to the power of left operand. | 4**3 = 32 |

## Example Of Python Arithmetic Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# Examples of Arithmetic Operator x = 10 y = 20 # Python Addition of numbers addition = x + y # Python Subtraction of numbers subtraction = x - y # Python Multiplication of number multiplication = x * y # Python Division(float) of number division = x / y # Python Division(floor) of number division2 = x // y # Python Modulo of both number modulus = x % y # Output of the above results print(addition) print(subtraction) print(multiplication) print(division) print(division2) print(modulus) |

1 2 3 4 5 6 |
30 -10 200 10 |

## Python Comparison Operators

These operators are used to compare the values and it gives us result back to us in the form of True or False. These operators are also called “**Relational Operators**“. See the table below to understand more easily.

Symbol of Comparison Operators | Name and Its Functions | Demo Illustration |
---|---|---|

!ERROR! unexpected operator '=' | Equal To: Return True if values on both sides are equal otherwise return False. | a == b |

!= | Not equal to: Return True if values on both sides are not equal otherwise return False | a !=b |

<> | Not equal to: Work same like the above not equal operator | (a <> b ) work same like a != b |

> | Greater than: If the value on the left side is bigger than the value on the right side then it return True Otherwise return False. | a > b |

< | Less Than: If the value on the left side is smaller than the value on the right side it returns True otherwise False. | a < b |

>= | Greater Than Or Equal To: If the value on the left side is greater or equal to the value on the right side it returns True otherwise returns False. | a >= b |

<= | Smaller Than Or Equal To: If the value on the left side is smaller or equal to the value on the right side it returns True otherwise returns False. | a <= b |

## Example Of Python Comparison Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
a = 15 b = 12 if ( a == b ): print "This Shows - a is equal to b" else: print "This Shows - a is not equal to b" if ( a != b ): print "This Shows - a is not equal to b" else: print "This Shows - a is equal to b" if ( a <> b ): print "This Shows - a is not equal to b" else: print "This Shows - a is equal to b" if ( a < b ): print "This Shows - a is less than b" else: print "This Shows - a is not less than b" if ( a > b ): print "This Shows - a is greater than b" else: print "This Shows - a is not greater than b" if ( a <= b ): print "This Shows - a is either less than or equal to b" else: print "This Shows - a is neither less than nor equal to b" if ( b >= a ): print "This Shows - b is either greater than or equal to b" else: print "This Shows - b is neither greater than nor equal to b" |

1 2 3 4 5 6 7 |
This Shows - a is not equal to b This Shows - a is not equal to b This Shows - a is not equal to b This Shows - a is not less than b This Shows - a is greater than b This Shows - a is neither less than nor equal to b This Shows - b is neither greater than nor equal to b |

## Python Assignment Operators

When you need to assign the value of one operand to other operand or variable you need to use assignment operators. There are different types of assignment operators. See the table below to understand more easily.

Symbol of Assignment Operators | Name and Its Functions | Demo Illustration |
---|---|---|

= | Assignment: It assigns the value of right operand to the left operand. | z = x + y It assigns the value of ( x + y ) to z |

+= | Add AND: It first add value of right operand to the left operand and then assign added value to the operand left side of the assignment operator. | z+= x is equal to z = z + x |

-= | Subtract AND: It first subtract the value of right operand from left operand and then assign subtracted value to the operand left side of the assignment operator. | z-= x is equal to z = z - x |

*= | Multiply AND: It first multiply right operand with left operand and assigns the multiplied value to the operand left side of the assignment operator. | z*= x is equal to z = z * x |

/= | Divide AND: It divides the left operand with the right operand and assigns the divided value to the operand left side of the assignment operator. | z/= x is equal to z = z / x |

%= | Modulus AND: It returns the remainder after division of two operands. | z %= x is equal to z = z % x |

**= | Exponent AND: It performs exponential power calculation and store the result to operand left side of the assignment operator. | z**= x is equal to z = z** x |

//= | Floor Division: This operators do floor division on the operands and assigns value to the left operand. | z //= x is equivalent to z = z // x |

## Example Of Python Assignment Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
x = 10 y = 12 z = z = x + y print "This Shows - Value of c is ", z z += x print "This Shows that the Value of c is ", z z *= x print "This Shows that the Value of c is ", z z /= x print "This Shows that the Value of c is ", z z = 2 z %= x print "This Shows that the Value of c is ", z z **= x print "This Shows that the Value of c is ", z z //= x print "This Shows that the Value of c is ", z |

1 2 3 4 5 6 7 |
This Shows that the Value of c is 22 This Shows that the Value of c is 32 This Shows that the Value of c is 320 This Shows that the Value of c is 32 This Shows that the Value of c is 2 This Shows that the Value of c is 1024 This Shows that the Value of c is 102 |

## Python Logical Operators

Python Programming Language supports some of the logical operators which are as follows:

Logical Operators | Functions | Demo Illustration |
---|---|---|

and Logical AND | This operator returns True if only both the operands are true. | ( x and y ) is True |

or Logical OR | This operator returns True if any one of the both operands are non-zero then it becomes true. | ( x or y ) is True |

not Logical NOT | This operator is use to reverse the current state of the operands. | Not(a and b) is false |

## Example Of Python Logical Operators

1 2 3 4 5 6 7 8 9 10 11 |
x = True y = False # Output: x and y is False print('x and b is',x and y) # Output: x or y is True print('x or y is',x or y) # Output: not x is False print('not x is',not x) |

1 2 3 |
('x and y is', False) ('x or y is', True) ('not x is', False) |

## Python Bitwise Operators

Another type of operators that work in Python Programming Language is called Bitwise Operators. They work on bits and perform their actions in a bit by bit sequence. We will take some values for better understanding.

Let’s assume **x = 10** which is 0000 1010 in binary and **y = 4** which is 0000 0100

Now we do calculation using Python Bitwise Operators.

x = 0000 1010

y = 0000 0100

——————-

Symbol of Bitwise Operators | Name and Its Functions | Demo Illustration |
---|---|---|

& | Binary AND: It gets the bit to the results if it exists in both operands. | x& y = 0 (0000 0000) |

| | Binary OR: It gets the bit in the result if it exists in either operands | x | y = 14 (0000 1110) |

~ | Binary NOT: It's function is to reverse the value. It is also called Binary one's complement | ~x = -11 (1111 0101) |

^ | Binary XOR: It gets the bit if it is available in one operand but not both. | x ^ y = 14 (0000 1110) |

>> | Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand. | x>> 2 = 2 (0000 0010) |

<< | Binary Left Shift: It's main function is that The left operands value is moved left by the number of bits specified by the right operand. | x<< 2 = 40 (0010 1000) |

## Python Membership Operators

In Python Programming Language membership operators are used to checking whether a value or variable present in a sequence in lists, strings or in tuples. There are two membership operators which are as follows:

Symbol of Membership Operators | Functions | Demo Illustration |
---|---|---|

in | It evaluates and If a value of variable found in a sequence it is True otherwise False. | 10 in x |

not in | It evaluates and check If a value of variable not found in a sequence it is True otherwise False. | 10 not in x |

## Example of Python Membership Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
x = 5 y = 10 list = [1, 2, 3, 4, 5 ]; if ( x in list ): print "This shows that x is available in the given list with sequence" else: print "This shows that x is not available in the given list with sequence" if ( y not in list ): print "This shows that y is not available in the given list with sequence" else: print "This shows that y is available in the given list with sequence" x = 2 if ( x in list ): print "This shows that x is available in the given list with sequence" else: print "This shows that x is not available in the given list with sequence" |

1 2 3 |
This shows that x is available in the given list with sequence This shows that y is not available in the given list with sequence This shows that x is available in the given list with sequence |

## Python Identity Operators

The Python Identity Operators are used in the case when we want to find out if two values or variable placed on the same memory address or location. Remember if two values of the variable are equal it does not mean they are identical.

Symbol | Function | Demo Illustration |
---|---|---|

is | It returns True if the operands are identical. | x is y here it will true is x is identical to y |

is not | It returns True if the operands are not identical | x is not y here it will true is x is not identical to y |

## Example of Python Identity Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
a1 = 7 b1 = 7 a2 = 'Hello' b2 = 'Hello' a3 = [1,2,3] b3 = [1,2,3] # Output Check: It is False print(a1 is not b1) # Output Check: It is True print(a2 is b2) # Output Check: It is also False print(a3 is b3) |

1 2 3 |
False True False |

What IDE should I use to type python?

You should use Atom IDE because it’s easy to understand.