课程: Python: Design Patterns (2021)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Iterator example

Iterator example

- [Instructor] Now, let's define our iterator. We start with creating a list. The list contains German words counting up to five. Here, we'll use Python's built-in iterator. The iterator will create tuples consisting of pairs of numbers and equivalent German words. For example, one and the German word, eins. The tuple here means a pair. One and eins that's a tuple. Two and zwei. That's another tuple. The built-in iterator provided by Python is zip. We have to generate numbers up to five. So we use the function range here. So type range. The argument of the function is the number of German words to display, which is count. So type count. This is the first argument of the zip iterator. The second argument is the list itself. The numbers in German is our list. Type numbers in German. The range function here is important, because it allows us to know up to what number we can count. We use a variable called iterator…

内容