Quantcast
Channel: Ibrahim El Merehbi
Viewing all articles
Browse latest Browse all 108

On Readable Python Code

$
0
0

Python is an very nice high-level programming language that provides the user with an easy way to write readable and clean code. It is often the case when someone is learning a new tool or language that they are not aware of small features that would greatly improve the readability of their code.

For example, when I was learning Python I learned to iterate over a list the way I keep seeing it when reading code of beginner Pythonists. Here is an example of how you could iterate over the list:

for ix in range(len(a_list)):
    print(a_list[ix])

However, a better and Pythonic way is to use iterators:

for el in a_list:
    print(el)

This equally works for dictionaries and strings:

for key in a_dictionary:
    print(key)

for char in a_string:
    print(char)

Viewing all articles
Browse latest Browse all 108

Trending Articles