Python Operators

Python Operators – Learn Python with me

What is an Operator?

When we store the values in variables, we intend to perform some operations on these values. The operations could be addition, subtraction, division, etc., but you would need the operators to achieve the same in any programming language. For example, if you want to add the values stored in the variables a and b, you’d need to include a+b in the code. Here, ‘+’ is an additional arithmetic operator, and it is responsible for letting the computer know that the program under execution requires an additional operation. Besides ‘+’, we also have several other symbols that are used as operators in Python. Let us now dive deep into all the kinds of Python Operators.

Python Operators

Listed below are the kinds of Python operators that you can use in your Python programs.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Unary Minus Operator
  4. Relational Operators
  5. Logical Operators
  6. Boolean Operators
  7. Bitwise Operators
  8. Membership Operators
  9. Identity Operators

Points to Note:

If any operator is used for single variable, it is called unary operator.

If any operator is used for two variables, it is called binary operator.

If any operator is used for three variables, it is called ternary operator.

Let us now look at each kind of operator in Python

1. Arithmetic Operators

Arithmetic operators are used in Python programs to perform mathematical operations such as addition, subtraction, division, etc. Python provides you with seven arithmetic operators, and please find the list below.

OperatorDescriptionExampleResult for values p=13 and q=5
+Used for additionp+q18
Used for subtractionp-q8
*Used for multiplicationp*q65
/Used for divisionp/q2.6
%Used to obtain the remainder of the divisionp%q3
**Calculates the exponential power value. p**q means p to the power qp**q371293
//Integer Division or Floor Division – Used to obtain integer quotient after performing divisionp//q2

When dealing with the arithmetic operators, you should also know the rules on how to use them if several arithmetic operators are included in an expression. In such cases, please follow the order as mentioned below.

  1. You should evaluate the parentheses first
  2. Exponentiation is carried out in the next step
  3. Multiplication, Division, modulus and floor division share equal priority
  4. Addition and Subtraction are carried out in the next step
  5. Assignment operation is finally carried out

For example, let us consider the expression j=(p+q)*r**f//g+h with the values of p=1,q=2,r=3; f=2,g=2,h=3. In this case, this expression will be evaluated as mentioned below

j=(1+2)*3**2//2+3

  1. Evaluation of parentheses. j=3*3**2//2+3
  2. Exponentiation is carried out in the next step. j=3*9//2+3
  3. Multiplication, Division, modulus and floor division share equal priority. j=27//2+3 and j=13+3
  4. Addition and subtraction are carried out in the next step. j=16
  5. Assignment is eventually carried out. It means that the value 16 gets stored into the variable j.

You can also enter the examples mentioned above in the python interpreter, and it will display the results for each expression by acting as a calculator.

2. Assignment Operators

Just as the name sounds, these operators are used to assign value to the variables. Usually, the assignment happens from the right side value to a left side variable. You can also use the assignment operators in conjunction with arithmetic operations such as Subtraction, addition etc.

Let us consider the variables with values as a=20, b=10, and c=5 and look at the table below with the examples to understand how the assignment operators work.

OperatorIllustrationDescriptionOutcome
=c=a+bAssignment Operator. It saves the values from the right side to the variable on the left sidec=30
+=c+=aAddition assignment operator. This is primarily considered as c=c+a. The result of c+a will be stored into cc=25
-=c-=aSubtraction assignment operator. This is primarily considered as c=c-a. The result of c-a will be stored into cc=-15
*=c*=aMultiplication assignment operator. This is primarily considered as c=c*a. The result of c*a will be stored into cc=100
/=c/=aDivision assignment operator. This is primarily considered as c=c/a. The result of c/a will be stored into c.c=0.25
%=c%=aModulus assignment operator. This is primarily considered as c=c%a. The result of c%a will be stored into c. (Stores the remainder)c=5
**=c**=bExponentiation assignment operator. This is primarily considered as c=c**b. It calculates the power value of c to the power b and finally stores the value in cc=9765625
//=c//=bFloor division assignment operator. This is primarily considered as c=c//b, and the final result will be stored into cc=0

3. Unary Minus Operator

The Unary Minus operator is represented by the symbol minus (-) in Python. If you use this operator before any variable, the value stored in the value gets negated. It means that if the variable holds a positive value, using this Unary minus operator, you can convert the same value to negative. We can also use this to convert the negative values to positive values. Let us understand more about it using the example below.

a=5 #stores the value 5 into a variable
print(-a) # displays output as -5
b=-5
b=-b
print(b) #displays 5
Output

>>> a=5 #stores the value 5 into a variable
>>> print(-a) # displays output as -5
-5
>>> b=-5
>>> b=-b
>>> print(b) #displays 5
5

4. Relational Operators

Using the relational operators in Python, you can compare two variables and determine which one is bigger or smaller and perform several such operations on them. The outcome of these operators is often returned in Boolean values of True or False. Let us consider the variables with values as p=1 and q=2, and look at the table below with the examples to understand how the relational operators work.

