Python Number Types
Number types are used to store numeric values. They are immutable data types, which means changing the value of a numeric variable actually creates a new object.
Python supports three main numeric types:
- Integer (
int): Positive or negative integers without decimal points. Integers in Python 3 can be arbitrarily large, limited only by memory. - Float (
float): Positive or negative real numbers with decimal points. Can also be represented in scientific notation (e.g.,2.5e2represents 2.5 * 10^2 = 250). - Complex (
complex): Composed of a real part and an imaginary part, in the forma + bj, wherejis the imaginary unit.
Basic Arithmetic Operations
Python supports all standard arithmetic operators.
Example:
Type Conversion in Operations
When an expression contains both integers and floats, Python automatically converts integers to floats to maintain precision. This conversion is implicit.
Number Type Conversion Functions
You can also use built-in functions to explicitly convert between different number types.
int(x): Convertsxto an integer (truncates the decimal).float(x): Convertsxto a float.complex(x): Convertsxto a complex number with real partxand imaginary part 0.complex(x, y): Creates a complex number with real partxand imaginary party.
Math Module math
For more advanced mathematical operations like trigonometric functions, logarithms, square roots, etc., you need to import the math module.