An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result.
There are seven main types of operators in python, they are:
- arithmetic operators
- bitwise operators
- membership operators
- identity operators
- comparison operators
- assignment operators
- logical operators
Arithmetic Operators
These operators are used to carry out mathematical operations in a program.
| Operator | Description | Syntax |
|---|---|---|
| + | Addition | a+b |
| - | Subtraction | a-b |
| / | Division | a/b |
| * | Multiplication | a*b |
| ** | Exponential | a**b |
| % | Modulus | a%b |
| // | Floor division(ignores decimal) | a//b |
Bitwise Operators
These operators are used to manipulate individual bits in a program
| OPERATORS | DESCRIPTIONS | SYNTAX | OUTPUT |
|---|---|---|---|
| & | Binary AND | a&b | copies a bit if it exists in both operands |
| | | Binary OR | a|b | copies a bit if it exists in either operand. |
| ^ | Binary XOR | a^b | copies the bit if it is set in one operand but not both. |
| ~ | Binary One’s Complement | a~b | Unary operation of flipping bits |
| << | Binary Left Shift | a<<b | The left operands value is moved left by the number of bits specified by the right operand. |
| >> | Binary Right Shift | a>>b | left operands value is moved right by the number of bits specified by the right operand. |
Membership Operators
These operators are used to test if a sequence is presented in an object
| Operators | Meaning | Syntax |
|---|---|---|
| in | Returns True if a sequence with the specified value is present in the object | x in y |
| not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
Identity Operators
These operators are used to determine the class or type of the operand
| Operators | Meaning | Syntax |
|---|---|---|
| is | returns True if the type of the value in y points to the same type in the x. | x is y |
| is not | returns True if the type of the value in y points to a different type than the value in the x | x is not y |
Comparison (Relational) Operators
These operators are used to determine the relationships between operands
| Operators | Meaning | Syntax |
|---|---|---|
| == | returns True if a and b are equal | (a == b) |
| != | returns True if a and b are not equal | (a != b) |
| > | returns True if the value of a is greater than that of b | (a > b) |
| < | returns True if the value of a is less than that of b | (a < b) |
| >= | returns True if the value of a is greater than or equal to that of b | (a >= b) |
| <= | returns True if the value of a is less than or equal to that of b | (a <= b) |
Assignment Operators
These operators are used to assign values to variables
| Operators | Meaning | Syntax |
|---|---|---|
| = | equal to | c = a + b |
| += | add to and assign | c += a |
| -= | subtract from and assign | c -= a |
| *= | multiply and assign | c *= a |
| /= | divide and assign | c /= a |
| %= | modulus and assign | c %= a |
| **= | exponent and assign | c **= a |
| //= | floor division and assign | c //= a |
logical Operators
These operators are used to combine conditional statements. These operators implement the logic gates theory into our program.
| Operators | Meaning | Syntax |
|---|---|---|
| AND | a condition is true if both a and b are true | a and b |
| OR | a condition is true if either a and b are true | a or b |
| NOT | Reverse the result, returns False if the result is true | not a |
No comments:
Post a Comment