diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e97fcca15..30dbe63142 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Types of change: ### Changed - [Html - Link Relative Paths - Change part of PQ as it wasn't worder properly](https://github.com/enkidevs/curriculum/pull/2985) - [Python - Format Text Paragraphs With Textwrap - Make the fill method more clear](https://github.com/enkidevs/curriculum/pull/2981) +- [Python - Iterators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3027) ## January 4th 2022 diff --git a/python/functional-programming/iterators/the-iteration-protocol.md b/python/functional-programming/iterators/the-iteration-protocol.md index 74998ca833..a521d85162 100644 --- a/python/functional-programming/iterators/the-iteration-protocol.md +++ b/python/functional-programming/iterators/the-iteration-protocol.md @@ -34,22 +34,22 @@ Iterators are always implemented as classes. Let's examine an iterator's code fo ```python class Counter(object): - def __init__(self, start, finish): - self.current = start - self.finish = finish - - def __iter__(self): - return self - - def __next__(self): - if self.current > self.finish: - raise StopIteration - else: - self.current += 1 - return self.current - 1 + def __init__(self, start, finish): + self.current = start + self.finish = finish + + def __iter__(self): + return self + + def __next__(self): + if self.current > self.finish: + raise StopIteration + else: + self.current += 1 + return self.current - 1 ``` -We're already familiar with the `iter` and `next` methods. The `init` method is what is called when the iterator is first created, however, it is not a constructor since, the object is already created when the code within `init` is executed. Instead, this is referred to as an initializer. +We're already familiar with the `iter` and `next` methods. The `init` method is what is called when the iterator is first created. However, it is not a constructor since the object is already created when the code within `init` is executed. Instead, this is referred to as an initializer. In this `Counter` example, we can see that `init` takes the values defined by the creator of the iterator (the start and finish values) and keeps track of them. The `next` method checks to see if the iterator has gone beyond the defined `finish` value, and if not, increases the current value and returns the value before that. If the value has exceeded the `finish` value, a StopIteration exception is raised. Simple! diff --git a/python/functional-programming/iterators/the-itertools-module-ii.md b/python/functional-programming/iterators/the-itertools-module-ii.md index 9e8f8cd2df..a70cd4d28b 100644 --- a/python/functional-programming/iterators/the-itertools-module-ii.md +++ b/python/functional-programming/iterators/the-itertools-module-ii.md @@ -36,9 +36,7 @@ import itertools; letters = ['a', 'b', 'c', 'd'] numbers = [1, 2, 3, 4] -print( - list( - itertools.chain(letters, numbers))) +print(list(itertools.chain(letters, numbers))) # Result = ['a', 'b', 'c', 'd', 1, 2, 3, 4] ``` @@ -48,10 +46,7 @@ Next, `filterfalse` iterates through a collection of elements, and, given a bool ```python numbers = [1, 2, 3, 4, 5, 6, 7, 8] -print( - list( - itertools.filterfalse( - lambda x: 2 < x < 7, numbers))) +print(list(itertools.filterfalse(lambda x: 2 < x < 7, numbers))) # Result = [1, 2, 7, 8] ``` @@ -62,9 +57,7 @@ Finally, `compress()`, which takes two collections, a and b, and returns only th numbers = [1, 2, 3, 4, 5, 6, 7, 8] boolean = [1, 0, 1, 0, 1, 0, 1, 0] -print( - list( - itertools.compress(numbers, boolean))) +print(list(itertools.compress(numbers, boolean))) # Result: [1, 3, 5, 7] ``` @@ -86,26 +79,18 @@ discounts = [-30, -100, -35, -85, -15] isInSale = [1, 0, 1, 1, 1] salePrices = [] -discountIterator = -iter( - itertools.???( - discounts, isInSale)) +discountIterator = iter(itertools.???(discounts, isInSale)) -fullPricesInSale = -itertools.compress(prices, isInSale) +fullPricesInSale = itertools.compress(prices, isInSale) def f(x): price = x + next(discountIterator) salePrices.append(price) return(price <= 0) -print( - list( - itertools.???( - lambda x: f(x), fullPricesInSale))) +print(list(itertools.???(lambda x: f(x), fullPricesInSale))) -print( - list(salePrices)) +print(list(salePrices)) ``` - `compress` @@ -125,16 +110,10 @@ What is the result of the following code execution? ```python import itertools; -names = ['Tom', 'Sadiq', 'Lars', - 'Lee', 'Jean'] +names = ['Tom', 'Sadiq', 'Lars', 'Lee', 'Jean'] boolean = [1, 0, 1, 1, 0] -print( - list( - itertools.islice( - itertools.cycle( - itertools.compress( - names, boolean)), 0, 6))) +print(list(itertools.islice(itertools.cycle(itertools.compress(names, boolean)), 0, 6))) ``` ???