- Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
Operator |
Description |
Syntax |
+ |
Addition: adds two operands |
x + y |
- |
Subtraction: subtracts two operands |
x - y |
* |
Multiplication: multiplies two operands |
x * y |
/ |
Division (float): divides the first operand by the second |
x / y |
// |
Division (floor): divides the first operand by the second |
x // y |
% |
Modulus: returns the remainder when first operand is divided by the second |
x % y |
a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
div1 = a / b
div2 = a / / b
mod = a % b
print (add)
print (sub)
print (mul)
print (div1)
print (div2)
print (mod)
|
Output:
13
5
36
2.25
2
1
- Relational Operators: Relational operators compares the values. It either returns True or False according to the condition.
Operator |
Description |
Syntax |
> |
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 |
a = 13
b = 33
print (a > b)
print (a < b)
print (a = = b)
print (a ! = b)
print (a > = b)
print (a < = b)
|
Output:
False
True
False
True
False
True
- Logical operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations.
Operator |
Description |
Syntax |
and |
Logical AND: True if both the operands are true |
x and y |
or |
Logical OR: True if either of the operands is true |
x or y |
not |
Logical NOT: True if operand is false |
not x |
a = True
b = False
print (a and b)
print (a or b)
print ( not a)
|
Output:
False
True
False
- Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
Operator |
Description |
Syntax |
& |
Bitwise AND |
x & y |
| |
Bitwise OR |
x | y |
~ |
Bitwise NOT |
~x |
^ |
Bitwise XOR |
x ^ y |
>> |
Bitwise right shift |
x>> |
<< |
Bitwise left shift |
x<< |
a = 10
b = 4
print (a & b)
print (a | b)
print (~a)
print (a ^ b)
print (a >> 2 )
print (a << 2 )
|
Output:
0
14
-11
14
2
40
- Assignment operators: Assignment operators are used to assign values to the variables.
Operator |
Description |
Syntax |
= |
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 |
- Special operators: There are some special type of operators like-
a1 = 3
b1 = 3
a2 = 'GeeksforGeeks'
b2 = 'GeeksforGeeks'
a3 = [ 1 , 2 , 3 ]
b3 = [ 1 , 2 , 3 ]
print (a1 is not b1)
print (a2 is b2)
print (a3 is b3)
|
Output:
False
True
False
x = 'Geeks for Geeks'
y = { 3 : 'a' , 4 : 'b' }
print ( 'G' in x)
print ( 'geeks' not in x)
print ( 'Geeks' not in x)
print ( 3 in y)
print ( 'b' in y)
|
Output:
True
True
False
True
False
leave a comment
0 Comments