Muestra las diferencias entre dos versiones de la página.
Próxima revisión | Revisión previa | ||
modelado:introduccion_python [2024/01/25 09:19] – creado thejuanvisu | modelado:introduccion_python [2024/02/01 08:39] (actual) – thejuanvisu | ||
---|---|---|---|
Línea 1: | Línea 1: | ||
====== Introducción a python ====== | ====== Introducción a python ====== | ||
+ | |||
+ | ===== Variables ===== | ||
+ | Python no es un lenguaje fuertemente tipado, por lo que muchas veces para obtener una variable simplemente se pone el nombre seguido de un igual y el valor: | ||
+ | <code python> | ||
+ | variable = " | ||
+ | numero = 1 | ||
+ | |||
+ | #Para saber el tipo de dato de una variable usamos type: | ||
+ | print(type(variable), | ||
+ | </ | ||
+ | |||
+ | ===== Arrays ===== | ||
+ | En python se puede cargar casi cualquier valor a un array independientemente del tipo, por lo que hay que tener cuidado. | ||
+ | <code python> | ||
+ | Array = [1,2,3,4] #Array standard | ||
+ | ArrayMultiTipo = [1, 2, " | ||
+ | |||
+ | DelUnoAl100 = [x for x in range (1000)] #Array compuesto por los números del 1 al 1000 | ||
+ | De2en2 = [x fo x in range(0, | ||
+ | # | ||
+ | |||
+ | </ | ||
+ | |||
+ | Para el manejo de los array tenemos los siguientes ejemplos: | ||
+ | <code python> | ||
+ | array = [x for x in range (0, | ||
+ | size = len(array) #Obtenemos el tamaño del array | ||
+ | |||
+ | print(array[: | ||
+ | print(array[-10: | ||
+ | print(array[5: | ||
+ | print(array[85: | ||
+ | |||
+ | for value in array[:: | ||
+ | print(value) | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Comentarios ===== | ||
+ | |||
+ | Para hacer comentarios utilizamos # para una sola línea y 3 comillas (""" | ||
+ | <code python> | ||
+ | #Comentario de una línea | ||
+ | |||
+ | """ | ||
+ | Comentario | ||
+ | multi | ||
+ | línea | ||
+ | """ | ||
+ | </ | ||
===== Imprimir en pantalla ===== | ===== Imprimir en pantalla ===== | ||
Línea 16: | Línea 66: | ||
<code python> | <code python> | ||
a = 6//7 | a = 6//7 | ||
+ | </ | ||
+ | |||
+ | ===== Condicionales ===== | ||
+ | En python tenemos el condicional if que funciona de forma similar a otros lenguajes de programación. OJO: Aquí no hay llaves se hace todo con tabulaciones. | ||
+ | <code python> | ||
+ | if (variable == true): | ||
+ | print(" | ||
+ | else: | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ===== Bucle For ===== | ||
+ | <code python> | ||
+ | #ejemplo de uso del bucle for para recorrer un array: | ||
+ | Array = [1,2,3,4] | ||
+ | for x in Array: | ||
+ | print(x) | ||
+ | </ | ||
+ | |||
+ | ===== Bucle While ===== | ||
+ | |||
+ | <code python> | ||
+ | y = true | ||
+ | while(y): | ||
+ | print(1) | ||
+ | y=False | ||
</ | </ | ||
Línea 23: | Línea 99: | ||
<code python> | <code python> | ||
- | def funcion(parametroA, | + | #Sin definir tipos de datos que recibe y devuelve: |
+ | def funcion(parametroA, | ||
a = " | a = " | ||
print(" | print(" | ||
return a | return a | ||
+ | | ||
+ | #Definiendo el tipo de datos que debe recibir y devolver: | ||
+ | def nombre(par_1: | ||
+ | print(type(par_1)) | ||
+ | return par_1, par_2 | ||
</ | </ | ||
Línea 34: | Línea 116: | ||
return 1,2 | return 1,2 | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Diccionarios en Python ===== | ||
+ | Se utilizan para almacenar datos. | ||
+ | <code python> | ||
+ | dicc = dict{ | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | print(dicc)# | ||
+ | |||
+ | #Para recorrer las claves del diccionario: | ||
+ | for key, value in dicc.items(): | ||
+ | print(f" | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ |