Operators in Python
 

Operators in Python

 

An operator is a symbol that tells the Python interpreter to perform specific arithmetic or logical operations on variables and values.

For example –

# Here + and = are examples of operators in Python
sum = 5+6
print(sum)

Python has the following type of built-in operators.

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Python special operators

So in this article, we will learn about each type of operator along with some examples.

Arithmetic operators

These operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, etc. The list of arithmetic operators used in Python is given below.

OperatorDescriptionUsage
+Additiona+b
-Subtractiona-b
*Multiplicationa*b
/Divisiona/b
%Modulusa%b
**Exponentiationa**b
//Floor divisiona//b

Floor division –

This can be new to many of you when you use // operator it performs floor division. The division which results into the whole number is known as floor division. You can differentiate between normal and floor by understanding the given example.

x=18
y=5
# Normal division
print(f"x/y is {x/y}")
# Floor division
print(f"x//y is {x//y}")

You can see the output of this code in the given image –

Example of using arithmetic operators –

a=10
b=4
print(f"Addition of a and b is {a+b}")
print(f"Subtraction of a and b is {a-b}")
print(f"Multiplication of a and b is {a*b}")
print(f"Division of a and b is {a/b}")
print(f"The remainder when a is divided by b is {a%b}")
print(f"a to the power b is {a**b}")
print(f"Floor division of a and b is {a//b}")

Execute this code on your own system to see the output.

Relational operators

A relational or comparison operator is used to compare two values. The following table shows the list of relational operators used in Python.

OperatorDescriptionUsage
>Greater thana>b
<Less thana<b
==Equal (Check if two values equal or not)a==b
>=Greater than or equal toa>=b
<=Less than or equal toa<=b
!=Not equala!=b

It compares two values if the condition goes true it returns True otherwise, it returns False.

For example –

a=4
b=5
#Since a is not greater than b this will return False
print("a >b is", a>b)

See the output in the image below –

Similarly, you can use other comparison operators in Python.

Logical operators

Logical operators in Python are used to combine two conditional statements. The list of logical operators is given in the table below.

OperatorDescriptionExample
andReturns True if both the conditional statements are truea>10 and b<7
orReturns True if both the conditional statements are truea>5 or b>2
notIt returns False if condition is True and vice versanot(a>5 or b<2)

For example –

a=4
b=5
# Here both the statements are true the and operation will return True
print("a<10 and b>4 is",a<10 and b>4 )

You can see the output in the given image –

Bitwise operators

The bitwise operators work on bits these perform operation bit by bit. The following table shows the list of bitwise operators that are used in Python.

OperatorDescriptionUsage
&ANDa&b
|ORa|b
~NOT~a
^XORa^b
>>Right Shifta>>2
<<Left Shifta<<2

For example-

Lets a=4 and b=5  that means in 4-bit binary a will be 0100 and b will be 0101

         b          a&b

0          0           0

1          1           1

0          0           0

0          1           0

After performing bitwise and operation i.e. a&b will be 4 you can see the output in the given image-

Assignment operators

Assignment operators are used to assigning a value to a variable. Equal to i.e. = is a simple assignment operator we can combine this with other types of operators to calculate and assign a value to a variable. For example, + = will add and assign the calculated value to the variable.

The given table shows the list of assignment operators used in Python.

OperatorsExampleExample is same asDescription
=x=10x=10Assign 10 to variable x
+=x+=10x=x+10Add and assign to variable x
-=x-=10x=x-10Subtract and assign to variable x
*=x*=10x=x*10Multiply and assign to variable x
/=x/=10x=x/10Divide and assign to variable x
%=x%=10x=x%10Find remainder and assign to x
//=x//=10x=x//10Divide and assign a whole number to x (remove fractional part)
**=x**=10x=x**10Find x to the power 10 and assign to the variable x
&=x&=10x=x&10Perform bitwise AND operation and assign the value to variable x
|=x|=10x=x|10Perform bitwise OR operation and assign the value to variable x
^=x^=10x=x^10Perform bitwise XOR operation and assign the value to variable x
>>=x>>=10x=x>>10Shift value 10 bit right and assign it to variable x
<<=x<<10x=x<<10Shift value 10 bit left and assign it to variable x

Python special operators

Python has some special operators i.e. identity and membership operators.

Identity operators

Identity operators check if the two values or variables are equal and at the same location in memory. There are two identity operators, you can see them in the given table.

OperatorDescriptionUsage
isReturn True if both the values are identicala is b
is notReturn True if both the values are not identicala is not b

For example –

a=5
b=5
c=[6,7,8]
d=c
# b is identical to a so the given statement will be True
print("b is a is",b is a)
# c is identical to d so the given statement will be False
print("c is not d", c is not d)

You can see the output in the image below –

Membership operators

You can use membership operators to test whether a value or variable is in the sequence or not. You can see membership operators in the given table.

OperatorDescriptionUsage
inReturn True if value found in the sequencea in b
in notReturns True if value not found in the sequencea not in b
a="Hello World!"
b="World"
c=[6,7,8]
d=[10]
# b is in a so the given statement will be True
print("b in a is",b in a)
# d is not in c so the given statement will also return True
print("d not in c", d not in c)

You can see the output in the given image –

Operator precedence and associativity

An expression in programming is the combination of operators and operands. The 5+2*3 is an example of a valid Python expression. Python interpreter performs the calculation on the basis of operator precedence and associativity.

For example in the expression 5+2*3 Python interpreter will first perform multiplication and later it will perform addition. That means * operator has higher precedence over +.

The following table shows the operator precedence and associativity. It is in descending order that means upper groups have higher precedence than lower ones.

OperatorsDescription
()Parentheses
**Exponentiation(raise to the power)
+, -, ~Unary plus, Unary minus, Bitwise NOT
*, /,//,%Multiplication, Division, Floor division, Modulus
+, -Addition, Subtraction
<<,>>Left and right shift
&Bitwise AND
^Bitwise XOR
|Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not inComparisons, Identity, Membership operators
notLogical NOT
andLogical AND
orLogical OR

Now which operator will be calculated first if they fall under the same group? Here associativity helps to determine the order of operations. So almost all the groups have left to right associativity that means the operator on the left side will be calculated first.

Conclusion

For better understanding try to evaluate Python expressions on your own and then try to find them with code. Now If you find any difficulty then write us in the comments below.

Previous Keywords In Python
Next Taking Input From Users In Python
 

About author

You might also like

Programming 0 Comments

Writing Hello World! Program In Python

In the last few articles, we have installed python and set up VS code IDE on Linux and Windows for development using Python. Today in this tutorial we will write

Programming 0 Comments

Introduction To The Course On Python Programming

Python is a high-level, interpreted, general-purpose programming language. It emphasizes more on code readability so its syntax is simple and easy to use. People from different areas use it for

Programming 0 Comments

Python Boolean Data Type

Boolean or bool is a built-in data type in the python programming language. It can have two possible values –   True  False These are equivalent to 1 and 0

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply