Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jcomte23/Python_vanilla/llms.txt
Use this file to discover all available pages before exploring further.
Introduction to Numbers
Python provides robust support for numeric operations with two primary numeric types: integers (int) and floating-point numbers (float). Understanding the distinction between these types is essential for effective numeric programming.
Integer vs Float
Integers (int)
Integers represent whole numbers without decimal points:
numeroB = int(input("Ingresa un número entero para 'numeroB': "))
print(f"Valor de numeroB: {numeroB}, Tipo: {type(numeroB)}")
Integers can only contain whole numbers without decimals. Examples: 42, -17, 0, 1000
Floating-Point Numbers (float)
Floats represent numbers with decimal points:
numeroC = float(input("Ingresa un número decimal para 'numeroC': "))
print(f"Valor de numeroC: {numeroC}, Tipo: {type(numeroC)}")
Floats can contain both whole numbers and decimals. Examples: 3.14, 2.0, -5.7, 0.001
Type Conversion
Python allows seamless conversion between numeric types:
Converting Float to Integer
entero = int(3.14)
print(entero) # 3
print(type(entero)) # <class 'int'>
When converting float to int, Python truncates the decimal part (rounds down toward zero), not rounds to nearest integer.
Converting String to Float
flotante = float("3.14")
print(flotante) # 3.14
print(type(flotante)) # <class 'float'>
Complete Conversion Example
# Original values
entero = int(3.14)
flotante = float("3.14")
print(f"Entero: {entero}, Tipo: {type(entero)}")
print(f"Flotante: {flotante}, Tipo: {type(flotante)}")
The Math Module
Python’s math module provides advanced mathematical functions and constants:
Mathematical Functions
# Square root
print(math.sqrt(25)) # 5.0
# Factorial
print(math.factorial(5)) # 120
Mathematical Constants
# Pi constant
print(math.pi) # 3.141592653589793
# Euler's number
print(math.e) # 2.718281828459045
The math module must be imported before use: import math
Common Math Module Functions
| Function | Description | Example |
|---|
math.sqrt(x) | Square root of x | math.sqrt(16) → 4.0 |
math.factorial(x) | Factorial of x | math.factorial(5) → 120 |
math.pow(x, y) | x raised to power y | math.pow(2, 3) → 8.0 |
math.floor(x) | Round down to integer | math.floor(3.7) → 3 |
math.ceil(x) | Round up to integer | math.ceil(3.2) → 4 |
math.pi | Pi constant | math.pi → 3.14159… |
math.e | Euler’s number | math.e → 2.71828… |
When accepting numeric input from users, you must convert the string input to the appropriate numeric type:
numero = int(input("Enter an integer: "))
print(f"You entered: {numero}")
print(f"Type: {type(numero)}")
numero = float(input("Enter a decimal number: "))
print(f"You entered: {numero}")
print(f"Type: {type(numero)}")
Always wrap input() with int() or float() when expecting numeric input, as input() returns strings by default.
Practical Example
Here’s a complete example demonstrating number handling:
import math
print("*" * 41)
print("| Manejo de numeros en python |")
print("*" * 41)
print()
print("Para el manejo de números, es importante distinguir entre enteros (int) y números decimales (float)")
print("La variable 'numeroB' será un entero, lo que significa que solo puede contener números enteros, sin decimales.")
print("La variable 'numeroC' permitirá valores decimales, lo que significa que puede contener números enteros y decimales.")
print()
numeroB = int(input("Ingresa un número entero para 'numeroB': "))
numeroC = float(input("Ingresa un número decimal para 'numeroC': "))
print()
print("Conversion de numeros")
print("Python permite convertir entre diferentes tipos de números, como enteros, flotantes y complejos.")
print()
entero = int(3.14)
flotante = float("3.14")
print()
print(f"Valor de numeroB: {numeroB}, Tipo: {type(numeroB)}")
print(f"Valor de numeroC: {numeroC}, Tipo: {type(numeroC)}")
# Otras funciones matemáticas
print(math.sqrt(25)) # Imprime la raíz cuadrada de 25
print(math.factorial(5)) # Imprime el factorial de 5
# Constantes matemáticas
print(math.pi) # Imprime el valor de π
print(math.e) # Imprime el valor de e
Key Points
- Use
int() for whole numbers only
- Use
float() for numbers that may have decimal points
- Import
math module for advanced mathematical operations
- Always convert user input to the appropriate numeric type
- Converting float to int truncates the decimal part