Python Operators
Python Operators Codigence
What are Operators?
Operators are special symbols which represents computation. They are applied on operand(s), which can be values or variables. Same operator can behave differently on different data types. Operators when applied on operands form an expression.Value and variables when used with operator are known as operands.
Types of Operator:
Python language supports the following types of operators :
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
Python Arithmetic Operators
Operator | Name | Example |
---|---|---|
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%y |
** | Exponential | x**y |
// | Floor Division | x//y |
Example :
x = 20
y = 5
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x**y)
print(x//y)
Python Comparison Operators
These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.
Operator | Name | Example |
---|---|---|
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Example :
x = 20
y = 5
print(x==y)
print(x!=y)
print(x>y)
print(x<y)
print(x>=y)
print(x<=y)
Python Assignment Operators
Operator | Example | Equals to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
Example :
#python assignment operator
x = 20
print(x)
x+=5
print(x)
x-=10
print(x)
x*=8
print(x)
x/=4
print(x)
x%=8
print(x)
x//=2
print(x)
x**=2
print(x)
Python Logical Operators
There are following logical operators supported by Python language.
Operator | Description | Example |
---|---|---|
and Logical AND | If both the operands are true then condition becomes true. | x > 5 and y <10 |
or Logical OR | If any of the two operands are non-zero then condition becomes true. | x > 5 or y <10 |
not Logical NOT | Used to reverse the logical state of its operand. | not(x > 5) |
Example :
x = 20
y = 5
print(x>5 and y<10)
print(x<5 or y<10)
print(not(x > 10))
Python Membership Operators
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.
There are two membership operators as explained below −.
Operator | Description | Example |
---|---|---|
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | x in y, here in results in a 1 if x is a member of sequence y. |
not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. | x not in y, here not in results in a 1 if x is not a member of sequence y. |
Example :
x = ["apple", "banana"]
print("apple" in x)
print("orange" not in x)
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
Example :
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
print(x is not z)
print(x is y)
print(x is not y)
print(x != y)
Python Bitwise Operators
IBitwise operator works on bits and performs bit by bit operation.
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
Example :
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print(c)
c = a | b; # 61 = 0011 1101
print(c)
c = a ^ b; # 49 = 0011 0001
print(c)
c = ~a; # -61 = 1100 0011
print(c)
c = a << 2; # 240 = 1111 0000
print(c)
c = a >> 2; # 15 = 0000 1111
print(c)
Post a Comment