A Thought Process to Learn the Pythonic Coding Style

I am providing some information about a few important concepts and built-in functions to learn Pythonic way coding styles in this blog.

Narendra Harny
The Startup
5 min readSep 17, 2020

--

If you are not aware of what is Pythonic way and why it is important then, please invest little time with this blog(Link given below).

I have accumulated some random examples so you can skip if you already do use of that way in your code.

First of all, I would like to mention a few Python topics which are more important to do code in a Pythonic way and few suggestions that help you to think to optimize the code to make to more Pythonic.

  • Use Python built-in functions.
  • Use the Python advance modules and libs for example collections, lambda, etc.
  • Learn Comprehension with iterators.
  • Learn lambda expressions functions.
  • Learn functions in python more, like functions as an object and its features.
  • Learn Generators and Decorators.
  • See if is built-in functions that create a new copy of iterators and decide when to use those built-in functions.
  • Optimize the loops and conditional statements.
  • Always think about selecting the correct iterator object for the logic.
  • Try to find out a way to reduce lines of code.
  • Only think of reducing lines of code to make it more understandable. (Too much on this will again make it more complex to understand and less Pythonic).
  • Use PEP 8 guidelines to write the code.
  • Break the main logic in functions to make code more understandable.

Let’s see the examples, Please try writing the code by putting your thoughts in It, so that It will be more easy and clear to understand.

The Empty list checks without a size compare:

Use of sorted() and list.sort() functions:

The difference between sorted() and list.sort() function:

sorted(): The function returns a new list containing all items from the iterable in ascending order.

list.sort(): The functions will not create a new list It modifies the same list on which it is called.

Must use built-in functions:

all(): The function returns True if all items in an iterable are true, otherwise it returns False.

any(): The function returns True if any item in an iterable is true, otherwise it returns False.

filter(): The function returns an iterator where the items are filtered through a function to test if the item is accepted or not.

hash(): The function returns the hash value of a specified object.

An object is hashable if it has a hash value which never changes during its lifetime, All of Python’s immutable built-in objects are hashable.

map(): The function executes a specified function for each item in an iterable. The item is sent to the function as a parameter.

max(): The function returns the item in an iterable with the highest value.

min(): The function returns the item in an iterable with the smallest value.

reversed(): The function returns a reversed iterator object created newly without modifying the original iterator object.

list.reverse(): The functions modify the same iterator object in reverse order.

enumerate(): The functions give touples with index values.

Comprehension & Lambda

Comprehensions are constructs that allow sequences to be built from other sequences. Python 2.0 introduced list comprehensions and Python 3.0 comes with a dictionary and set comprehensions.

Iterating through a string Using List Comprehension:

List Comprehension is not the only way to work with a list in Python, Python has lambda and related built-in functions.

Sometimes List Comprehension and sometimes using lambda functions looks more or less Pythonic in writing. we can choose the way which makes code more understandable.

Code example for the above string iteration with lambda functions.

Conditions with lambda vs List Comprehension

List comprehension sometimes is a perfect replacement for loops, lambda function as well as the functions map(), filter(), and reduce(). As I mention using comprehension and lambda both are Pythonic but It will be based on the requirement of logic. we can write the code which is easier understandable.

Optimize the loops and conditional statements

For the conditional statement, we can avoid more nesting of if-else “{ if { if {}}}” statements by writing them in a single line if possible.

Optimizing loops according to me is using comprehension and lambda expressions with a map, filter, and reduce functions for performing iteration.

As I have given examples above for map and filter functions, the reduce() function belongs to from functools module see the use of reduce function below.

This topic has full python to explore and learn so I am stopping here, I hope the article gives a thought process by the above-written points so explore more and learn more.

Happy coding!! thanks

--

--

Narendra Harny
The Startup

Connect on https://medium.com/make-android | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI