Table of Contents » Reference Materials : Operators
Operators
Page Contents
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Operator | Operation | Description |
---|---|---|
+ | Addition |
Adds two values together. Examples:
|
- | Subtraction | Subtracts one value from another values. |
* | Multiplication | Multiples two values together. |
/ | Division | Divids one value by another value. Be careful not to attempt to divide by zero. |
% | Modulus | Returns the remainder of dividing one vaolule by another value. |
** | Exponentiation | Rasies the power of one value by another value. |
// | Floor Division | Divides one value by another value, then returns the floor value of the result. The floor value is the greatest integer less than or equal to the resulting value. |
Operator | Description |
---|---|
= |
Assignment Operator: The most basic assignment operator, which simply assigns the value on the right-hand side to the variable on the left-hand side.
Example:
|
+= |
Add AND Operator: This operator adds the value on the right-hand side to the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
-= |
Subtract AND Operator: This operator subtracts the value on the right-hand side from the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
*= |
Multiply AND Operator: This operator multiplies the value on the right-hand side with the variable on the left-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
/= |
Divide AND Operator: This operator divides the variable on the left-hand side by the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
%= |
Modulo AND Operator: This operator calculates the remainder when the variable on the left-hand side is divided by the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
//= |
Integer Division AND Operator: This operator performs integer division between the variable on the left-hand side and the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
**= |
Exponentiation AND Operator: This operator raises the variable on the left-hand side to the power of the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
&= |
Bitwise AND Operator: This operator performs a bitwise AND operation between the variable on the left-hand side and the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
!= |
Bitwise OR Operator: This operator performs a bitwise OR operation between the variable on the left-hand side and the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
^= |
Bitwise XOR Operator: This operator performs a bitwise XOR operation between the variable on the left-hand side and the value on the right-hand side, and then assigns the result to the variable on the left-hand side.
Examples:
|
Operator | Comparison | Description |
---|---|---|
== | Equal |
The double-equal symbol compares the left-side value with the right-side value. If they are equal, then it results in True. If the comparison is not equal, then it results in False. Note: A common mistake in coding is to use the single-equal sign (=) instead of the double-equal (==). This mistake can cause significant problems because instead of making a comparison, the single-equal assigns the valuse of the right-side to the left-side. Example:
Condition Result: False
|
!= | Not Equal |
This comparison operator is the opposite of the equal operator above. It compares the left-side value with the right-side value. If they are not equal, then it results in True. If the comparison is equal, then it results in False.
Example:
Condition Result: True
|
> | Greater Than |
This comparison operator checks if the left-side value is greater than (determined by the data type) the right-side value. If the left-side is greater, then it results in True. If the left-side is not greater than the right side, then it results in False.
Example:
Condition Result: False
|
< | Less Than |
This comparison operator checks if the left-side value is less than (determined by the data type) the right-side value. If the left-side is less, then it results in True. If the left-side is not less than the right side, then it results in False.
Example:
b>Condition Result: True
|
>= | Greater Than or Equal To |
This comparison operator checks if the left-side value is greater or equal to (determined by the data type) the right-side value. If the left-side is greater or equal to, then it results in True. If the left-side is not greater or equal to than the right side, then it results in False.
Example:
Condition Result: False
|
<= | Less Than or Equal To |
This comparison operator checks if the left-side value is less than or equal to (determined by the data type) the right-side value. If the left-side is less than or equal to, then it results in True. If the left-side is not less than or equal to the right side, then it results in False.
Example:
Condition Result: True
|
Operator | Description |
---|---|
and | Returns True if both statements are true |
or | Returns True if one of the statements is true |
not | Reverse the result, returns False if the result is true |
Operator | Description |
---|---|
is | Returns True if both variables are the same object |
is not | Returns True if both variables are not the same objec |
Operator | Description |
---|---|
in | Returns True if a sequence with the specified value is present in the object |
not in | Returns True if a sequence with the specified value is not present in the object |
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 |