Numbers are fundamental to nearly all programming tasks. Python provides several built-in numeric data types to handle different kinds of numbers:
- Integers (
int) Integers are whole numbers, positive or negative, without a decimal point. Python 3 integers have arbitrary precision, meaning they can be as large as your computer's memory allows, unlike some other languages with fixed-size integers.
python# Examples of integers a = 10 b = -5 c = 0 d = 12345678901234567890 # Arbitrary precision print(type(a)) # Output: <class 'int'>
- Floating-Point Numbers (
float) Floating-point numbers (or 'floats') are numbers that have a decimal point. They can represent real numbers, and are typically implemented using double-precision (64-bit) floating-point numbers.
python# Examples of floats e = 3.14 f = -0.5 g = 2.0 # Even if it's a whole number, the decimal makes it a float h = 1.23e5 # Scientific notation: 1.23 * 10^5 = 123000.0 print(type(e)) # Output: <class 'float'>
- Complex Numbers (
complex) Python also supports complex numbers, which are numbers with a real and an imaginary part, expressed asx + yj, wherexis the real part,yis the imaginary part, andjis the imaginary unit (equal to the square root of -1).
python# Example of a complex number i = 3 + 4j j = -1.5 + 2.7j print(type(i)) # Output: <class 'complex'> print(i.real) # Output: 3.0 print(i.imag) # Output: 4.0
Type Conversion (Type Casting)
Sometimes you need to convert a value from one data type to another.
Python provides built-in functions for this:
int(): Converts a value to an integer.float(): Converts a value to a floating-point number.str(): Converts a value to a string (we'll cover strings next).
Important Notes on Conversions:
floattoint: Truncates the decimal part (rounds down towards zero). It does not round to the nearest whole number.strtoint/float: The string must represent a valid number. Attempting to convert a non-numeric string will result in aValueError.
python# Example conversions num_int = 10 num_float = 3.14 num_str_int = "100" num_str_float = "25.7" # int to float converted_float = float(num_int) print(f"int to float: {converted_float}, type: {type(converted_float)}") # Output: 10.0, <class 'float'> # float to int converted_int = int(num_float) print(f"float to int: {converted_int}, type: {type(converted_int)}") # Output: 3, <class 'int'> # string to int converted_from_str_int = int(num_str_int) print(f"string to int: {converted_from_str_int}, type: {type(converted_from_str_int)}") # Output: 100, <class 'int'> # string to float converted_from_str_float = float(num_str_float) print(f"string to float: {converted_from_str_float}, type: {type(converted_from_str_float)}") # Output: 25.7, <class 'float'> # Invalid conversion example (will raise ValueError) # invalid_str = "hello" # int(invalid_str) # Even converting a float string to an integer directly will error (must first go to float) # int("3.14") # ValueError: invalid literal for int() with base 10: '3.14' # correct way: correct_int_from_float_str = int(float("3.14")) print(f"Correct float string to int: {correct_int_from_float_str}, type: {type(correct_int_from_float_str)}") # Output: 3, <class 'int'>
Understanding these basic numeric types and how to convert between them is crucial for performing calculations and handling user input in your Python programs.
Key Takeaways:
- Python has
int(whole numbers),float(decimal numbers), andcomplex(complex numbers). - Integers in Python 3 have arbitrary precision.
- Use
int(),float(), andstr()for type conversions. - Be careful when converting between types, especially
floattoint(truncates) andstrto numbers (requires valid numeric string).