From the course: Python for JavaScript Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Iterating in Python

Iterating in Python - Python Tutorial

From the course: Python for JavaScript Developers

Start my 1-month free trial

Iterating in Python

- [Instructor] In Python, a deeper understanding of iterations may help you write code that is easier to reason about. As an added bonus, you may achieve significant performance improvements to your programs. So let's take a look at an example of this. Here, we iterate over a list that goes from 1 to 5, and for each number we print a string with the number in it and then we print the number + 1. So we can imagine these numbers on a conveyor belt, if you will, that drops these numbers into the code block where different things are done with the numbers. So looking at this example, do we need this list hard coded there in order to use this for-loop syntax? And the answer is no. We can use Python's built-in range, and a range from 1 to 6 will yield similar results. And there's a reason I say similar. We can think of this as having the same outcome, but there's an added bonus that comes with it, and let me show you what I…

Contents