Operators in Python

Operators in Python

Operators in Python

Operators are constructs you use to manipulate the data such that you can conclude some sort of solution to us. A simple example would be that if there were 2 friends having 70 rupees each, and you wanted to know the total they each had, you would add the money. In Python, you use the + operator to add the values which would sum up to 140, which is the solution to the problem.

Python has a list of operators which can be grouped as :

1. Arithmetic Operators

Logical Operators Identity Operators

2. Assignment Operators

3. Comparison Operators

4. Bitwise Operators

5. Logical Operators

6. Membership Operators

7. Identity Operators

Let us move ahead and understand each of these operators carefully.

Note: Variables are called operands that come on the left and right of the operator. Ex :

1
2
3
a=10
b=20
a+b

Here ‘a’ and ‘b’ are the operands and + is the operator.

1. Arithmetic Operator

They are used to perform arithmetic operations on data.

Operator Description
+ Adds the values of the operands
Subtracts the value of the right operator with the left operator
* Multiples left operand with the right operand
/ Divides the left operand with the right operand
% Divides the left operand with the right operand and returns the remainder
** Performs the exponential of the left operand with the right operand

The code snippet below will help you understand it better.

1
2
3
a = 2
b = 3
print(a+b, a-b, a*b, a/b, a%b, a**b, end=',')

Output : 5, -1, 6, 0.6666666666666666, 2, 8

Once you have understood what the arithmetic operators are in Python Basics, let us move to assignment operators.

2. Assignment Operators

As the name suggests, these are used to assign values to the variables. Simple as that.

The various assignment operators are :

Operator Description
= It is used to assign the value on the right to the variable on the left
+= Notation for assigning the value of the addition of the left and right operand to the left operand.
-= Notation for assigning the value of the difference of the left and right operand to the left operand.
*= Short-hand notation for assigning the value of the product of the left and right operand to the left operand.
/= Short-hand notation for assigning the value of the division of the left and right operand to the left operand.
%= Short-hand notation for assigning the value of the remainder of the left and right operand to the left operand.
**= Short-hand notation for assigning the value of exponential of the left and right operand to the left operand.

3. Comparison Operators

These operators are used to bring out the relationship between the left and right operands and derive a solution that you would need. It is as simple as to say that you use them for comparison purposes. The output obtained by these operators will be either true or false depending if the condition is true or not for those values.

Operator Description
== Find out whether the left and right operands are equal in value or not
!= Find out whether the values of left and right operators are not equal
< Find out whether the value of the right operand is greater than that of the left operand
> Find out whether the value of the left operand is greater than that of the right operand
<= Find out whether the value of the right operand is greater than or equal to that of the left operand
>= Find out whether the value of the left operand is greater than or equal to that of the right operand

You can see the working of them in the example below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = 21
b = 10
if a == b:
    print ( 'a is equal to b' )
if a != b
    print ( 'a is not equal to b' )
if a < b:
    print ( 'a is less than b' )
if a > b:
    print ( 'a is greater than b' )
if a <= b:
    print ( 'a is either less than or equal to b' )
if a >= b:
    print ( 'a is either greater than or equal to b' )

Output :
a is not equal to b
a is greater than b
a is either greater than or equal to b

Let us move ahead with the bitwise operators in the Python Basics.

4. Bitwise Operators

To understand these operators, you need to understand the theory of bits. These operators are used to directly manipulate the bits.

Operator Description
& Used to do the AND operation on individual bits of the left and right operands
| Used to do the OR operation on individual bits of the left and right operands
^ Used to do the XOR operation on individual bits of the left and right operands
~ Used to do the 1’s compliment operation on individual bits of the left and right operands
<< Used to shift the left operand by right operand times. One left shift is equivalent to multiplying by 2.
>> Used to shift the left operand by right operand times. One right shift is equivalent to dividing by 2.

It would be better to practice this by yourself on a computer. Moving ahead with logical operators in Python Basics.

5. Logical Operators

These are used to obtain a certain logic from the operands. We have 3 operands.

  • and (True if both left and right operands are true)
  • or (True if either one operand is true)
  • not (Gives the opposite of the operand passed)
1
2
3
a = True
b = False
print(a and b, a or b, not a)

Output: False True False

Moving over to membership operators in Python Basics.

6. Membership Operators

These are used to test whether a particular variable or value exists in either a list, dictionary, tuple, set and so on.

The operators are :

  • in (True if the value or variable is found in the sequence)
  • not in (True if the value is not found in the sequence)
1
2
3
4
5
a = [1, 2, 3, 4]
if 5 in a:
    print('Yes!')
if 5 not in a:
    print('No!')

Output: No!

Let us jump ahead to identity operators in Python Basics.

7. Identity Operator

These operators are used to check whether the values, variables are identical or not. As simple as that.

The operators are :

  • is (True if they are identical)
  • is not (True if they are not identical)
1
2
3
4
5
6
a = 5
b = 5
if a is b:
print('Similar')
if a is not b:
print('Not Similar!')

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Variables in Python – Constant, Global & Static Variables

Namespacing and Scopes in Python

Data Types in Python

STRING Data Type in Python

LIST Data Structure in PYTHON

TUPLE Data Structure in PYTHON

Differences between List and Tuple

SET Data Structure in PYTHON

DICTIONARY Data Structure in PYTHON

Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python Random module

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers