Numeric data types
Python supports two basic number formats, integer and floating-point. An integer represents a whole number, and a floating-point format represents a decimal number. The format a language uses to represent data is called a data type. In addition to integer and floating-point types, programming languages typically have a string type for representing text.
Basic arithmetic
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication,
and division.
Four basic arithmetic operators exist in Python:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
Operator precedence
When a calculation has multiple operators, each operator is evaluated in order of precedence. Ex: 1 + 2 * 3 is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses take precedence over multiplication.
Operator | Description | Example | Result |
() | Parentheses | (1 + 2) * 3 | 9 |
** | Exponentiation | 2 ** 4 | 16 |
+, | Positive, negative | -math.pi | -3.14159 |
*, / | Multiplication, division | 2 * 3 | 6 |
+, – | Addition, subtraction | 1 + 2 | 3 |
Responses