{"id":14187,"date":"2023-03-25T18:01:39","date_gmt":"2023-03-25T12:31:39","guid":{"rendered":"https:\/\/savewithnerds.com\/?p=14187"},"modified":"2024-03-16T14:26:45","modified_gmt":"2024-03-16T08:56:45","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/savewithnerds.com\/es\/python-functions\/","title":{"rendered":"Equ\u00edpate con estas funciones de Python"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">En Python, las funciones se pueden llamar varias veces desde diferentes partes del programa, lo que hace que el c\u00f3digo sea m\u00e1s modular y m\u00e1s f\u00e1cil de mantener. Se definen mediante la palabra clave def, seguida del nombre de la funci\u00f3n y un conjunto de par\u00e9ntesis que contienen los par\u00e1metros de la funci\u00f3n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">La sintaxis de una funci\u00f3n de Python es sencilla. Comienza con la palabra clave def, seguida del nombre de la funci\u00f3n y un conjunto de par\u00e9ntesis que pueden contener cero o m\u00e1s par\u00e1metros. El cuerpo de la funci\u00f3n est\u00e1 sangrado y debe contener al menos una instrucci\u00f3n. <\/span><\/p>\n\t\t<div class=\"web-stories-list alignnone has-archive-link is-view-type-circles is-style-default is-carousel\" data-id=\"1\">\n\t\t\t<div\n\t\t\tclass=\"web-stories-list__inner-wrapper carousel-1\"\n\t\t\tstyle=\"--ws-circle-size:100px\"\n\t\t\t>\n\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\tclass=\"web-stories-list__carousel circles\"\n\t\t\t\t\tdata-id=\"carousel-1\"\n\t\t\t\t\tdata-prev=\"Anterior\"\n\t\t\t\t\tdata-next=\"Siguiente\"\n\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<div\n\t\t\t\tclass=\"web-stories-list__story\"\n\t\t\t\tdata-wp-interactive=\"web-stories-block\"\n\t\t\t\tdata-wp-context='{\"instanceId\":1}'\t\t\t\tdata-wp-on--click=\"actions.open\"\n\t\t\t\tdata-wp-on-window--popstate=\"actions.onPopstate\"\n\t\t\t\t>\n\t\t\t\t\t\t\t<div class=\"web-stories-list__story-poster\">\n\t\t\t\t<a href=\"https:\/\/savewithnerds.com\/es\/web-stories\/wiper-blades\/\" >\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More.webp\"\n\t\t\t\t\t\talt=\"Wiper Blades: Types, How To Change, Where To Buy, And Much More\"\n\t\t\t\t\t\twidth=\"185\"\n\t\t\t\t\t\theight=\"308\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More.webp 640w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More-225x300.webp 225w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More-9x12.webp 9w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More-150x200.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/09\/cropped-Wiper-Blades-Types-How-To-Change-Where-To-Buy-And-Much-More-450x600.webp 450w\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tsizes=\"auto, (max-width: 640px) 100vw, 640px\"\n\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<div\n\t\t\t\tclass=\"web-stories-list__story\"\n\t\t\t\tdata-wp-interactive=\"web-stories-block\"\n\t\t\t\tdata-wp-context='{\"instanceId\":1}'\t\t\t\tdata-wp-on--click=\"actions.open\"\n\t\t\t\tdata-wp-on-window--popstate=\"actions.onPopstate\"\n\t\t\t\t>\n\t\t\t\t\t\t\t<div class=\"web-stories-list__story-poster\">\n\t\t\t\t<a href=\"https:\/\/savewithnerds.com\/es\/web-stories\/why-blend-the-rules-eyeshadow-palette-a-must-have\/\" >\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2022\/11\/cropped-Untitled-design.webp\"\n\t\t\t\t\t\talt=\"Why Blend The Rules Eyeshadow Palette A Must-Have?\"\n\t\t\t\t\t\twidth=\"185\"\n\t\t\t\t\t\theight=\"308\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2022\/11\/cropped-Untitled-design.webp 640w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2022\/11\/cropped-Untitled-design-225x300.webp 225w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2022\/11\/cropped-Untitled-design-150x200.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2022\/11\/cropped-Untitled-design-450x600.webp 450w\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tsizes=\"auto, (max-width: 640px) 100vw, 640px\"\n\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<div\n\t\t\t\tclass=\"web-stories-list__story\"\n\t\t\t\tdata-wp-interactive=\"web-stories-block\"\n\t\t\t\tdata-wp-context='{\"instanceId\":1}'\t\t\t\tdata-wp-on--click=\"actions.open\"\n\t\t\t\tdata-wp-on-window--popstate=\"actions.onPopstate\"\n\t\t\t\t>\n\t\t\t\t\t\t\t<div class=\"web-stories-list__story-poster\">\n\t\t\t\t<a href=\"https:\/\/savewithnerds.com\/es\/web-stories\/unveiling-the-ultimate-bargains-top-us-budget-smartphones-of-2023\/\" >\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4.webp\"\n\t\t\t\t\t\talt=\"Unveiling the Ultimate Bargains: Top US Budget Smartphones of 2023\"\n\t\t\t\t\t\twidth=\"185\"\n\t\t\t\t\t\theight=\"308\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4.webp 640w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4-225x300.webp 225w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4-9x12.webp 9w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4-150x200.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/08\/7-4-450x600.webp 450w\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tsizes=\"auto, (max-width: 640px) 100vw, 640px\"\n\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<div\n\t\t\t\tclass=\"web-stories-list__story\"\n\t\t\t\tdata-wp-interactive=\"web-stories-block\"\n\t\t\t\tdata-wp-context='{\"instanceId\":1}'\t\t\t\tdata-wp-on--click=\"actions.open\"\n\t\t\t\tdata-wp-on-window--popstate=\"actions.onPopstate\"\n\t\t\t\t>\n\t\t\t\t\t\t\t<div class=\"web-stories-list__story-poster\">\n\t\t\t\t<a href=\"https:\/\/savewithnerds.com\/es\/web-stories\/unveiling-the-best-sleeping-bags-for-every-camping-adventure\/\" >\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2.webp\"\n\t\t\t\t\t\talt=\"Unveiling the Best Sleeping Bags for Every Camping Adventure\"\n\t\t\t\t\t\twidth=\"185\"\n\t\t\t\t\t\theight=\"308\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2.webp 640w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2-225x300.webp 225w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2-9x12.webp 9w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2-150x200.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/06\/5-2-450x600.webp 450w\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tsizes=\"auto, (max-width: 640px) 100vw, 640px\"\n\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<div\n\t\t\t\tclass=\"web-stories-list__story\"\n\t\t\t\tdata-wp-interactive=\"web-stories-block\"\n\t\t\t\tdata-wp-context='{\"instanceId\":1}'\t\t\t\tdata-wp-on--click=\"actions.open\"\n\t\t\t\tdata-wp-on-window--popstate=\"actions.onPopstate\"\n\t\t\t\t>\n\t\t\t\t\t\t\t<div class=\"web-stories-list__story-poster\">\n\t\t\t\t<a href=\"https:\/\/savewithnerds.com\/es\/web-stories\/unveiling-the-advantages-of-investing-in-high-quality-health-equipment-for-your-gym\/\" >\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5.webp\"\n\t\t\t\t\t\talt=\"Unveiling the Advantages of Investing in High-Quality Health Equipment for Your Gym\"\n\t\t\t\t\t\twidth=\"185\"\n\t\t\t\t\t\theight=\"308\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5.webp 640w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5-225x300.webp 225w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5-9x12.webp 9w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5-150x200.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/07\/7-5-450x600.webp 450w\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tsizes=\"auto, (max-width: 640px) 100vw, 640px\"\n\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<div tabindex=\"0\" aria-label=\"Anterior\" class=\"glider-prev\"><\/div>\n\t\t\t\t\t<div tabindex=\"0\" aria-label=\"Siguiente\" class=\"glider-next\"><\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\n<h2><span style=\"font-weight: 400;\">Definici\u00f3n de una funci\u00f3n de Python<\/span><\/h2>\n<figure id=\"attachment_14192\" aria-describedby=\"caption-attachment-14192\" style=\"width: 900px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14192\" src=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon.webp\" alt=\"Defining a Python function\" width=\"900\" height=\"500\" srcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon.webp 900w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon-300x167.webp 300w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon-768x427.webp 768w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon-18x10.webp 18w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon-150x83.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/defining-a-python-function-11zon-450x250.webp 450w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption id=\"caption-attachment-14192\" class=\"wp-caption-text\">Definici\u00f3n de una funci\u00f3n de Python | guardarconnerds<\/figcaption><\/figure>\n<p><span style=\"font-weight: 400;\">Es un c\u00f3digo que se puede reutilizar para realizar una determinada operaci\u00f3n y se conoce como funci\u00f3n. \u00c9l <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">toma entrada, realiza operaciones<\/span><\/a><span style=\"font-weight: 400;\"> en \u00e9l, y devuelve la salida. En Python, las funciones se definen mediante la palabra clave &quot;def&quot;, seguida del nombre de la funci\u00f3n, par\u00e9ntesis y dos puntos. La sintaxis para definir un <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">funci\u00f3n de pit\u00f3n<\/span><\/a><span style=\"font-weight: 400;\"> es como sigue:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def nombre_funci\u00f3n(argumentos):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0declaraciones)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0expresi\u00f3n de retorno<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Analicemos la sintaxis y comprendamos cada elemento.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201cdef\u201d: Es una palabra clave que le dice a Python que estamos definiendo una funci\u00f3n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201cfunction_name\u201d: Es el nombre de la funci\u00f3n, el cual debe ser descriptivo y significativo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201cargumentos\u201d: Estos son los <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">entradas o par\u00e1metros<\/span><\/a><span style=\"font-weight: 400;\"> pasado a la funci\u00f3n. Somos libres de elegir cualquier n\u00famero de caracter\u00edsticas.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201cdos puntos\u201d: Significa el final del encabezado de la funci\u00f3n y el comienzo del cuerpo de la funci\u00f3n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201cstatement(s)\u201d: Es un conjunto de sentencias que realizan algunas operaciones sobre las entradas o variables.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u201creturn\u201d: Especifica la salida de la funci\u00f3n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Una vez que definimos una funci\u00f3n, podemos llamarla desde cualquier parte de nuestro c\u00f3digo pasando el <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">argumentos requeridos<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<hr \/>\n<h2><span style=\"font-weight: 400;\">Par\u00e1metros de la funci\u00f3n Python<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Las funciones de Python pueden tener <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">m\u00faltiples par\u00e1metros<\/span><\/a><span style=\"font-weight: 400;\">, y podemos pasarlos de dos maneras: argumentos posicionales y de palabras clave. Los argumentos posicionales se pasan en el orden en que aparecen en la definici\u00f3n de la funci\u00f3n, mientras que los argumentos de palabra clave se pasan con el nombre del par\u00e1metro seguido de dos puntos y el valor. Veamos un ejemplo de ambos tipos de argumentos.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def suma_n\u00fameros(x, y):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0suma = x + y<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0suma devuelta<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Argumentos posicionales #<\/span><\/p>\n<p><span style=\"font-weight: 400;\">resultado = sumar_numeros(3, 5)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">imprimir (resultado) # Salida: 8<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Argumentos de palabras clave #<\/span><\/p>\n<p><span style=\"font-weight: 400;\">resultado = suma_numeros(x=3, y=5)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">imprimir (resultado) # Salida: 8<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En el ejemplo anterior, definimos una funci\u00f3n &quot;add_numbers&quot; que toma dos par\u00e1metros, &quot;x&quot; e &quot;y&quot;, y devuelve su suma. Llamamos a esta funci\u00f3n dos veces: una vez con argumentos posicionales y otra vez con argumentos de palabras clave. Ambos caminos dieron como resultado <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">la misma salida<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<hr \/>\n<h2><span style=\"font-weight: 400;\">C\u00f3mo llamar a una funci\u00f3n de Python<\/span><\/h2>\n<figure id=\"attachment_14193\" aria-describedby=\"caption-attachment-14193\" style=\"width: 900px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14193\" src=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon.webp\" alt=\"How to Call a Python function\" width=\"900\" height=\"500\" srcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon.webp 900w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon-300x167.webp 300w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon-768x427.webp 768w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon-18x10.webp 18w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon-150x83.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/how-to-call-a-python-function-11zon-450x250.webp 450w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption id=\"caption-attachment-14193\" class=\"wp-caption-text\">C\u00f3mo llamar a una funci\u00f3n de Python | guardarconnerds<\/figcaption><\/figure>\n<p><span style=\"font-weight: 400;\">Se puede crear una funci\u00f3n en Python escribiendo primero la palabra clave def, luego escribiendo el nombre de la funci\u00f3n y finalmente escribiendo par\u00e9ntesis. El c\u00f3digo de Python que forma el bloque de c\u00f3digo que especifica la funci\u00f3n est\u00e1 sangrado y puede ser cualquier cosa que sea legal en Python. La siguiente es una ilustraci\u00f3n de un <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">funci\u00f3n directa<\/span><\/a><span style=\"font-weight: 400;\"> que acepta dos par\u00e1metros y genera la suma de esos par\u00e1metros:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def sumar_n\u00fameros(a, b):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0devolver a + b<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Para llamar a esta funci\u00f3n, debemos proporcionar los valores de los par\u00e1metros a y b. Podemos hacer esto pasando los valores como argumentos a la funci\u00f3n, como se muestra a continuaci\u00f3n:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">resultado = sumar_numeros(5, 10)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">imprimir (resultado) # Salida: 15<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En este ejemplo, llamamos a la funci\u00f3n add_numbers con los argumentos 5 y 10. La funci\u00f3n devuelve la suma de estos dos valores, que se almacena en la variable de resultado. Luego imprimimos el valor del resultado, que es 15.<\/span><\/p>\n<hr \/>\n<h3><span style=\"font-weight: 400;\">Par\u00e1metros posicionales en funciones de Python<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">En las funciones de Python, los par\u00e1metros pueden ser posicionales o de palabras clave. Los par\u00e1metros posicionales son el tipo m\u00e1s com\u00fan de par\u00e1metros y se definen en el encabezado de la funci\u00f3n. Al llamar a una funci\u00f3n con <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">par\u00e1metros posicionales<\/span><\/a><span style=\"font-weight: 400;\">, los valores se pasan en el orden en que aparecen en el encabezado de la funci\u00f3n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Aqu\u00ed hay un ejemplo de una funci\u00f3n con dos par\u00e1metros posicionales:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def saludar(nombre, mensaje):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0imprimir(f\u201d{mensaje}, {nombre}!\u201d)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Para llamar a esta funci\u00f3n, debemos proporcionar los valores de los par\u00e1metros de nombre y mensaje en el orden en que aparecen en la definici\u00f3n de la funci\u00f3n:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">saludar(\u201cAlice\u201d, \u201cHola\u201d) # Salida: \u00a1Hola, Alice!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En este ejemplo, estamos llamando a la funci\u00f3n de saludo con los argumentos &quot;Alice&quot; y &quot;Hello&quot;. La funci\u00f3n imprime el mensaje &quot;\u00a1Hola, Alicia!&quot; a la consola<\/span><\/p>\n<hr \/>\n<h3><span style=\"font-weight: 400;\">Par\u00e1metros de palabras clave en funciones de Python<\/span><\/h3>\n<figure id=\"attachment_14191\" aria-describedby=\"caption-attachment-14191\" style=\"width: 900px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-14191\" src=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon.webp\" alt=\"Keyword parameters in Python functions\" width=\"900\" height=\"500\" srcset=\"https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon.webp 900w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon-300x167.webp 300w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon-768x427.webp 768w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon-18x10.webp 18w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon-150x83.webp 150w, https:\/\/savewithnerds.com\/wp-content\/uploads\/2023\/03\/keyword-parameters-in-python-functions-11zon-450x250.webp 450w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption id=\"caption-attachment-14191\" class=\"wp-caption-text\">Par\u00e1metros de palabras clave en funciones de Python | guardarconnerds<\/figcaption><\/figure>\n<p><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">Par\u00e1metros de palabras clave<\/span><\/a><span style=\"font-weight: 400;\"> son otro tipo de par\u00e1metro en las funciones de Python. Se definen mediante la sintaxis nombre_par\u00e1metro=valor_predeterminado en el encabezado de la funci\u00f3n. Al llamar a una funci\u00f3n con par\u00e1metros de palabras clave, podemos pasar los valores en cualquier orden y especificar el nombre del par\u00e1metro.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Aqu\u00ed hay un ejemplo de una funci\u00f3n con dos par\u00e1metros de palabras clave:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">def calcular_descuento(precio, tasa_descuento=0.1):<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0cantidad_descuento = precio * tasa_descuento<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0descuento_precio = precio \u2013 descuento_cantidad<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0volver precio_rebajado<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En este ejemplo, la funci\u00f3n calcular_descuento toma dos par\u00e1metros: precio y tasa_descuento. El par\u00e1metro tasa_descuento tiene un valor predeterminado de 0,1, lo que significa que si no pasamos un valor para este par\u00e1metro, se establecer\u00e1 de forma predeterminada en 0,1.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Podemos llamar a esta funci\u00f3n con el par\u00e1metro de precio y anular el valor predeterminado de tasa_descuento usando la sintaxis de palabra clave:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">precio = 100<\/span><\/p>\n<p><span style=\"font-weight: 400;\">precio_rebajado = calcular_descuento(precio, tasa_descuento=0.2)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">imprimir (precio_con descuento) # Salida: 80.0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En este ejemplo, estamos llamando a la funci\u00f3n calcular_descuento con el par\u00e1metro de precio establecido en 100 y el par\u00e1metro tasa_descuento establecido en 0,2. La funci\u00f3n devuelve el precio con descuento, que es 80,0.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Conclusi\u00f3n<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">pit\u00f3n es un <\/span><a href=\"https:\/\/savewithnerds.com\/es\/v9kw\/\" target=\"_blank\" rel=\"nofollow noopener sponsored\"><span style=\"font-weight: 400;\">lenguaje de programaci\u00f3n vers\u00e1til<\/span><\/a><span style=\"font-weight: 400;\"> con una amplia gama de funcionalidades. Una de las caracter\u00edsticas m\u00e1s poderosas de Python es la capacidad de definir y usar funciones. Las funciones en Python son bloques de c\u00f3digo aut\u00f3nomos que se pueden reutilizar en un programa. Hay varios tipos de <a href=\"https:\/\/en.wikipedia.org\/wiki\/Python_(programming_language)\" target=\"_blank\" rel=\"noopener\">Funciones de Python<\/a>, cada uno con sus caracter\u00edsticas \u00fanicas. Para m\u00e1s informaci\u00f3n visite <\/span><a href=\"https:\/\/savewithnerds.com\/es\/\"><span style=\"font-weight: 400;\">GuardarConNerds.<\/span><\/a><\/p>\n<h2><span style=\"font-weight: 400;\">preguntas frecuentes<\/span><\/h2>\n<div class=\"su-accordion su-u-trim\"><div class=\"su-spoiler su-spoiler-style-default su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>\u00bfLos cuatro tipos de funciones de Python?<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p>Hay cuatro tipos de funciones en Python<\/p>\n<p>Funciones integradas<br \/>\nFunciones definidas por el usuario<br \/>\npit\u00f3n de la funci\u00f3n lambda<br \/>\nFunciones recursivas<\/p>\n<\/div><\/div> <div class=\"su-spoiler su-spoiler-style-default su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>\u00bfQu\u00e9 es una funci\u00f3n en Python?<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p>En el lenguaje de programaci\u00f3n Python, una &quot;funci\u00f3n&quot; es una secci\u00f3n discreta de c\u00f3digo que realiza una tarea espec\u00edfica y se puede &quot;llamar&quot; varias veces para realizar la misma acci\u00f3n. Puede darle a su programa m\u00e1s versatilidad y aumentar su potencial de reutilizaci\u00f3n incorpor\u00e1ndole funciones.<\/p>\n<p>&nbsp;<\/p>\n<p>D\u00e9 un ejemplo de lo que es una funci\u00f3n en Python.<\/p>\n<p>Una pieza de c\u00f3digo que se utiliza para llevar a cabo una operaci\u00f3n particular se conoce como funci\u00f3n.<\/p>\n<p>Ejemplo:<\/p>\n<p>definitivamente saludar():<\/p>\n<p>imprimir(&#039;\u00a1Hola mundo!&#039;)<\/p>\n<p>saludar()<\/p>\n<p>Salida #: \u00a1Hola mundo!<\/p>\n<\/div><\/div> <div class=\"su-spoiler su-spoiler-style-default su-spoiler-icon-plus su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>\u00bfC\u00f3mo escribir una funci\u00f3n de Python?<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p>El nombre de una funci\u00f3n de Python va seguido de la palabra clave def y luego se encierra entre par\u00e9ntesis cuando se crea una funci\u00f3n en Python. ( ). Los par\u00e9ntesis deben usarse para encerrar cualquier argumento o especificaci\u00f3n que se reciba como entrada. Adem\u00e1s, tiene la opci\u00f3n de definir par\u00e1metros dentro de estos par\u00e9ntesis.<\/p>\n<p>La cadena de documentaci\u00f3n de una funci\u00f3n, tambi\u00e9n conocida como docstring, puede ser una declaraci\u00f3n discrecional que viene antes de la primera declaraci\u00f3n de una funci\u00f3n.<\/p>\n<p>Todos y cada uno de los bloques de c\u00f3digo de una funci\u00f3n comienzan con dos puntos (:), y todos est\u00e1n subrayados.<\/p>\n<p>Cuando deja una funci\u00f3n con la instrucci\u00f3n return [expresi\u00f3n], puede optar por devolver una expresi\u00f3n a la persona que llam\u00f3 a la funci\u00f3n. Una declaraci\u00f3n de retorno que no toma ning\u00fan par\u00e1metro se comporta exactamente igual que una declaraci\u00f3n de retorno Ninguno.<\/p>\n<\/div><\/div><\/div>\n<p>&nbsp;<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Python's four function types?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"There are four function types in Python\n1. Built-in Functions\n2. User-defined Functions\n3. lambda function python\n4. Recursive Functions\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is a function in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In the programming language Python, a \\\"function\\\" is a discrete section of code that performs a specific task and can be \\\"called\\\" numerous times to carry out the same action. You can give your program more versatility and increase its potential for reuse by incorporating functions into it.<\/p>\n<p>Give an example of what a function is in Python.\nA piece of code that is used to carry out a particular operation is known as a function. \nExample: \ndef greet(): \n    print('Hello World!') \ngreet() \n# Output: Hello World!\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to write a Python function?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The name of a Python function is followed by the keyword def and then enclosed in parentheses when a function is created in Python. ( ). The parentheses should be used to enclose any arguments or specifications that are received as input. Additionally, you have the option of defining parameters inside these parentheses.\nThe documentation string of a function, also known as the docstring, can be a discretionary statement that comes before the first statement of a function.\nEach and every one of a function's code blocks begin with a colon (:), and they are all underlined.\nWhen you leave a function with the statement return [expression], you can choose to return an expression to the person who called the function. A return statement that does not take any parameters behaves exactly the same as a return None statement.\"\n    }\n  }]\n}\n<\/script><\/p>","protected":false},"excerpt":{"rendered":"<p>In Python, functions can be called multiple times from different parts of the program, making the code more modular and easier to maintain. They are defined using the def keyword, followed by the function name and a set of parentheses containing the function parameters. The syntax of a Python function is straightforward. It starts with<\/p>","protected":false},"author":2,"featured_media":14190,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[426,1870],"tags":[705],"class_list":{"0":"post-14187","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-tech-software","8":"category-datacamp","9":"tag-python-functions"},"_links":{"self":[{"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/posts\/14187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/comments?post=14187"}],"version-history":[{"count":0,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/posts\/14187\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/media\/14190"}],"wp:attachment":[{"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/media?parent=14187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/categories?post=14187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savewithnerds.com\/es\/wp-json\/wp\/v2\/tags?post=14187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}