Press "Enter" to skip to content

Python operator

Zigya Acadmey 0

In this article, you’ll learn everything about different types of operators in Python, their syntax and how to use them with examples.

What are operators in python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

For example:

>>> 2+3
5

Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.

Arithmetic operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.

OperatorDescriptionSyntax
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when first operand is divided by the secondx % y
**Power : Returns first raised to power secondx ** y

Example for Arithmetic operators

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Relational Operator

Relational operators compares the values. It either returns True or False according to the condition.

OperatorDescriptionSyntax
>Greater than: True if left operand is greater than the rightx > y
<Less than: True if left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to: True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to: True if left operand is less than or equal to the rightx <= y

Example for Relational operator

# Examples of Relational Operators 
a = 10
b = 30
  
# a > b is False 
print(a > b) 
  
# a < b is True 
print(a < b) 
  
# a == b is False 
print(a == b) 
  
# a != b is True 
print(a != b) 
  
# a >= b is False 
print(a >= b) 
  
# a <= b is True 
print(a <= b)

Logical operators

Logical operators perform Logical ANDLogical OR and Logical NOT operations.

OperatorDescriptionSyntax
andLogical AND: True if both the operands are truex and y
orLogical OR: True if either of the operands is truex or y
notLogical NOT: True if operand is falsenot x

Example for logical operators

# Examples of Logical Operator 
a = True
b = False

# Print a and b is False 
print(a and b) 

# Print a or b is True 
print(a or b) 

# Print not a is False 
print(not a) 

Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.

For example, 2 is 10 in binary and 7 is 111.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

OperatorMeaningExample
&Bitwise ANDx & y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx >> 2 = 2 (0000 0010)
<<Bitwise left shiftx << 2 = 40 (0010 1000)

Example for Bitwise operators

# Examples of Bitwise operators 
a = 10
b = 4

# Print bitwise AND operation 
print(a & b) 

# Print bitwise OR operation 
print(a | b) 

# Print bitwise NOT operation 
print(~a) 

# print bitwise XOR operation 
print(a ^ b) 

# print bitwise right shift operation 
print(a >> 2) 

# print bitwise left shift operation 
print(a << 2) 

Assignment operators

Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.

There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

OperatorDescriptionSyntax
=Assign value of right side of expression to left side operandx = y + z
+=Add AND: Add right side operand with left side operand and then assign to left operanda+=b     a=a+b
-=Subtract AND: Subtract right operand from left operand and then assign to left operanda-=b       a=a-b
*=Multiply AND: Multiply right operand with left operand and then assign to left operanda*=b       a=a*b
/=Divide AND: Divide left operand with right operand and then assign to left operanda/=b         a=a/b
%=Modulus AND: Takes modulus using left and right operands and assign result to left operanda%=b   a=a%b
//=Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda//=b       a=a//b
**=Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda**=b     a=a**b
&=Performs Bitwise AND on operands and assign value to left operanda&=b     a=a&b
|=Performs Bitwise OR on operands and assign value to left operanda|=b         a=a|b
^=Performs Bitwise xOR on operands and assign value to left operanda^=b       a=a^b
>>=Performs Bitwise right shift on operands and assign value to left operanda>>=b     a=a>>b
<<=Performs Bitwise left shift on operands and assign value to left operanda <<= b                    a= a << b

Special operators

Python language offers some special types of operators like the identity operator or the membership operator. They are described below with examples.

Identity operators

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

Example for Identity operators

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

# Output: False
print(x1 is not y1)

# Output: True
print(x2 is y2)

# Output: False
print(x3 is y3)

Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).

But x3 and y3 are lists. They are equal but not identical. It is because the interpreter locates them separately in memory although they are equal.

Membership operators

in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).

In a dictionary we can only test for presence of key, not the value.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Example #5: Membership operators in Python

x = 'Hello world'
y = {1:'a',2:'b'}

# Output: True
print('H' in x)

# Output: True
print('hello' not in x)

# Output: True
print(1 in y)

# Output: False
print('a' in y)

Conclusion

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *