By Rahul Agarwal

Published: 2019-04-23

URL: https://www.mlwhiz.com/p/python_forloops


Python provides us with many styles of coding.

In a way, it is pretty inclusive.

One can come from any language and start writing Python.

However, learning to write a language and writing a language in an optimized way are two different things.

In this series of posts named <strong>Python Shorts</strong> , I will explain some simple but very useful constructs provided by Python, some essential tips and some use cases I come up with regularly in my Data Science work.

In this post, I am going to talk about for loops in Python and how you should avoid them whenever possible.

3 Ways of writing a for loop:

Let me explain this with a simple example statement.

Suppose you want to take the sum of squares in a list.

This is a valid problem we all face in machine learning whenever we want to calculate the distance between two points in n dimension.

You can do this using loops easily.

In fact, I will show you\\ three ways to do the same task which I have seen people use and let you choose for yourself whic…