Thursday, December 26, 2024
Google search engine
HomeLanguagesPython 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.

Operator Meaning Example
+ Add two operands x + y
Subtract right operand from the left x – y
* Multiply two operands x * y
/ Divide left operand by the right one x / y
% Modulus – the remainder of the division of left operand by the right x % y (remainder of x/y)
// Floor division – division that results into whole number adjusted to the left in the number line x // y
** Exponent – left operand raised to the power of right x**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.

Operator Meaning Example
> Greater than – True if left operand is greater than the right x > y
< Less than – True if left operand is less than the right x < y
== Equal to – True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
>= Greater than or equal to – True if left operand is greater than or equal to the right x >= y
<= Less than or equal to – True if left operand is less than or equal to the right x <= y

.banner-1-multi-360{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

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.

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

Operator Meaning Example
is True if the operands are identical (refer to the same object) x is True
is not True 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.

Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True 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.

Operator Meaning Example
in True if value/variable is found in the sequence 5 in x
not in True if value/variable is not found in the sequence 5 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.

Oprator Meaning Example
& Bitwise AND x & y
| Bitwise OR x | y
~ Bitwise NOT ~x
^ Bitwise XOR x ^ y
>> Bitwise right shift x>>
<< Bitwise left shift x<<

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) 

RELATED ARTICLES

Most Popular

Recent Comments