I’ve been working on Python a bit more casually but now decided to make it a focus of my summer. Inspired by CGP Grey, it’s the summer of Python. And I’ll be writing weeknotes to keep myself accountable.

Where I’m starting from: familar with loops, variables, funtions, classes… but never put all these together to make any concrete project or in production code. At work I use Python ocassionally to write scripts for different data tasks and Jupyter Notebooks for analysis. My implementations get the job done but I’m keen on learning more Pythonic ways of approaching things.

The goal more concretely is to improve my problem solving using Python, learn more advanced features (standard library, common libraries) and building small personal tools. Losely the plan over the next few weeks is to:

  • Work through different practice tasks on Exercism
  • Read Grokking Algorithms
  • Reimplement small Python projects I’ve collected on GitHub. Top of the list is: libro - a command-line book tracking tool which I’m reimplementing as bookshelf.
  • Using Anki for spaced repetition to make memory a choice.
  • Follow my curiosity. I’m optimizing for fun as I learn so detours in service of the main goal are encouraged.

This week I worked through the following Exercism tasks: Resistor Color Expert, Secret Handshake, Anagram, House and Binary Search . Took a lot of detours and learned about a few things that were new to me:

:g - the general formatter for floats which chooses between fixed-point and scientific notation, depending on the value.It defaults to 6 significant digits unless you specify a precision with .Ng where N is the significant digits.

f"{123456.789:g}"    # โ†’ '123457'
f"{0.000012345:g}"   # โ†’ '1.2345e-05'
f"{123.0:g}"         # โ†’ '123'
f"{123456.789:.2g}"  # โ†’ '1.2e+05'
f"{0.0123456:.4g}"   # โ†’ '0.01235'
f"{123.456:.5g}"     # โ†’ '123.46'

Reversing lists - I kept getting TypeError as I tried to enumerate over mylist.reverse() directly. Some reminders on reversing lists:

  • list.reverse() reverses the list in-place and returns None. Be careful using in a loop or anywhere were you expect the reversed loop in the same operation.
  • reverse(list) returns a generator object which you need to cast into a list or use elements one by one. Remember generator objects can only be used once.
  • list[::-1 gets you a convenient reversed copy to use without modifying the original which I’ve sure encountered before.

bisect module - learned about this module while consulting Claude on alternative solutions binary search. The module provides efficient binary search operations on sorted lists, allowing you to find insertion points and maintain sorted order without implementing binary search yourself. It has 3 common operations:

  • bisect.bisect_left(sorted_list, num) - returns leftmost insertion index
  • bisect.bisect_right(sorted_list, num) - returns leftmost insertion index
  • bisect.insort(sorted_list, num) - insert while maintaining order

Overall a good week. I struggled the most when I started writing the solution as I thought of the logic. To try in the future, map out the complete logic on paper before typing in the IDE. Next week: more exercism workouts, work my way throug Grokking Algorithms and try ship my bookshelf implementation of libro.