OperatorIllustrationDescriptionOutcome
>p>qUsing this “greater than” operator, you can determine if p is greater than q or not.False
>=p>=qUsing this “greater than or equal to” operator, you can determine if p is greater than or equal to q or not.False
<p<qUsing this “less than” operator, you can determine if p is lesser than q or not.True
<=p<=qUsing this “less than or equal to” operator, you can determine if p is lesser than or equal to q or not.True
==p==qUsing this “Equals operator”, you can determine if the value stored in the p variable is equal to the value stored in the variable qFalse
!=p!=qUsing this “Not equals operator”, you can determine if the value stored in the p variable is not equal to the value stored in the variable qTrue

We use the relational operators often in the condition statements of the program, as shown in the example below.

p=1
 q=2
 if(p<q):
     print("Yes")
else:
     print("No")

In the example program mentioned above, we assigned the values 1 and 2 to the p and q variables. We used the less than operator to compare if the p variable is lesser than q. As per the logic mentioned above, the output will be displayed as Yes since the first block of the print statement will be executed as it satisfies the if condition. If you are confused about the if condition mentioned above, do not worry, we will have a detailed look at them in our future chapters.

Output

>>> p=1
>>> q=2
>>> if(p<q):
...     print("Yes")
... else:
...     print("No")
...
Yes

5. Logical Operators

We use logical operators to create compound conditions. It is a mixture of more than one simple condition for those who don’t know about compound conditions. Every simple condition in a compound is evaluated to either True or False, and then eventually, the whole compound statement is considered to be either True or False. Let us understand it better using the example below.

a=1
b=2
c=3
if(a<b and b<c):
     print("Yes")
else:
     print("No")
Output

>>> a=1
>>> b=2
>>> c=3
>>> if(a<b and b<c):
...     print("Yes")
... else:
...     print("No")
...
Yes

In the example mentioned above, we have assigned the values 1, 2, and 3 to a, b, and c variables, respectively. In the fourth line of code, you can see a compound condition that includes two simple conditions of a<b and b<c. Since a is less than b, it gets evaluated as True, and since b is less than c, it also gets evaluated as True. Eventually, the whole compound condition considers the two results of True and True from the simple individual conditions and finally evaluates the complete compound condition to True.

In the above program, “and” is a logical operator; we have two more logical operators in Python that we can use in any compound statement. Please find below the complete list of Python logical operators.

OperatorIllustrationDescription
Anda and bAnd Operator
Ora or bOr Operator
Notnot aNot Operator

6. Boolean Operators

In our previous chapters, we have already discussed that Python contains only two kinds of boolean values: True and False. In a way, you can view the logical operators also as boolean operators. Let’s consider a=True, b= False and understand more about this operator using the examples below.

OperatorIllustrationDescriptionOutcome
Anda and bUsing this boolean AND operator, if both a and b are true, you will get the result as true. If at least one of the variables is false, the result will also be false.False
Ora or bUsing this boolean OR operator, if at least one of the variables is true, the result will also be true.True
Notnot aUsing this boolean NOT operator, you can convert the true to false and false to true. Here in our example, since the variable a is true, the result will be false.False

7. Bitwise Operators

Bitwise operators are used for performing operations on binary values (0 or 1). You can use them directly on the integers or the binary values. If they are used on integers, the integer values are first converted to binary, and then the bitwise operations are performed on them.

Bitwise Operators

Bitwise Operators Examples

Bitwise Operator Example

8. Membership Operators

Membership Operators in Python are useful for testing whether a sequence like dictionaries, tuples, lists, and strings are available in an object. You can use these operators to know if an element is available in a sequence or not. Listed below are the kinds of membership operators.

  • in – The “in” operator returns true if the desired element is discovered in a sequence.
  • not in – The “not in” operator returns true if the desired element is not found in a sequence.
 cities=["Hyderabad","Chennai","Delhi","Mumbai"] #declares a list cities
 print("Hyderabad" in cities) #returns True in the results since Hyderabad is present in the declared list, cities
 print("Lucknow" in cities) #returns False in the results since Lucknow is not present in the declared list, cities
 print ("Bangalore" not in cities) #returns True in the results since Bangalore is not present in the declared list, cities
 print("Hyderabad" not in cities) # returns False in the results since Hyderabad is present in the declared list, cities
Output

>>> cities=["Hyderabad","Chennai","Delhi","Mumbai"]
>>> print("Hyderabad" in cities)
True
>>> print("Lucknow" in cities)
False
>>> print ("Bangalore" not in cities)
True
>>> print("Hyderabad" not in cities)
False

9. Identity Operators

Identity Operators in Python are used to compare the memory location of two objects. We have two kinds of Identity operators, as listed below.

  • is – it returns true if both objects are same
  • is not – it returns true if both objects are not same.
>>> p=25
>>> q=25
>>> if p is q:
...     print("both p and q share same identity")
... else:
...     print("p and q have different identities")
...
both p and q share same identity
>>> if p is not q:
...     print("p and q share different identities")
... else:
...     print("p and q share same identity")
...
p and q share same identity
>>>
Output

>>> p=25
>>> q=25
>>> if p is q:
...     print("both p and q share same identity")
... else:
...     print("p and q have different identities")
...
both p and q share same identity
>>> if p is not q:
...     print("p and q share different identities")
... else:
...     print("p and q share same identity")
...
p and q share same identity
>>>

In this post, we learned about all the Python operators, and they will help you perform operations in Python programs. Would you please not hesitate to reach us through the comments or the contact section if you have any queries? We would love to hear from you.

Scroll to Top