Python Operators: Logical, Arithmetic, Comparison with E.g.

Python Operators: Logical, Arithmetic, Comparison with E.g.

In this operator in the python tutorial, you will learn everything about operators in Python with their syntax and how to use operators with operands.

What is operators in python?

In Python programming, operators are used to performing operations on one and more operand and operand values.

Types of Operators in Python

In Python programming, operators are divided into the following groups, which are given below:

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Identity operators
  • Logical operators
  • Membership operators
  • Bitwise operators

Arithmetic operators

The Python Arithmetic operators, which are used to perform mathematical operations like addition, subtraction, multiplication, Modulus, Floor division, and division.

OperatorMeaningExample
+Add two operandsx + y
Subtract right operand from the leftx – y
*Multiply two operandsx * y
/Divide left operand by the right onex / y
%Modulus – the remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y (x to the power y)

Example of arithmetic operators

x = 25
y = 6

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

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

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

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

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

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

Comparison operators

Python comparison operators, which are used to perform compare operations on operand values. This results in true and false returns.

OperatorMeaningExample
>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 of Comparison operators

x = 25
y = 15

# Output: x > y is True
print('x > y  is',x>y)

# Output: x < y is False
print('x < y  is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is True
print('x >= y is',x>=y)

# Output: x <= y is False
print('x <= y is',x<=y)

Assignment operators

Python Assignment operators, which are used to assign a value to the operand. Here operands mean variable.

OperatorMeaningExample
=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

Example of Assignment operators

x = 5

# Output: x = 5
print(x)

# Output: x += 3 is 8
x += 3
print(x)

# Output: x -= 3 is 5
x -= 3
print(x)

# Output: x /= 3 is 1.6666
x /= 3
print(x)

a = 5

# Output: a <<= 3 is 40
a <<= 3
print(a)

# Output: a >>= 3 is 5
a >>= 3
print(a)

# Output: a ^= 3 is 6
a ^= 3
print(a)

# Output: a |= 3 is 7
a |= 3
print(a)

# Output: a &= 3 is 3
a &= 3
print(a)

# Output: a **= 3 is 27
a **= 3
print(a)

# Output: a //= 3 is 9
a//=3
print(a)

# Output: a %= 3 is 0
a%=3
print(a)

Identity operators

Identity operators are used to comparing the objects, not if they are equal, but if they are actually the same object, with the same memory location.

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 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)

Logical operators

The Python logical operators, which are used to perform logical operations on operands.

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

Example of Logical operators

x = True
y = False

# Output: x and y is False
print('x and y is',x and y)

# Output: x or y is True
print('x or y is',x or y)

# Output: not x is False
print('not x is',not x)

Membership operators

Python membership operators, which are used to test whether a value or variable is in a sequence.

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 of Membership operators

Result Size: 668 x 508
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)

Bitwise operators

Python Bitwise operators, which are used perform the compare (binary numbers) operation on the operand. Here operands mean variable.

OpratorMeaningExample
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Example of Bitwise operators

a = 10
b = 4

# bitwise AND operation 
print(a & b) 

# bitwise OR operation 
print(a | b) 

# bitwise NOT operation 
print(~a) 

# bitwise XOR operation 
print(a ^ b) 

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

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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