Skip to content
Advertisement

what is the difference and uses of these lines of code?

first line of code:

JavaScript

second line of code:

JavaScript

what would I use each of them for?

Advertisement

Answer

You can see for yourself what the difference is. The first one iterates over range and then prints integers.

JavaScript

The second one is a generator expression.

JavaScript

How iteration works in the generator. Python generators are a simple way of creating iterators.

Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).

JavaScript

In python3, you can use tuple unpacking to print the generator. If that’s what you were going for.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement