Operators
Operators are used to perform operations on variables and data.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Identity Operators
- Membership Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables or data.
| Operator | Name | Code | Description |
|---|---|---|---|
| + | Addition | a + b | Adds two data values. |
| - | Subtraction | a - b | Subtracts two data values. |
| * | Multiplication | a * b | Multiply two data values. |
| / | Division | a / b | Divides two data values. |
| % | Modulus | a % b | Return remainder when two data values are divided. |
| // | Floor Division | a // b | Return answer of floor division. |
| ** | Exponentiation | a ** b | Raises the first operand to the power of the second operand. |
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Name | Code | Description |
|---|---|---|---|
| = | Assignment | a = 5 | Assign value to a variable. |
| += | Addition Assignment | a += b | Adds a and b and stores result in a. |
| -= | Subtraction Assignment | a -= b | Subtracts a and b and stores result in a. |
| *= | Multiplication Assignment | a *= b | Multiplies a and b and stores result in a. |
| /= | Division Assignment | a /= b | Divides a and b and stores result in a. |
| //= | Floor Division Assignment | a //= b | Applies floor division on a and b and stores result in a. |
| %= | Modulus Assignment | a %= b | Takes modulus of a and b and stores result in a. |
| **= | Exponentiation Assignment | a **= b | Raises a to the power of b and assigns the result to a. |
| |= | Bitwise OR Assignment | a |= b | Performs bitwise OR operation between a and b, and assigns the result to a. |
| &= | Bitwise AND Assignment | a &= b | Performs bitwise AND operation between a and b, and assigns the result to a. |
| ^= | Bitwise XOR Assignment | a ^= b | Performs bitwise XOR operation between a and b, and assigns the result to a. |
| <<= | Left Shift Assignment | a <<= b | Shifts the bits of a to the left by the number of positions specified on b, and assigns the result to a. |
| >>= | Right Shift Assignment | a >>= b | Shifts the bits of a to the right by the number of positions specified on b, and assigns the result to a. |
Comparison Operators
Comparison operators are used to compare two variables or data values.
| Operator | Name | Code | Description |
|---|---|---|---|
| == | equal to | a == b | Check if a is equal to b. |
| != | not equal to | a != b | Check if a is not equal to b. |
| > | greater than | a > b | Checks whether a is greater than b. |
| < | less than | a < b | Checks whether a is less than b. |
| >= | greater than or equal to than | a >= b | Checks whether a is greater than or equal to b. |
| <= | less than or equal to than | a <= b | Checks whether a is less than or equal to b. |
Logical Operators
Logical operators are used along with conditional statements.
| Operator | Name | Syntax | Description |
|---|---|---|---|
| and | Logical AND | condition1 and condition2 | check if both conditions are true. |
| or | Logical OR | condition1 or condition2 | check if condition1 or condition2 is true. |
| not | Logical NOT | not condition | return the opposite of the given condition. |
Bitwise Operators
Bitwise operators perform operations on bit level on numbers.
| Operator | Name | Syntax | Description |
|---|---|---|---|
| & | Bitwise AND | num1 & num2 | Performs a bitwise AND operation on the corresponding bits of two integers. |
| | | Bitwise OR | num1 | num2 | Performs a bitwise OR operation on the corresponding bits of two integers. |
| ^ | Bitwise XOR | num1 ^ num2 | Performs a bitwise exclusive OR (XOR) operation on the corresponding bits of two integers. |
| ~ | Bitwise NOT | num1 ~ num2 | Performs a bitwise NOT operation, which inverts the bits of the integer. |
| &ls; | Bitwise Left Shift | num1 &ls;&ls; num2 | Shifts the bits of an integer to the left by a specified number of positions. |
| &rs; | Bitwise Right Shift | num1 &rs;&rs; num2 | Shifts the bits of an integer to the right by a specified number of positions. |
Identity Operators
Identity operators are used to compare the memory locations of two objects to determine whether they are the same object or different.
| Operator | Name | Syntax | Description |
|---|---|---|---|
| is | is | x is y | returns True if both operands point to the same object in memory, |
| is not | is not | x is not y | returns True if both operands do not point to the same object in memory, |
Membership Operators
Membership operators are used to test whether a value is a member of a sequence (like a string, list, or tuple) or a collection (like a dictionary or a set).
| Operator | Name | Syntax | Description |
|---|---|---|---|
| in | in | value in sequence | returns True if the specified value is found in the sequence or collection |
| not in | not in | value not in sequence | returns True if the specified value is not found in the sequence or collection |
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
What will be the output of this code?
a = 10, b = 3
result = a / b
print(result)