Cool tricks that even experienced Python programmers don't know!

in #programming9 months ago

Using an else-statement on loops

One of the less known features of python is the possibility to define an else-statement for a loop. This allows to transform the following code:

to this code:

The number of lines of code that can be saved is not large, but you can save the temporary variable. I think in any case that the code looks nicer =)

Merging dictionaries

Merging dictionaries has been simplified with Python 3.9. The snipped below shows you how dictionary merging was done in Python prior to version 3.9.

In Python 3.9 the same thing can be achieved with the following code

Using separators for large numbers

Since it can be difficult to read large constants without a thousand separator, python allows the bottom bar to be used as a separator. You can find an example below.

I think everyone will agree that the delimiter makes the constants much easier to read.


Cool! Consider adding a source where to learn more about these features. I tried them myself.

Thanks for the suggestion =) I will try to add a link to the documentation next time!

Wow! The first one is interesting! I always used a flag variable to perform that! Good to know

Glad you could learn a new trick =)


You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
And they will bring !PIZZA 🍕.

Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.


PIZZA Holders sent $PIZZA tips in this post's comments:
@curation-cartel(8/20) tipped @grider123 (x1)

Learn more at

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 

Congratulations @grider123! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Feedback from the July 1st Hive Power Up Day
The 7th edition of the Hive Power Up Month starts today!
Thank you for sharing this amazing post on HIVE!
  • Your content got selected by our fellow curator @priyanarc & you just received a little thank you via an upvote from our non-profit curation initiative!

  • You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.

Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server: !

If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!

Stay creative & hive on!