{"id":73625,"date":"2020-09-23T16:18:40","date_gmt":"2020-09-23T13:18:40","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/6-practical-tricks-every-python-developer-should-have\/"},"modified":"2020-09-23T16:18:40","modified_gmt":"2020-09-23T13:18:40","slug":"6-practical-tricks-every-python-developer-should-have","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/","title":{"rendered":"#6 practical tricks every Python developer should have"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a418830e72d1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a418830e72d1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#1_List_comprehensions\" >1. List comprehensions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#2_Zipping\" >2. Zipping<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#3_Counting_items\" >3. Counting items<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#4_Enumerating\" >4. Enumerating<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#5_Parameter_expansion\" >5. Parameter expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#6_Type_annotations\" >6. Type annotations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/6-practical-tricks-every-python-developer-should-have\/#Closing_thoughts\" >Closing thoughts<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#6 practical tricks every Python developer should have<\/strong>&#8221;<\/p>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">watch Movies<\/a> or TV series visit the <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/dizi.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Dizi.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<div>\n                                <span style=\"font-weight: 400;\">Python has become a popular programming language because it is clear, versatile, easy to learn, and it has plenty of useful libraries for different tasks. From web development to data <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a> and cybersecurity, Python programmers are high in demand.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But like all programming languages, in Python, everything depends on you, the programmer. You decide whether your code looks professional or ugly.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Fortunately, Python has a vast array of built-in features that can help you write code that is elegant, concise, and expandable, the kind of characteristics you expect from a professional programmer.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here some of my favorite tricks, which I think every Python developer should know. <\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"1_List_comprehensions\"><\/span><strong>1. List comprehensions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">List comprehensions are one of the key features of Python that help you write code that is more concise and elegant. Let\u2019s say you want to create an array containing numbers from 1 to 100. The convenient way to do it would be to use the <\/span><i><span style=\"font-weight: 400;\">range()<\/span><\/i><span style=\"font-weight: 400;\"> function:<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">numbers = <\/span><span style=\"font-weight: 400;\">list<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">101<\/span><span style=\"font-weight: 400;\">))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">But what if you wanted to do something more complicated, such as creating a list of squares from 1 to 100? In that case, the classic way would be to use a <\/span><i><span style=\"font-weight: 400;\">for<\/span><\/i><span style=\"font-weight: 400;\"> loop:<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">numbers = []<\/span><\/code><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> i <\/span><b>in<\/b> <span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">101<\/span><span style=\"font-weight: 400;\">):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0numbers.<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>end(i**<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">This is the standard way that most programming languages support. But fortunately, in Python, list comprehensions make things a lot easier. Here\u2019s what the same code would look like when written in list comprehension mode:<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">numbers = [i**<\/span><span style=\"font-weight: 400;\">2<\/span> <b>for<\/b><span style=\"font-weight: 400;\"> i <\/span><b>in<\/b> <span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">101<\/span><span style=\"font-weight: 400;\">)]<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">You can use list comprehensions to pack several instructions and expressions in the brackets that define a list. They\u2019re much shorter and more elegant than the <\/span><i><span style=\"font-weight: 400;\">for <\/span><\/i><span style=\"font-weight: 400;\">loop. And there\u2019s a lot more you can do while still keeping your code clean. For instance, say you have a function <\/span><i><span style=\"font-weight: 400;\">is_prime()<\/span><\/i><span style=\"font-weight: 400;\"> that checks an input number and returns True if it\u2019s a prime number. The following code snippet creates a list of squares of primes from 1 to 100 by adding <\/span><i><span style=\"font-weight: 400;\">is_prime()<\/span><\/i><span style=\"font-weight: 400;\"> a condition to the comprehension.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"2_Zipping\"><\/span><strong>2. Zipping<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Another Python feature that comes handy every once in a while is the <\/span><i><span style=\"font-weight: 400;\">zip()<\/span><\/i><span style=\"font-weight: 400;\"> function. <\/span><i><span style=\"font-weight: 400;\">zip<\/span><\/i><span style=\"font-weight: 400;\"> combines two or more lists into a single variable. Say you have collected a list of customer names, their ages, and their favorite ice cream flavor.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">customers = [<\/span><span style=\"font-weight: 400;\">'John'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'Natasha'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'Eric'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'Sally'<\/span><span style=\"font-weight: 400;\">]<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">ages = [<\/span><span style=\"font-weight: 400;\">26<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">31<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">39<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">]<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">flavors = [<\/span><span style=\"font-weight: 400;\">'cherry'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'chocolate'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'strawberry'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'lemon'<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Using <\/span><i><span style=\"font-weight: 400;\">zip()<\/span><\/i><span style=\"font-weight: 400;\">, you can consolidate all three lists into a single list where each entry contains a tuple with the name, age, and preferred flavor of one customer.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">combined = <\/span><span style=\"font-weight: 400;\">zip<\/span><span style=\"font-weight: 400;\">(customers, ages, flavors)<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">customers_ice_cream = <\/span><span style=\"font-weight: 400;\">list<\/span><span style=\"font-weight: 400;\">(combined)<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">This is what your customers_ice_cream list looks like after zipping the data:<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">[('John', 26, 'cherry'),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0('Natasha', 31, 'chocolate'),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0('Eric', 39, 'strawberry'),<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0('Sally', 22, 'lemon')]<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">And here\u2019s how the zipped list can be used in a loop:<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> cust <\/span><b>in<\/b><span style=\"font-weight: 400;\"> customers_ice_cream:<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\"> is <\/span><b>{}<\/b><span style=\"font-weight: 400;\"> years old and likes <\/span><b>{}<\/b><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">.format(*cust))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">The output looks like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">John is 26 years old and likes cherry<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Natasha is 31 years old and likes chocolate<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Eric is 39 years old and likes strawberry<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sally is 22 years old and likes lemon<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Counting_items\"><\/span><strong>3. Counting items<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Often, you want to know how many times a certain value occurs in a list. For instance, say we\u2019ve collected a list of 1-10 ratings from an online survey. To simulate this, we\u2019ll generate a list of 1,000 random numbers from 1 to 10 using the <\/span><i><span style=\"font-weight: 400;\">randint() <\/span><\/i><span style=\"font-weight: 400;\">function.<\/span><\/p>\n<p><code><b>from<\/b> <b>random<\/b> <b>import<\/b><span style=\"font-weight: 400;\"> randint<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">ratings = [randint(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">) <\/span><b>for<\/b><span style=\"font-weight: 400;\"> _ <\/span><b>in<\/b> <span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\">1001<\/span><span style=\"font-weight: 400;\">)]<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Now, we want to know how many of each rating is included in the list. One way to do this is to use the built-in <\/span><i><span style=\"font-weight: 400;\">count<\/span><\/i><span style=\"font-weight: 400;\"> function of the list. <\/span><i><span style=\"font-weight: 400;\">count() <\/span><\/i><span style=\"font-weight: 400;\">takes a value and returns the number of times that value occurs in the list.<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> i <\/span><b>in<\/b> <span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\"> occurences\"<\/span><span style=\"font-weight: 400;\">.format(i, ratings.count(i)))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">This code produces the following output:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1: 95 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2: 115 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3: 111 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4: 109 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">5: 81 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">6: 110 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">7: 80 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">8: 94 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">9: 98 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">10: 107 occurrences<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, this only works if you know in advance what is the range of values in your list. In case you don\u2019t know the possible values, you can use a <\/span><i><span style=\"font-weight: 400;\">set<\/span><\/i><span style=\"font-weight: 400;\">, which creates a list of unique items contained in another list. For instance, if you have a list of names and want to know how many times each name has occurred you can use the following code.<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> name <\/span><b>in<\/b> <span style=\"font-weight: 400;\">set<\/span><span style=\"font-weight: 400;\">(names):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\"> occurences\"<\/span><span style=\"font-weight: 400;\">.format(name, names.count(name)))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Alternatively, you can use the <\/span><i><span style=\"font-weight: 400;\">Counter<\/span><\/i><span style=\"font-weight: 400;\"> class, which specializes in counting values in lists.<\/span><\/p>\n<p><code><b>from<\/b> <b>collections<\/b> <b>import<\/b><span style=\"font-weight: 400;\"> Counter<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">ratings_count = Counter(ratings)<\/span><\/code><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> rating <\/span><b>in<\/b><span style=\"font-weight: 400;\"> ratings_count:<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\"> occurences\"<\/span><span style=\"font-weight: 400;\">.format(rating, ratings_count[rating]))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Counter provides some added functionality, such as the <\/span><i><span style=\"font-weight: 400;\">most_common()<\/span><\/i><span style=\"font-weight: 400;\"> function, which gives you the most occurring values in a list. For instance, the following code will print out the three most-popular values:<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> rating <\/span><b>in<\/b><span style=\"font-weight: 400;\"> ratings_count.most_common(<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\"> occurences\"<\/span><span style=\"font-weight: 400;\">.format(*rating))<\/span><\/code><\/p>\n<p><em>Want to work at Rijksoverheid?\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" class=\"c-link\" href=\"https:\/\/www.werkenvoornederland.nl\/vakgebieden\/icters-bij-de-rijksoverheid\" data-stringify-link=\"https:\/\/www.werkenvoornederland.nl\/vakgebieden\/icters-bij-de-rijksoverheid\" data-sk=\"tooltip_parent\">They\u2019re looking for Python developers<\/a>.<\/em><\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Enumerating\"><\/span><strong>4. Enumerating<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Sometimes, you want to keep track of the count of items as you iterate through a list. Say you have a list of customer names and you want to list them along with their index number. Here\u2019s one way to do it:<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> i <\/span><b>in<\/b> <span style=\"font-weight: 400;\">range<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">len<\/span><span style=\"font-weight: 400;\">(customers)):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">.format(i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, customers[i]))<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">This would produce an output that looks something like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1: Samantha<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2: Mara<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3: Eric<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4: James<\/span><\/p>\n<p><span style=\"font-weight: 400;\">5: George<\/span><\/p>\n<p><span style=\"font-weight: 400;\">6: Toni<\/span><\/p>\n<p><span style=\"font-weight: 400;\">7: Margaret<\/span><\/p>\n<p><span style=\"font-weight: 400;\">8: Steven<\/span><\/p>\n<p><span style=\"font-weight: 400;\">While this code works, it\u2019s not very elegant. Notice the mismatch between the index and the counter? Fortunately, Python has an <\/span><i><span style=\"font-weight: 400;\">enumerate()<\/span><\/i><span style=\"font-weight: 400;\"> function that makes your index-tracking code much more understandable and pleasing to the eye. <\/span><i><span style=\"font-weight: 400;\">enumerate() <\/span><\/i><span style=\"font-weight: 400;\">takes two arguments, the list you want to enumerate and the starting number of the counter, and gives two outputs at each round of the loop, the counter value and the list item. Here\u2019s what the same problem is solved with <\/span><i><span style=\"font-weight: 400;\">enumerate<\/span><\/i><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><code><b>for<\/b><span style=\"font-weight: 400;\"> i, customer <\/span><b>in<\/b> <span style=\"font-weight: 400;\">enumerate<\/span><span style=\"font-weight: 400;\">(customers, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"<\/span><b>{}<\/b><span style=\"font-weight: 400;\">: <\/span><b>{}<\/b><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">.format(i, customer))<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Much better.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Parameter_expansion\"><\/span><strong>5. Parameter expansion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Say you have a function that processes student information:<\/span><\/p>\n<p><code><b>def<\/b> <span style=\"font-weight: 400;\">process_student_info<\/span><span style=\"font-weight: 400;\">(first_name, last_name, fav_topic, score):<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">print<\/span><span style=\"font-weight: 400;\">(first_name, last_name, fav_topic, score)<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">In many cases, the values you want to pass to the function are included in a <\/span><i><span style=\"font-weight: 400;\">list<\/span><\/i><span style=\"font-weight: 400;\"> or <\/span><i><span style=\"font-weight: 400;\">dictionary<\/span><\/i><span style=\"font-weight: 400;\"> object that has been populated from a database or text file. In such cases, calling the function would be a bit clunky:<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">process_student_info(student[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">], student[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">], student[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">], student[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">])<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Fortunately, Python \u201cparameter expansion\u201d feature, which enables you to directly pass an entire list to a function. By adding a * at the beginning of the list\u2019s name, you expand it to its individual values before submitting it to the function.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">process_student_info(*student)<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">Parameter expansion with lists works as long as the number of parameters and their sequence is similar to the parameters of the target function. In case of a mismatch, it will raise an error.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can also use parameter expansion with dictionaries, in which case, the order of values doesn\u2019t matter. You only need to have keys that correspond to your function parameters. Parameter expansion for dictionaries requires the ** operator before the object.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">student = {<\/span><span style=\"font-weight: 400;\">'last_name'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'doe'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'score'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">89<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'first_name'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'john'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'fav_topic'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'calculus'<\/span><span style=\"font-weight: 400;\">}<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">process_student_info(**student)<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">One of the benefits of using dictionary expansion is that if your function has default parameters, omitting them in your dictionary won\u2019t raise an error.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"6_Type_annotations\"><\/span><strong>6. Type annotations<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Python is a <\/span><i><span style=\"font-weight: 400;\">dynamically-typed<\/span><\/i><span style=\"font-weight: 400;\"> language, which means if you try to mix variables that have different data types, it usually finds a way to resolve the difference or raises an exception if it can\u2019t. But this flexibility can also result in unpredictable behavior.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Say you have a function that multiplies two variables.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">def<\/span> mul(a, b):<\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return a * b<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">if you call <\/span><i><span style=\"font-weight: 400;\">mul()<\/span><\/i><span style=\"font-weight: 400;\"> on two integers or floats \u2014 your intended use for the function \u2014 the result is predictable.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">a = 5<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">b = 6<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">print(mul(a, b))<\/span><\/code><\/p>\n<p><em><span style=\"font-weight: 400;\">Output: 30<\/span><\/em><\/p>\n<p><span style=\"font-weight: 400;\">But what if one of your variables is a list?<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">a = [1, 2, 4, 5]<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">b = 3<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">print(mul(a, b))<\/span><\/code><\/p>\n<p><em><span style=\"font-weight: 400;\">Output: [1, 2, 4, 5, 1, 2, 4, 5, 1, 2, 4, 5]<\/span><\/em><\/p>\n<p><span style=\"font-weight: 400;\">The function creates a list that is three concatenated copies of the list. This is clearly not what you wanted to do. Python versions 3.6 and higher provide \u201ctype annotations,\u201d a feature that enables you to define the type of data each function argument should take. Here\u2019s how the type annotated <\/span><i><span style=\"font-weight: 400;\">mul() <\/span><\/i><span style=\"font-weight: 400;\">function looks like.<\/span><\/p>\n<p><code><span style=\"font-weight: 400;\">def mul(a: int, b: int):\u00a0<\/span><\/code><\/p>\n<p><code><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return a * b<\/span><\/code><\/p>\n<p><span style=\"font-weight: 400;\">This format explicitly states that <\/span><i><span style=\"font-weight: 400;\">mul()<\/span><\/i><span style=\"font-weight: 400;\"> takes two integer values. To be clear, type annotation won\u2019t prevent you from using the function in unintended ways. But there are some good reasons to use it anyway.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">First, defining the type of data is a form of documentation, and makes it easier for other developers going through your code to understand what type of data you expect in your function (compare the annotated and non-annotated versions of the function).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But even more importantly, some code editors process type annotations and help you with features such as autocomplete and type errors.<\/span><\/p>\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1315461 lazy\" alt=\"\" width=\"598\" height=\"291\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Screenshot-2020-09-02-at-10.49.30.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Screenshot-2020-09-02-at-10.49.30.png 598w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Screenshot-2020-09-02-at-10.49.30-280x136.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Screenshot-2020-09-02-at-10.49.30-540x263.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Screenshot-2020-09-02-at-10.49.30-270x131.png 270w\"\/><figcaption><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/thenextweb.com\/readme\/2020\/09\/23\/6-practical-tricks-every-python-developer-should-have\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Freadme%2F2020%2F09%2F23%2F6-practical-tricks-every-python-developer-should-have%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Screenshot of Spyder IDE showing autocomplete for typed variable.\" data-title=\"Share Screenshot of Spyder IDE showing autocomplete for typed variable. on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Screenshot of Spyder IDE showing autocomplete for typed variable. on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Screenshot of Spyder IDE showing autocomplete for typed variable.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Closing_thoughts\"><\/span><strong>Closing thoughts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">These were six of my favorite Python tricks. Using them will make your code concise and elegant. With Python fast becoming the defacto programming language in many domains and institutions, having a good set of best practices will ensure you can be a productive member of your development team.<\/span><\/p>\n<div class=\"c-post-paidNotice c-post-paidNotice--below\">\n            <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.werkenvoornederland.nl\/vakgebieden\/icters-bij-de-rijksoverheid\"><img decoding=\"async\" class=\"c-post-paidNotice-image\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2019\/12\/logo_rijksoverheid_png.-1-e1576163902139.png\"\/><\/a><\/p>\n<p class=\"c-post-paidNotice-text\"><span>This article is brought to you by <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" onclick=\"ga('primary.send', 'event', 'Article', 'Sponsored Post', 'Link');\" href=\"https:\/\/www.werkenvoornederland.nl\/vakgebieden\/icters-bij-de-rijksoverheid\">Rijksoverheid.<\/a>\u00a0<\/span><\/p>\n<\/div>\n<p class=\"c-post-pubDate\">\n                                    Published September 23, 2020 \u2014 13:18 UTC\n                                <\/p>\n<\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><script data-src=\"https:\/\/connect.facebook.net\/en_US\/sdk.js#xfbml=1&amp;appId=378011798897423&amp;version=v2.6\" id=\"socialSrcFacebook\" type=\"text\/template\"><\/script><\/p>\n<blockquote>\n<p style=\"text-align: center;\"><strong>if you want to watch Movies or Tv Shows go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/dizi.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Dizi.BuradaBiliyorum.Com<\/a> <\/span> for forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/readme\/2020\/09\/23\/6-practical-tricks-every-python-developer-should-have\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#6 practical tricks every Python developer should have&#8221; If you want to watch Movies or TV series visit the Dizi.BuradaBiliyorum.Com Python has become a popular programming language because it is clear, versatile, easy to learn, and it has plenty of useful libraries for different tasks. From web development to data science and cybersecurity, Python programmers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":73626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/readme?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/09\/Untitled-design-7.png&signature=2fb6dfeb85eef5d7f8057114e4501f8f","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-73625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/73625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=73625"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/73625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/73626"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=73625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=73625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=73625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}