{"id":727,"date":"2024-03-31T06:38:06","date_gmt":"2024-03-31T06:38:06","guid":{"rendered":"https:\/\/javigomez.org\/?p=727"},"modified":"2025-09-29T20:33:15","modified_gmt":"2025-09-29T20:33:15","slug":"05-01-python-funciones","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/03\/31\/05-01-python-funciones\/","title":{"rendered":"05.01 Python \u2013 Funciones"},"content":{"rendered":"\n<p>Par\u00e1metros de entrada<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def mi_funcion(nombre, apellido):\n    print('saludos desde mi funci\u00f3n')\n    print(f'Nombre: {nombre}, Apellido: {apellido}')\n\nmi_funcion('Juan', 'Perez')\nmi_funcion('Karla','Lara')\n<\/code><\/pre>\n\n\n\n<p>Retornar un valor<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def sumar(a, b):\n    return a + b\n\nresultado = sumar(5, 3)\nprint(f'Resultado sumar: {resultado}')\n# print(f'Resultado sumar: {sumar(5,3}')\n<\/code><\/pre>\n\n\n\n<p>Argumentos por defecto<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># def sumar(a:int = 0, b:int = 0) -> int:\ndef sumar(a = 0, b = 0):\n    return a + b\n\nresultado = sumar()\nprint(f'Resultado sumar: {resultado}')\n\nprint(f'Resultado sumar: {sumar(2,8)}')<\/code><\/pre>\n\n\n\n<p>Argumentos variables<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def mi_funcion(*args):\n    for arg in args:\n        print(arg)\n\nmi_funcion(1, 2, 3)  # Resultado: 1, 2, 3\nmi_funcion('a', 'b', 'c', 'd')  # Resultado: 'a', 'b', 'c', 'd<\/code><\/pre>\n\n\n\n<p>Argumentos variables key-value<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def listarTerminos(**terminos):\n    for llave, valor in terminos.items():\n        print(f'{llave}: {valor}\u2019)\n    print(terminos&#91;\u201cIDE\u201d])\n\nlistarTerminos(IDE='Integrated Developement Environment', PK='Primary Key')\nlistarTerminos(DBMS='Database Management System')<\/code><\/pre>\n\n\n\n<p>Argumentos combinados<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def funcion_combinada(a, b, *args, **kwargs):\n    print(f\"a: {a}\")\n    print(f\"b: {b}\")\n    print(\"Argumentos posicionales:\")\n    for arg in args:\n        print(arg)\n    print(\"Argumentos clave-valor:\")\n    for clave, valor in kwargs.items():\n        print(f\"{clave}: {valor}\")\n\n# Ejemplo de uso\nfuncion_combinada(1, 2, 3, 4, nombre=\"Juan\", edad=30, ciudad=\"Madrid\")\n<\/code><\/pre>\n\n\n\n<p>Lista o tupla como argumento<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def desplegarNombres(nombres):\n    for nombre in nombres:\n        print(nombre)\n\nnombres = &#91;'Carlos', 'Lara', 'Maria']\ndesplegarNombres(nombres)\nprint('***')\ndesplegarNombres('Lara')\nprint('***')\ndesplegarNombres((8, 9)) #tupla\nprint('***')\ndesplegarNombres(&#91;10, 15]) #lista\n<\/code><\/pre>\n\n\n\n<p>Funciones recursivas<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def factorial(numero):\n\u00a0 \u00a0 if numero == 1:\n\u00a0 \u00a0 \u00a0 \u00a0 return 1\n\u00a0 \u00a0 else:\n\u00a0 \u00a0 \u00a0 \u00a0 return numero * factorial(numero-1)\n\u000bnumero = 5\nresultado = factorial(numero)\nprint(f'El factorial de {numero} es {resultado}')\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PYTHON U05 01 Funciones\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/ihXXvZFZ9Lk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Par\u00e1metros de entrada Retornar un valor Argumentos por defecto Argumentos variables Argumentos variables key-value Argumentos combinados Lista o tupla como argumento Funciones recursivas<\/p>\n","protected":false},"author":1,"featured_media":728,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[167,277],"class_list":["post-727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-funciones","tag-python"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=727"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/727\/revisions\/863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/728"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}