{"id":907,"date":"2024-05-06T16:42:20","date_gmt":"2024-05-06T14:42:20","guid":{"rendered":"https:\/\/arter-demo.bslthemes.com\/?p=146"},"modified":"2026-10-05T08:15:54","modified_gmt":"2026-10-05T06:15:54","slug":"pro-tis-python-1","status":"publish","type":"post","link":"https:\/\/germanmallo.com\/en\/pro-tis-python-1\/","title":{"rendered":"Gu\u00eda de \u00abPro Tips\u00bb para principiantes en Python"},"content":{"rendered":"<h2 id=\"-tip-1-m-ltiples-condiciones-en-una-l-nea\" style=\"text-align: justify\">[TIP 1] M\u00faltiples condiciones en una l\u00ednea<\/h2>\n<p style=\"text-align: justify\">Para hacer condiciones tipo \u00abif\u00bb, se puede hacer de una forma mucho m\u00e1s elegante y en una l\u00ednea de la siguiente forma:<\/p>\n<ul style=\"text-align: justify\">\n<li>En vez de hacer esto:<\/li>\n<\/ul>\n<pre><code class=\"lang-python\"><span class=\"hljs-built_in\">if<\/span> a == <span class=\"hljs-number\">3<\/span>:\n    <span class=\"hljs-built_in\">print<\/span>(a)\n<span class=\"hljs-built_in\">else<\/span>:\n    <span class=\"hljs-built_in\">print<\/span>(b)\n<\/code><\/pre>\n<ul style=\"text-align: justify\">\n<li>Puedes hacer esto:<\/li>\n<\/ul>\n<pre><code class=\"lang-python\"><span class=\"hljs-function\"><span class=\"hljs-title\">print<\/span><span class=\"hljs-params\">(a)<\/span><\/span> <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-selector-tag\">a<\/span> == <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">else<\/span> print(b)\n<\/code><\/pre>\n<h2 id=\"-tip-2-comparar-dos-listas-en-una-l-nea\" style=\"text-align: justify\">[TIP 2] Comparar dos listas en una l\u00ednea<\/h2>\n<p style=\"text-align: justify\">Poner explicaci\u00f3n:<\/p>\n<ul style=\"text-align: justify\">\n<li>En vez de hacer esto para comparar dos listas:<\/li>\n<\/ul>\n<pre><code class=\"lang-python\">h = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\nlist = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\n\nr = []\n<span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-built_in\">elem<\/span> <span class=\"hljs-keyword\">in<\/span> h:\n    <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-built_in\">elem<\/span> <span class=\"hljs-keyword\">in<\/span> list:\n        r.<span class=\"hljs-built_in\">append<\/span>(<span class=\"hljs-built_in\">elem<\/span>)\n# r = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\n<\/code><\/pre>\n<ul style=\"text-align: justify\">\n<li>Puedes hacer esto:<\/li>\n<\/ul>\n<pre><code class=\"lang-python\">h = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\n<span class=\"hljs-type\">list<\/span> = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\n\nr = [elem for elem in h if elem in <span class=\"hljs-type\">list<\/span>]\n# r = [<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">10<\/span>]\n<\/code><\/pre>\n<h2 id=\"-tip-3-comprobaci-n-de-variables-de-forma-m-s-pythonic-\" style=\"text-align: justify\">[TIP 3] Comprobaci\u00f3n de variables de forma m\u00e1s \u00abpythonic\u00bb.<\/h2>\n<p style=\"text-align: justify\">Si tengo dos variables y quiero que me devuelva la que no es None, en vez de hacer esto:<\/p>\n<pre><code>````python\na = None\nb = <span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-keyword\">if<\/span> a:\n    print(a)\nelse:\n    print(b)\n````\nPrintea <span class=\"hljs-number\">2<\/span>\n<\/code><\/pre>\n<p style=\"text-align: justify\">Haz mejor esto:<\/p>\n<pre><code>````python\na = None\nb = <span class=\"hljs-number\">2<\/span>\nprint(a or b)\n````\nPrintea <span class=\"hljs-number\">2<\/span>\n<\/code><\/pre>\n<h2 id=\"-tip-4-objetos-tipo-queue-en-python-librer-a-collections-https-docs-python-org-3-library-collections-html-\" style=\"text-align: justify\">[TIP 4] Objetos tipo <code>queue<\/code> en Python (<a href=\"https:\/\/docs.python.org\/3\/library\/collections.html\">Librer\u00eda collections<\/a>).<\/h2>\n<p style=\"text-align: justify\">Si queremos hacer una cola de objetos, en vez de crear una lista y tratarla como tal, podemos importar la libreria <code>collections<\/code> y usar el objeto <code>queue<\/code> que nos ofrece.<\/p>\n<pre><code class=\"lang-python\">from collections <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-built_in\">deque<\/span>\n<\/code><\/pre>\n<p style=\"text-align: justify\">Estas son las principales diferencias entre una lista y una cola:<\/p>\n<ul style=\"text-align: justify\">\n<li>Acceso a elementos:\n<ul>\n<li>En una lista, puedes acceder a elementos por su \u00edndice, y eliminar o insertar elementos en cualquier posici\u00f3n.<\/li>\n<li>En una cola, solo puedes acceder al primer elemento, y solo puedes eliminar o insertar elementos por el final de la cola.<\/li>\n<\/ul>\n<\/li>\n<li>Operaciones eficientes:\n<ul>\n<li>Las colas (deque) est\u00e1n optimizadas para operaciones de agregar y quitar elementos tanto al principio como al final. Las operaciones <code>popleft()<\/code> y <code>pop()<\/code> son eficientes, al igual que <code>append()<\/code> y <code>appendleft()<\/code>.<\/li>\n<li>En una lista, agregar o quitar elementos al principio es menos eficiente, ya que implica desplazar todos los elementos restantes.<\/li>\n<\/ul>\n<\/li>\n<li>Uso espec\u00edfico:\n<ul>\n<li>Las colas son \u00fatiles cuando necesitas seguir el principio de \u00abprimero en entrar, primero en salir\u00bb (FIFO). Es decir, el primer elemento que se agrega es el primero en ser eliminado.<\/li>\n<li>Las listas son m\u00e1s vers\u00e1tiles y se utilizan para una variedad de prop\u00f3sitos, ya que admiten operaciones en cualquier posici\u00f3n y son m\u00e1s adecuadas cuando el orden exacto de los elementos es crucial. En resumen, mientras que una cola (<code>deque<\/code>) comparte algunas similitudes con una lista, est\u00e1 optimizada para ciertos escenarios, como la gesti\u00f3n eficiente de elementos en el principio y el final de la secuencia.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-python\">from collections <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-built_in\">deque<\/span>\n\n<span class=\"hljs-built_in\">queue<\/span> = <span class=\"hljs-built_in\">deque<\/span>()\n\n<span class=\"hljs-built_in\">queue<\/span>.append(<span class=\"hljs-number\">1<\/span>)\n<span class=\"hljs-built_in\">queue<\/span>.append(<span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-built_in\">queue<\/span>.append(<span class=\"hljs-number\">3<\/span>)\n\nprint(<span class=\"hljs-built_in\">queue<\/span>) # <span class=\"hljs-built_in\">deque<\/span>([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\n\n<span class=\"hljs-built_in\">queue<\/span>.popleft()\nprint(<span class=\"hljs-built_in\">queue<\/span>) # <span class=\"hljs-built_in\">deque<\/span>([<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\n\n<span class=\"hljs-built_in\">queue<\/span>.pop()\nprint(<span class=\"hljs-built_in\">queue<\/span>) # <span class=\"hljs-built_in\">deque<\/span>([<span class=\"hljs-number\">2<\/span>])\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[TIP 1] M\u00faltiples condiciones en una l\u00ednea Para hacer condiciones tipo \u00abif\u00bb, se puede hacer de una forma mucho m\u00e1s elegante y en una l\u00ednea de la siguiente forma: En vez de hacer esto: if a == 3: print(a) else: print(b) Puedes hacer esto: print(a) if a == 3 else print(b) [TIP 2] Comparar dos [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1020,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[73],"tags":[130,96,90],"class_list":["post-907","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desarrollo-de-software","tag-buenas-practicas","tag-programacion","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/posts\/907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/comments?post=907"}],"version-history":[{"count":1,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/posts\/907\/revisions"}],"predecessor-version":[{"id":4510,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/posts\/907\/revisions\/4510"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/media\/1020"}],"wp:attachment":[{"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/media?parent=907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/categories?post=907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/germanmallo.com\/en\/wp-json\/wp\/v2\/tags?post=907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}