Operators in Python
Operators
In Python, operators are special symbols that perform specific operations on values and variables.
Arithmetic Operators
Arithmetic operators are symbols used to perform basic mathematical operations on numerical values. These operations include addition, subtraction, multiplication, division, modulus, floor division, and exponentiation.
| Operator | Description | Example |
|---|---|---|
| + | Addition | x + y |
| - | Subtraction | x - y |
| * | Multiplication | x * y |
| / | Division | x / y |
| % | Modulus | x % y |
| ** | Exponentiation | x ** y |
| // | Floor Division | x // y |
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False). They are essential for making decisions and controlling the flow of your program
| Operator | Description | Example |
|---|---|---|
| == | Equal to | x == y |
| != | Not equal to | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Assignment Operators
Assignment operators are used to assign values to variables
| Operator | Description | Example |
|---|---|---|
| = | Assign value | x = y |
| += | Add and assign | x += y |
| -= | Subtract and assign | x -= y |
| *= | Multiply and assign | x *= y |
| /= | Divide and assign | x /= y |
| %= | Modulus and assign | x %= y |
| **= | Exponentiate and assign | x **= y |
| //= | Floor divide and assign | x //= y |
Logical Operators
Logical operators are used to combine conditional statements and return a Boolean value (True or False). They are essential for making decisions and controlling the flow of your program.
| Operator | Description | Example |
|---|---|---|
| and | Logical AND | x and y |
| or | Logical OR | x or y |
| not | Logical NOT | not x |
Bitwise Operators
Bitwise operators work on bits of an integer. They are used to manipulate individual bits within a binary representation of a number.
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | x & y |
| | | Bitwise OR | x | y |
| ^ | Bitwise XOR | x ^ y |
| ~ | Bitwise NOT | ~x |
| << | Left shift | x << y |
| >> | Right shift | x >> y |
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 refer to the same object | x is y |
| is not | Returns True if both variables do not refer to the same object | x is not y |
Membership Operators
Membership operators are used to test whether a value or variable is found in a sequence (such as a string, list, tuple, set, or dictionary).
| Operator | Description | Example |
|---|---|---|
| in | Returns True if a value is present in a sequence | x in y |
| not in | Returns True if a value is not present in a sequence | x not in y |
Comments
Post a Comment