|
| 1 | +# Funciones basicas sin retorno |
| 2 | +def saludo(): |
| 3 | + print("Hola mundo") |
| 4 | + |
| 5 | + |
| 6 | +saludo() |
| 7 | + |
| 8 | +# Funcion con retorno |
| 9 | + |
| 10 | + |
| 11 | +def saludo_retorno(): |
| 12 | + return "Hola mundo" |
| 13 | + |
| 14 | + |
| 15 | +print(saludo_retorno()) |
| 16 | + |
| 17 | +# Con un argumento |
| 18 | + |
| 19 | + |
| 20 | +def arg_saludo(nombre): |
| 21 | + print(f"Hola {nombre}") |
| 22 | + |
| 23 | + |
| 24 | +print(arg_saludo("Juan")) |
| 25 | + |
| 26 | +# Con argumentos |
| 27 | + |
| 28 | + |
| 29 | +def argts_saludo(nombre, apellido): |
| 30 | + print(f"hola {nombre} {apellido}") |
| 31 | + |
| 32 | + |
| 33 | +argts_saludo("Juan", "Yesca") |
| 34 | + |
| 35 | +# Con argumentos predefinidos |
| 36 | + |
| 37 | + |
| 38 | +def arg_definido(nombre="Juan"): |
| 39 | + print(f"Hola {nombre}") |
| 40 | + |
| 41 | + |
| 42 | +arg_definido() |
| 43 | +arg_definido("Juanito") |
| 44 | + |
| 45 | +# Con argumento y retorno |
| 46 | + |
| 47 | + |
| 48 | +def ret_arg(nombre, apellido): |
| 49 | + return f"Hola {nombre} {apellido}" |
| 50 | + |
| 51 | + |
| 52 | +print(ret_arg("Juan", "Yesca")) |
| 53 | + |
| 54 | +# Con un numero variable de argumentos |
| 55 | + |
| 56 | + |
| 57 | +def num_arg(*nombres): |
| 58 | + for nombre in nombres: |
| 59 | + print(f"Hola {nombre}") |
| 60 | + |
| 61 | + |
| 62 | +num_arg("Juan", "Yesca", "Pedro", "Maria") |
| 63 | + |
| 64 | + |
| 65 | +""" |
| 66 | +Funciones dentro de funciones |
| 67 | +""" |
| 68 | + |
| 69 | + |
| 70 | +def funcion_out(): |
| 71 | + def funcion_in(): |
| 72 | + print("Hola mundo") |
| 73 | + funcion_in() |
| 74 | + |
| 75 | + |
| 76 | +funcion_out() |
| 77 | + |
| 78 | +# Funciones del lenguaje |
| 79 | +print(len("Juan")) |
| 80 | +print(type("Juan")) |
| 81 | +print("Juan".upper()) |
| 82 | + |
| 83 | +""" |
| 84 | +Variables globales y locales |
| 85 | +""" |
| 86 | +var_global = "Juan" |
| 87 | + |
| 88 | + |
| 89 | +def hola_juan(): |
| 90 | + var_local = "Juanito" |
| 91 | + print(f"{var_global}, {var_local}") |
| 92 | + |
| 93 | + |
| 94 | +print(var_global) |
| 95 | +hola_juan() |
| 96 | + |
| 97 | + |
| 98 | +""" |
| 99 | +Extra |
| 100 | +""" |
| 101 | + |
| 102 | + |
| 103 | +def print_numeros(texzto1, texto2) -> int: |
| 104 | + count = 0 |
| 105 | + for numeros in range(1, 101): |
| 106 | + if numeros % 3 == 0 and numeros % 5 == 0: |
| 107 | + print(texzto1 + texto2) |
| 108 | + elif numeros % 3 == 0: |
| 109 | + print(texzto1) |
| 110 | + elif numeros % 5 == 0: |
| 111 | + print(texto2) |
| 112 | + else: |
| 113 | + print(numeros) |
| 114 | + count += 1 |
| 115 | + return count |
| 116 | + |
| 117 | + |
| 118 | +print(print_numeros("Fizz", "Buzz")) |
0 commit comments