{"id":34790,"date":"2021-05-27T10:49:29","date_gmt":"2021-05-27T08:49:29","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=34790"},"modified":"2021-05-27T11:05:29","modified_gmt":"2021-05-27T09:05:29","slug":"how-to-make-a-function-in-python","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/como-hacer-una-funcion-en-python\/","title":{"rendered":"My First Function in Python"},"content":{"rendered":"<p>A few weeks ago we talked to you about the <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/phyton-predefined-functions\/\">Predefined functions in Python<\/a>, are those that<strong> they come integrated <\/strong>and they help us list, add, print on the screen, etc.<\/p>\n\n\n\n<p>But sometimes we&#039;re going to need a function to do something<strong> specific<\/strong> outside of the predefined ones and we will have to create it ourselves, so let&#039;s see how it is done.<\/p>\n\n\n\n<p>Remember that you can continue learning about programming, in &nbsp;<a href=\"https:\/\/eiposgrados.com\/eng\/\">EIP<\/a>&nbsp;with our&nbsp;<a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\">master in Advanced Programming in Python for Big Data, Hacking and Machine Learning<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a function in Python<\/h2>\n\n\n\n<p>To create a function we start from the instruction <strong><em>def<\/em><\/strong><em>, <\/em>Let us remember that this word is included within the <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/python-reserved-words\/\">Keywords<\/a> of Python, followed by the name of the function and ending it with a colon &#039; <strong>:<\/strong> &#039;.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"126\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-en-python-1024x126.png\" alt=\"\" class=\"wp-image-34791\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-en-python-1024x126.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-en-python-300x37.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-en-python-768x94.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-en-python.png 1402w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>For example, we are going to make our first function print the typical <strong>&quot;Hello World&quot;<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"210\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/primera-funcion-con-python-1024x210.png\" alt=\"\" class=\"wp-image-34792\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/primera-funcion-con-python-1024x210.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/primera-funcion-con-python-300x62.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/primera-funcion-con-python-768x158.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/primera-funcion-con-python.png 1482w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>First we define in the <strong>function a print<\/strong> with Hello World and then we invoke it to print the message, remember that if the function is not called it will never do anything.<\/p>\n\n\n\n<p>We can also make that function <strong>return <\/strong>a data to later assign it to some variable.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"255\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-programando-con-python-1024x255.png\" alt=\"\" class=\"wp-image-34794\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-programando-con-python-1024x255.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-programando-con-python-300x75.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-programando-con-python-768x191.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/mi-primera-funcion-programando-con-python.png 1444w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>Finally, so as not to extend ourselves, we are going to see the<strong> functions with parameters<\/strong>, these functions receive parameters that we have previously defined to execute the corresponding code.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"218\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/funcion-con-python-1024x218.png\" alt=\"\" class=\"wp-image-34795\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/funcion-con-python-1024x218.png 1024w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/funcion-con-python-300x64.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/funcion-con-python-768x164.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/05\/funcion-con-python.png 1436w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>The parameters that our function receives can be any type of data, numbers, letters, lists, etc. But we will see that another time.<\/p>\n\n\n\n<p>Next week we will use what we learned to update our <a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/how-to-make-python-calculator\/\">Calculator in Python<\/a> and make it a little more efficient.<\/p>","protected":false},"excerpt":{"rendered":"<p>From EIP we explain how to make a function in python and everything you need to know. Do you know functions with parameters? We explain it to you!<\/p>","protected":false},"author":51,"featured_media":34839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-34790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/34790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=34790"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/34790\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/34839"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=34790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=34790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=34790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}