#PRIMEROS PASOS EN PYTHON

PRIMEROS PASOS EN PYTHON

Python es un lenguaje de programación de alto nivel, interpretado y multiparadigma, esta diseñado para ser fácil de leer y escribir gracias a su sintaxis clara y sencilla.

CARACTERISTICAS DE PYTHON

#Version
import sys
print(sys.version)
## 3.11.13 (main, Jul 11 2025, 22:36:59) [MSC v.1944 64 bit (AMD64)]
#Informacion completa
import sys
import platform

info = {
    "version": sys.version,
    "major": sys.version_info.major,
    "minor": sys.version_info.minor,
    "micro": sys.version_info.micro,
    "compiler": platform.python_compiler(),
    "implementation": platform.python_implementation(),
    "system": platform.system(),
    "release": platform.release()
}
print(info)
## {'version': '3.11.13 (main, Jul 11 2025, 22:36:59) [MSC v.1944 64 bit (AMD64)]', 'major': 3, 'minor': 11, 'micro': 13, 'compiler': 'MSC v.1944 64 bit (AMD64)', 'implementation': 'CPython', 'system': 'Windows', 'release': '10'}

Directorio de trabajo

import os
import os, inspect

# Observar el directorio actual
print(os.getcwd())
## C:\Users\MARYORIE\Downloads\CURSO PROGRAMACION
# Cambiar directorio
os.chdir("C:/Users/MARYORIE/Downloads")
print("Directorio cambiado:",os.getcwd())
## Directorio cambiado: C:\Users\MARYORIE\Downloads

Operaciones basicas

