{"id":40351,"date":"2021-07-26T09:13:06","date_gmt":"2021-07-26T07:13:06","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=40351"},"modified":"2021-10-29T16:15:01","modified_gmt":"2021-10-29T14:15:01","slug":"python-tricks","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/blog-python\/trucos-de-python\/","title":{"rendered":"Python Tricks You Should Be Using"},"content":{"rendered":"<p>Last week we saw a \u201cchop\u201d with the<strong><a href=\"https:\/\/eiposgrados.com\/eng\/python-blog\/numpy-and-pandas-modules\/\" data-type=\"URL\" data-id=\"https:\/\/eiposgrados.com\/blog-python\/modulos-numpy-y-pandas\/\">&nbsp;NumPy\/SciPy and Pandas methods<\/a>&nbsp;<\/strong>most used, so this week we thought we would continue along the same lines and provide you with some Python programming tricks that will surely make your life as a programmer easier.<\/p>\n\n\n\n<p>If you want to train as a programmer and be a <strong>highly qualified expert<\/strong>&nbsp;with the necessary skills to carry out programming work in&nbsp;<strong>Python<\/strong>&nbsp;<strong>specialized<\/strong>, he<a href=\"https:\/\/eiposgrados.com\/eng\/master-in-python\/\" data-type=\"URL\" data-id=\"https:\/\/eiposgrados.com\/master-en-python\/\"> Master in Advanced Programming in Python for Big Data, Hacking and Machine Learning<\/a> will make you get it. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reverse a list<\/strong><\/h2>\n\n\n\n<p>We can simply reverse a given list using a reverse[] function. Serves for numeric and string data types present in the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/invertir-una-lista-trucos-de-python.png\" alt=\"\" class=\"wp-image-40352\" width=\"527\" height=\"142\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Print list items in any order<\/strong><\/h2>\n\n\n\n<p>If we need to print the values of a list in different orders, we can assign the list to a series of variables and programmatically decide the order in which we want to print the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/invertir-una-lista-trucos-de-python-1.png\" alt=\"\" class=\"wp-image-40353\" width=\"509\" height=\"137\" title=\"\"><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/imprimir-los-elementos-trucos-de-python.png\" alt=\"\" class=\"wp-image-40354\" width=\"90\" height=\"71\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using generators inside functions<\/strong><\/h2>\n\n\n\n<p>We can use generators directly inside a function to write shorter and cleaner code. In the following example, we find the sum by using a generator directly as an argument to the sum function.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/uso-de-generadores-trucos-de-python.png\" alt=\"\" class=\"wp-image-40355\" width=\"402\" height=\"78\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using the zip[] function<\/strong><\/h2>\n\n\n\n<p>When we need to join many iterator objects like list to get a single list, we can use zip function. The result shows that each element will be grouped with its respective elements from the other lists.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/funcion-zip-trucos-de-python.png\" alt=\"\" class=\"wp-image-40356\" width=\"578\" height=\"257\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Exchange of two numbers<\/strong><\/h2>\n\n\n\n<p>Exchanging numbers generally requires storing values in temporary variables. But with this Python trick we can do it using one line of code and without using any temporary variables.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/zip-trucos-de-python.png\" alt=\"\" class=\"wp-image-40357\" width=\"244\" height=\"239\" title=\"\"><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Transposition of a matrix<\/strong><\/h2>\n\n\n\n<p>Transposing a matrix involves converting columns into rows. In Python we can achieve this by designing some loop structure to iterate through the elements in the array and change their places or we can use the following script which involves the zip function along with the * operator to unzip a list which becomes a transpose of matrix.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2021\/07\/transposicion-matriz-trucos-de-python.png\" alt=\"\" class=\"wp-image-40358\" width=\"492\" height=\"228\" title=\"\"><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>Last week we saw a \u201ccheatsheet\u201d with the most used NumPy \/ SciPy and Pandas methods, so this week we thought we would continue in the same way\u2026 <a title=\"Python Tricks You Should Be Using\" class=\"read-more\" href=\"https:\/\/eiposgrados.com\/eng\/blog-python\/trucos-de-python\/\" aria-label=\"Read more about Python Tricks You Should Be Using\">Read more<\/a><\/p>","protected":false},"author":1,"featured_media":51575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407],"tags":[],"class_list":["post-40351","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\/40351","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=40351"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/40351\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/51575"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=40351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=40351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=40351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}