DEV Community

Cover image for Python Basics 3: Operators
Irfan Faisal
Irfan Faisal

Posted on

Python Basics 3: Operators

Operators are one of the most utilized elements of any programming language. We have to use operators in Python for numerous cases. From mathematical calculations to building any project, operators are inevitable. As usual, operators have classifications.

Arithmetic Operators:
1.Addition(+): Add values on either side of the operator

x=2
y=5
print(x+y)
#output:7
Enter fullscreen mode Exit fullscreen mode

2.Subtraction(-): Subtracts right hand operand from left operand

x=5
y=2
print(x-y) 
>>> 3
Enter fullscreen mode Exit fullscreen mode

3.Multiplication(*): Multiplies on either side of the operator

x=10
y=5
print(x*y) 
>>> 50
Enter fullscreen mode Exit fullscreen mode

4.Division(/): Divides left-hand operand by right-hand operand. The result of division is always a float value.

x= 20
y= 5
print(x/y) 
>>> 4
Enter fullscreen mode Exit fullscreen mode

5.Floor Division(//): It returns floor value for both integer and floating point arguments. Floor value effectively rounds down a real nummber to the nearest integre. It always shows the least value of your result.

print(30//4) >>> 7

5.Modulus(%): This operator is also known as remainder operator. It basically returns the remainder by dividing the first number with the second number.

x=30
y= 4
print(30%4) 
>>> 2
Enter fullscreen mode Exit fullscreen mode

6.Exponentiation(**): It performs power calculations on operators.

x=2
y= 3
print(x**y) 
>>> 8   

Enter fullscreen mode Exit fullscreen mode

Round(): For rounding the decimal point to the nearest possible integer.

Round(number [, ndigits])
•Takes a number as an argument
•Square bracket means its optional. The no of digits that you want the round function to round to(eg: round to 3 digits)

Assignment Operator:

1.=(Assign): It assigns the value found in the right operand to the left operand.
name= 123

2.+=(add and assign): Adds the value found in the right operand to the value found in the left operand.

x=5
x+=3    (x=x+3)
print(x) >>> 8

Enter fullscreen mode Exit fullscreen mode

3.-=(subtract and assign): Subtracts the value of the right operand from the value found in the left operand.

x=10
x-=3   (x=x-3)
print(x) >>>7
Enter fullscreen mode Exit fullscreen mode

4.*=(multiply and assign): Multiplies the value of the right operand by the value of the left operand.

x= 5
x*=3   (x=x*3)
print(x)  >>> 15
Enter fullscreen mode Exit fullscreen mode

5./=(Divide and assign):

x=25 
x/=5  (x=x/5)   
print(x) >>> 5
Enter fullscreen mode Exit fullscreen mode

6.%=(Modulus and assign):

x=30
x%=4(x=x%4)   
print(x) >>>2
Enter fullscreen mode Exit fullscreen mode

7.**=(exponent and assign):

x=2
x**=3    (x=x**3)  
print(x) >>>8

Enter fullscreen mode Exit fullscreen mode

8.//=(floor division and assign):

x=30
x//=4   (x=x//4)  
print(x) >>> 7

Enter fullscreen mode Exit fullscreen mode

Identical Operator:
Identical operators check whether two values are identical or not.

i.is: Returns true if the first value is identical or the same as the second value; otherwise, it returns false.

x=10
y=10
Print(x is y) >>>> True
Enter fullscreen mode Exit fullscreen mode

ii.is not: Returns true if the first value is not identical or not the same as the second value; otherwise, it returns false.

x=20
y=21
print(x is not y) >>>> True

Enter fullscreen mode Exit fullscreen mode

Membership Operator:

i.in: Returns true if the first value is in the second. Otherwise, returns False

x= ['apple', 'banana']
print('apple' in x) >>>> True
Enter fullscreen mode Exit fullscreen mode

ii.not in: Returns True if the first value is not in the second. Otherwise, returns False.

x='Hello World'
print('h' not in x) >>>> True

Enter fullscreen mode Exit fullscreen mode

Logical Operators

1.And
2.Or
3.Not

Comparison or Relational operators

1.==(equal)
2.!=(not equal)
3.>(Greater than)
4.<(Less than)
5.>= (greater than or equal)
6<=(Less than or equal)

Top comments (0)