{"id":4465,"date":"2019-04-23T15:53:15","date_gmt":"2019-04-23T14:53:15","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=4465"},"modified":"2019-04-29T00:45:30","modified_gmt":"2019-04-28T23:45:30","slug":"quick-python-tricks","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2019\/04\/quick-python-tricks\/","title":{"rendered":"Quick Python tricks"},"content":{"rendered":"<p>It&#8217;s always fun when you stumble across something in your programming toolkit that you had never noticed. Here are three things I&#8217;ve recently enjoyed learning.<\/p>\n<ul>\n<li>Ternary syntax\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">a = int(raw_input())\nis_even = True if a % 0 == 0 else False<\/pre>\n<\/li>\n<li>Enumerate<\/li>\n<\/ul>\n<p>I&#8217;ve been looping over the length of my list, all these years, like a chump. It turns out you can do this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">for index, item in enumerate(some_list):\n    # now the index of each item is available as well as the item    \n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># Don't do do this\nfor index in range(len(some_list)):\n    item = some_list[index]\n<\/pre>\n<ul>\n<li>for&#8230; else<\/li>\n<\/ul>\n<p>Every so often, you really need to know that a for loop has run to completion. That&#8217;s what for&#8230;else is for!<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for item in iterable:<br>    if item % 0 == 0:<br>       first_even_number = item<br>else:<br>    raise ValueError('No even numbers')<\/pre>\n<p><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s always fun when you stumble across something in your programming toolkit that you had never noticed. Here are three things I&#8217;ve recently enjoyed learning. Ternary syntax a = int(raw_input()) is_even = True if a % 0 == 0 else False Enumerate I&#8217;ve been looping over the length of my list, all these years, like [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,14,227],"tags":[],"ppma_author":[496],"class_list":["post-4465","post","type-post","status-publish","format-standard","hentry","category-code","category-howto","category-python-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":496,"user_id":46,"is_guest":0,"slug":"mark","display_name":"Mark","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/4993b5241cf007fa9b439f82360e05c268e9d358a0153d0a78f40df9ace86f42?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=4465"}],"version-history":[{"count":2,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4465\/revisions"}],"predecessor-version":[{"id":4640,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/4465\/revisions\/4640"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=4465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=4465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=4465"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}