Numeric Data and Operators
✕Numeric Data Type
- Python has three numeric data types: int, float, and complex
- Whole numbers (+ve, -ve or zero), but without decimal point
- Example:
10,-5,0 - Numbers with a decimal point (+ve, -ve or zero)
- Example:
3.14,-2.5,0.1,3.0 - Numbers with real and imaginary parts
- Example:
3 + 4j,-2.5 + 3.14j,0 + 2j
int (integer)
float
complex
Operators
- Special symbols that perform operations on variables and values
- Types: Assignment, Arithmetic, Comparison, Logical, Bitwise, Membership, Identity
a = 10b = 3 calc_1 = 3 + 2 print(calc_1) my_calc = a + 3 * (4 - 1) print(my_calc) calculation = (a + b) * (a - b) / 4 print(calculation)
Examples
Arithmetic Operators
Performs mathematical operations on numeric data
| Operator | Name | Description | Expression | Result |
|---|---|---|---|---|
| + | Addition | Add two numbers | 3 + 2 | 5 |
| - | Subtraction | Subtract one number from another | 5 - 1 | 4 |
| * | Multiplication | Multiply two numbers | 4 * 2 | 8 |
| / | Division | Divide one number by another | 7 / 4 | 1.75 |
| // | Floor Division | Divide and round down to nearest integer | 10 // 3 | 3 |
| % | Modulus | Remainder after division | 10 % 3 | 1 |
| ** | Exponent | Raise a number to the power of another | 2 ** 3 | 8 |
Arithmetic operators in Python
Assignment Operators
Assigns values to variables. Can also perform an operation and assignment in one step
| Operator | Name | Description | Expression | Result |
|---|---|---|---|---|
| = | Simple Assignment | Assigns value on right to variable on left | x = 5 | Equivalent to x = 5 |
| = | Multiple Assignment | Assigns same value to multiple variables | x = y = z = 10 | Equivalent to x = 10; y = 10; z = 10 |
| = | Positional Assignment | Assigns multiple values to multiple variables | a, b, c = 1, 2, 3 | Equivalent to a = 1; b = 2; c = 3 |
| += | Add and Assignment | Adds value on right to variable on left | x += 5 | Equivalent to x = x + 5 |
| -= | Subtract and Assignment | Subtracts value on right from variable on left | x -= 5 | Equivalent to x = x - 5 |
| *= | Multiply and Assignment | Multiplies value on right to variable on left | x *= 5 | Equivalent to x = x * 5 |
| /= | Divide and Assignment | Divides variable on left by value on right | x /= 5 | Equivalent to x = x / 5 |
| //= | Floor Divide and Assignment | Floor divides variable on left by value on right | x //= 5 | Equivalent to x = x // 5 |
| %= | Modulus and Assignment | Takes modulus of variable on left by value on right | x %= 5 | Equivalent to x = x % 5 |
| **= | Exponent and Assignment | Raises variable on left to power of value on right | x **= 5 | Equivalent to x = x ** 5 |
Assignment operators in Python
Comparison Operators
Compares two values and return a boolean result (True or False)
| Operator | Name | Description | Expression | Result |
|---|---|---|---|---|
| == | Equal to | Returns True if both operands are equal | 5 == 5 | True |
| != | Not equal to | Returns True if both operands are not equal | 5 != 5 | False |
| < | Less than | Returns True if left operand is less than right operand | 5 < 10 | True |
| > | Greater than | Returns True if left operand is greater than right operand | 10 > 5 | True |
| <= | Less than or equal | Returns True if left operand is less than or equal to right one | 5 <= 5 | True |
| >= | Greater than or equal | Returns True if left operand is greater than or equal to right one | 10 >= 5 | True |
Comparison operators in Python
Logical Operators
Combines multiple conditional statements and return a boolean result
| Operator | Name | Description | Expression | Result |
|---|---|---|---|---|
| and | Logical AND | Returns True if both operands are True | (5 < 10) and (4 > 5) | True |
| or | Logical OR | Returns True if at least one operand is True | (5 < 10) or (4 > 5) | True |
| not | Logical NOT | Reverses the result of the operand | not (5 < 10) | False |
Logical operators in Python
Membership Operators
Checks if a value is a member of a sequence (string, list, tuple, set, dictionary)
| Operator | Name | Description | Expression | Result |
|---|---|---|---|---|
| in | Membership Test | Returns True if value is found in the sequence | "a" in "cat" | True |
| not in | Membership Test | Returns True if value is not found in the sequence | "cat" not in ["cat", "rat"] | False |
Membership operators in Python