# Suma
print(9 + 7)
## 16
# Resta
print(85 - 41)
## 44
# Multiplicación
print(8 * 5)
## 40
# División (Python siempre devuelve float con /)
print(51 / 7)
## 7.285714285714286
# Paréntesis modifica el orden
print(87 / (5 + 9))
## 6.214285714285714
# Potenciación (solo con ** en Python)
print(6 ** 3)
## 216
# Resto (módulo)
print(17 % 5)
## 2
# División entera
print(17 // 5)
## 3

Funciones Matematicas

import math

# Raíz cuadrada
print(math.sqrt(73))
## 8.54400374531753
# Valor absoluto
print(abs(-73))
## 73
# Función trigonométrica - seno(pi/2)
print(math.sin(math.pi / 2))
## 1.0
# Función trigonométrica - coseno
print(math.cos(0))
## 1.0
# Logaritmo natural (base e)
print(math.log(10))
## 2.302585092994046
# Logaritmo base 10
print(math.log10(100))
## 2.0
# Logaritmo en base 4
print(math.log(10, 4))  # segundo argumento es la base
## 1.6609640474436813
# Exponencial (e^1)
print(math.exp(1))
## 2.718281828459045
# Redondeo
print(round(math.pi, 2))
## 3.14

Notacion cientifica

# Número grande
print(2324511042900)
## 2324511042900
# Número muy pequeño
print(0.000000002581)
## 2.581e-09
# Forzar notación científica
print("{:.2e}".format(123456))   # 2 decimales en notación científica
## 1.23e+05
print(f"{123456:.2e}")           # otra forma con f-strings
## 1.23e+05
# Suprimir notación científica (mostrar número normal)
print("{:.0f}".format(123456))   # sin notación científica
## 123456
print(f"{123456:d}")             # entero sin notación científica
## 123456

Sistema de ayuda

import math
import inspect

# Ayuda en funciones específicas
help(math.sqrt)   # muestra la documentación de sqrt
## Help on built-in function sqrt in module math:
## 
## sqrt(x, /)
##     Return the square root of x.
# Buscar documentación (similar a ?? en R)
# en Python se puede usar help() con palabras clave
help("sqrt")
## No Python documentation found for 'sqrt'.
## Use help() to get the interactive help utility.
## Use help(str) for help on the str class.
# Ayuda sobre operadores
help("**")
## The power operator
## ******************
## 
## The power operator binds more tightly than unary operators on its
## left; it binds less tightly than unary operators on its right.  The
## syntax is:
## 
##    power ::= (await_expr | primary) ["**" u_expr]
## 
## Thus, in an unparenthesized sequence of power and unary operators, the
## operators are evaluated from right to left (this does not constrain
## the evaluation order for the operands): "-1**2" results in "-1".
## 
## The power operator has the same semantics as the built-in "pow()"
## function, when called with two arguments: it yields its left argument
## raised to the power of its right argument.  The numeric arguments are
## first converted to a common type, and the result is of that type.
## 
## For int operands, the result has the same type as the operands unless
## the second argument is negative; in that case, all arguments are
## converted to float and a float result is delivered. For example,
## "10**2" returns "100", but "10**-2" returns "0.01".
## 
## Raising "0.0" to a negative power results in a "ZeroDivisionError".
## Raising a negative number to a fractional power results in a "complex"
## number. (In earlier versions it raised a "ValueError".)
## 
## This operation can be customized using the special "__pow__()" method.
## 
## Related help topics: EXPRESSIONS, OPERATORS
# Ejemplo de alguna función → en Python no hay "example()", 
# pero puedes probar directamente:
print(abs(-5))   # ejemplo abs
## 5
print(round(3.14159, 2))  # ejemplo round
## 3.14
# Argumentos de una función
print(inspect.signature(abs))    # muestra los argumentos de abs
## (x, /)
print(inspect.signature(round))  # muestra los argumentos de round
## (number, ndigits=None)

Aplicaciones numéricas, aritmética y asignación

Asignación

# Asignación estándar con "="
x = 20
print(x)
## 20
# Otra variable
y = 30
print(y)
## 30
# No existe el operador "->" en Python,
# simplemente se hace asignación normal
Z = 40
print(Z)
## 40
# Mostrar valor al asignar (igual que (w <- 50) en R)
w = 50
print(w)
## 50

Palabras reservadas

# Valores especiales en Python
print(True)    # equivalente a TRUE en R
## True
print(False)   # equivalente a FALSE en R
## False
print(None)    # equivalente a NULL en R
## None
print(float("inf"))   # Infinito positivo
## inf
print(float("-inf"))  # Infinito negativo
## -inf
print(float("nan"))   # Not a Number (NaN)
## nan
# NA no existe como tal en Python, 
# pero en pandas se usa pandas.NA o numpy.nan
import numpy as np
import pandas as pd
print(np.nan)   # NaN con numpy
## nan
print(pd.NA)    # NA con pandas
## <NA>
# Listar palabras reservadas en Python
import keyword
print(keyword.kwlist)   # muestra todas las palabras reservadas
## ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Gestión del workspace

# Listar todos los nombres en el espacio actual
print(dir())
## ['Z', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'info', 'inspect', 'keyword', 'math', 'np', 'os', 'pd', 'platform', 'r', 'sys', 'w', 'x', 'y']
# Información sobre un objeto (similar a str en R)
x = [1, 2, 3]
print(type(x))       # tipo de objeto
## <class 'list'>
print(len(x))        # longitud (si aplica)
## 3
print(x)             # contenido
## [1, 2, 3]
# Eliminar un objeto específico
y = 10
print(y)
## 10
del y   # elimina la variable
# print(y)  # daría error porque ya no existe

# Eliminar todo (similar a rm(list=ls()))
# ⚠️ Se hace con globals() en un script interactivo
for name in list(globals().keys()):
    if name not in ["__builtins__", "__name__", "__file__", "__doc__", "__package__"]:
        del globals()[name]

Vectores y escalares

Escalar

# Asignar
edad = 20
print(edad)
## 20

Vector

Es una secuencia de datos, que tiene un mismo tipo de dato

import math

# Creación de una lista (equivalente a vector en R)
vector = [21, 23, 25, 27, 29]
print(vector)
## [21, 23, 25, 27, 29]
# Lista de caracteres (strings)
color = ["rojo", "Azul", "verde"]
print(color)
## ['rojo', 'Azul', 'verde']
# Lista a partir de cálculos
vector3 = [74 + 5, math.sqrt(25), abs(-23), 84 / 7]
print(vector3)
## [79, 5.0, 23, 12.0]
# Combinar listas
v1 = [1, 2, 3]
v2 = [4, 5, 6]
v3 = v1 + v2   # concatenación
print(v3)
## [1, 2, 3, 4, 5, 6]
# Tipo de datos (similar a class en R)
print(type(vector))   # list
## <class 'list'>
print(type(vector3))  # list
## <class 'list'>
print(type(color))    # list
## <class 'list'>
print(type(v3))       # list
## <class 'list'>
import numpy as np

vector4 = np.array([40, "Hola", 50])
print(vector4)
## ['40' 'Hola' '50']
print(type(vector4))   # numpy.ndarray
## <class 'numpy.ndarray'>
print(vector4.dtype)   # dtype='<U21' (es decir, todo convertido a texto)
## <U21

operador para secuencias

# Secuencias enteras con range()
print(list(range(1, 11)))   # 1 al 10
## [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Secuencia descendente
print(list(range(15, 9, -1)))  # de 15 a 10
## [15, 14, 13, 12, 11, 10]
# Secuencia con negativos
print(list(range(-5, 6)))   # de -5 a 5
## [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
# Secuencia con decimales -> usar numpy.arange()
import numpy as np
print(np.arange(1.5, 5.9, 1))  # de 1.5 a 5.8 con paso de 1
## [1.5 2.5 3.5 4.5 5.5]

utilizando la función seq

import numpy as np

# Por incremento específico
print(np.arange(0, 11, 2))      # de 0 a 10 con paso de 2
## [ 0  2  4  6  8 10]
print(np.arange(0, 10.1, 0.2))  # de 0 a 10 con paso de 0.2
## [ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2.   2.2  2.4  2.6
##   2.8  3.   3.2  3.4  3.6  3.8  4.   4.2  4.4  4.6  4.8  5.   5.2  5.4
##   5.6  5.8  6.   6.2  6.4  6.6  6.8  7.   7.2  7.4  7.6  7.8  8.   8.2
##   8.4  8.6  8.8  9.   9.2  9.4  9.6  9.8 10. ]
# Por número de elementos (similar a length.out en R)
print(np.linspace(0, 10, 20))   # 20 valores de 0 a 10
## [ 0.          0.52631579  1.05263158  1.57894737  2.10526316  2.63157895
##   3.15789474  3.68421053  4.21052632  4.73684211  5.26315789  5.78947368
##   6.31578947  6.84210526  7.36842105  7.89473684  8.42105263  8.94736842
##   9.47368421 10.        ]
# Secuencia descendente
print(np.arange(10, -1, -1))    # de 10 a 0 bajando de 1 en 1
## [10  9  8  7  6  5  4  3  2  1  0]

Función de repetición

import numpy as np

# Repetir un valor
print([20] * 5)
## [20, 20, 20, 20, 20]
# Repetir cada elemento de un vector (como each en R)
print(np.repeat([21, 23], 3))   # cada valor 3 veces
## [21 21 21 23 23 23]
# Combinar times y each
print(np.tile(np.repeat(["Rojo", "Azul"], 2), 3))
## ['Rojo' 'Rojo' 'Azul' 'Azul' 'Rojo' 'Rojo' 'Azul' 'Azul' 'Rojo' 'Rojo'
##  'Azul' 'Azul']

indexación

# Longitud de una lista
vector = [21, 23, 25, 27, 29]
print(len(vector))
## 5
# Definir vector5
vector5 = [14, 15, 18, 19, 21]

# Extraer el elemento de la posición 1 (⚠️ Python empieza en 0)
print(vector5[0])   # equivalente a vector5[1] en R
## 14
# Extraer el 3er elemento
print(vector5[2])   # equivalente a vector5[3] en R
## 18
# Extraer secuencia de elementos desde 2 hasta 4
print(vector5[1:4])  # indices 1 a 3 en Python → elementos 2 a 4 en R
## [15, 18, 19]
# Extraer el 1, el 3er y el último elemento
print([vector5[0], vector5[2], vector5[-1]])
## [14, 18, 21]
# Extraer en orden inverso (excluyendo el primero)
print(vector5[1:][::-1])   # del segundo al final, pero invertido
## [21, 19, 18, 15]

Matrices y arreglos

import numpy as np

# Generar matriz (llenado por columnas en R, pero en Python por filas es default)
matriz1 = np.arange(1, 17).reshape(4, 4, order='F')  # 'F' = Fortran order (columna primero)
print(matriz1)
## [[ 1  5  9 13]
##  [ 2  6 10 14]
##  [ 3  7 11 15]
##  [ 4  8 12 16]]
# Generar matriz llenando por filas (default en Python)
matriz2 = np.arange(1, 17).reshape(4, 4)  # 'C' order (fila primero)
print(matriz2)
## [[ 1  2  3  4]
##  [ 5  6  7  8]
##  [ 9 10 11 12]
##  [13 14 15 16]]
import numpy as np

# Crear la matriz, llenando por filas (igual que R con byrow=TRUE)
mi_matriz = np.arange(1, 17).reshape(4, 4)
print(mi_matriz)
## [[ 1  2  3  4]
##  [ 5  6  7  8]
##  [ 9 10 11 12]
##  [13 14 15 16]]
import numpy as np

# Creamos la matriz
mi_matriz = np.arange(1, 17).reshape(4, 4)

# Dimensiones
print(mi_matriz.shape)   # (filas, columnas)
## (4, 4)
# Número de filas
print(mi_matriz.shape[0])
## 4
# Número de columnas
print(mi_matriz.shape[1])
## 4
import numpy as np

# Crear matriz (llenado por filas)
matriz2 = np.arange(1, 21).reshape(4, 5)
print(matriz2)
## [[ 1  2  3  4  5]
##  [ 6  7  8  9 10]
##  [11 12 13 14 15]
##  [16 17 18 19 20]]
# Extraer el 7 (fila 2, columna 2 en R → fila 1, columna 1 en Python)
print(matriz2[1, 1])
## 7
# Extraer el 20 (fila 4, columna 5 en R → fila 3, columna 4 en Python)
print(matriz2[3, 4])
## 20

Broadcasting (operaciones)

import numpy as np

# Crear matriz de ejemplo
matriz2 = np.arange(1, 21).reshape(4, 5)

# Sumar un escalar
print(matriz2 + 10)
## [[11 12 13 14 15]
##  [16 17 18 19 20]
##  [21 22 23 24 25]
##  [26 27 28 29 30]]
# Elevar al cuadrado cada elemento
print(matriz2 ** 2)
## [[  1   4   9  16  25]
##  [ 36  49  64  81 100]
##  [121 144 169 196 225]
##  [256 289 324 361 400]]
import numpy as np

# Matriz 4x3 igual que en R (llenado por columnas)
matriz1 = np.arange(1, 13).reshape(4, 3, order='F')
print(matriz1)
## [[ 1  5  9]
##  [ 2  6 10]
##  [ 3  7 11]
##  [ 4  8 12]]
# Matriz 3x4 igual que en R (llenado por columnas)
matriz2 = np.arange(13, 25).reshape(3, 4, order='F')
print(matriz2)
## [[13 16 19 22]
##  [14 17 20 23]
##  [15 18 21 24]]
# Multiplicación matricial
resultado = matriz1 @ matriz2
print(resultado)
## [[218 263 308 353]
##  [260 314 368 422]
##  [302 365 428 491]
##  [344 416 488 560]]

Instalacion de librerias de Python en Rstudio

library(reticulate)
## Warning: package 'reticulate' was built under R version 4.5.1
py_install("pandas")
## Using virtual environment "C:/Users/MARYORIE/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/pfRUvxOCyEethBPJmDtmq" ...
## + "C:/Users/MARYORIE/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/pfRUvxOCyEethBPJmDtmq/Scripts/python.exe" -m pip install --upgrade --no-user pandas
py_install("numpy")
## Using virtual environment "C:/Users/MARYORIE/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/pfRUvxOCyEethBPJmDtmq" ...
## + "C:/Users/MARYORIE/AppData/Local/R/cache/R/reticulate/uv/cache/archive-v0/pfRUvxOCyEethBPJmDtmq/Scripts/python.exe" -m pip install --upgrade --no-user numpy

Data frame

import numpy as np
import pandas as pd

# Crear matriz como en R (llenado por columnas)
matriz = np.arange(1, 17).reshape(4, 4, order='F')

# Convertir a DataFrame
df = pd.DataFrame(matriz, columns=["X1","X2","X3","X4"])
print(df)
##    X1  X2  X3  X4
## 0   1   5   9  13
## 1   2   6  10  14
## 2   3   7  11  15
## 3   4   8  12  16
import pandas as pd

# Crear el DataFrame
df = pd.DataFrame({
    "nombre": ["Maryorie", "Jorge"],
    "edad": [20, 22]
})

print(df)
##      nombre  edad
## 0  Maryorie    20
## 1     Jorge    22
import pandas as pd

# Importar datos desde GitHub
url = "https://github.com/VictorGuevaraP/ME-Machine-Learning/raw/refs/heads/master/Churn.CSV"

df_github = pd.read_csv(url, encoding="latin1", sep=";")

# Mostrar las primeras filas
print(df_github.head())
##    ID.Cliente     Genero  Mayor65años  ... pemanencia ServicioTel Churn
## 0  7590-VHVEG   Femenino            0  ...        1.0          No    No
## 1  5575-GNVDE  Masculino            0  ...       34.0          Si    No
## 2  3668-QPYBK  Masculino            0  ...        2.0          Si    Si
## 3  7795-CFOCW  Masculino            0  ...       45.0         NaN    No
## 4  9237-HQITU   Femenino            0  ...        2.0          Si    Si
## 
## [5 rows x 21 columns]