1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

2017-04-11 Python > generatorを試してみた > generator式 / generator関数 / next()を教えていただきました

Last updated at Posted at 2017-04-10

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2749 / 12833)

generatorを試してみた。
Unityで初めてみたyieldを使うようだ。

参考: 9.9. ジェネレータ (generator)

試してみた。

def my_odds(last=10, step=1):
	number = 0
	while number < last:
		if number % 2 == 1:
		  yield number
		number += step

alist = my_odds(10, 1)
for elem in alist:
	print(elem)
結果
Success	time: 0.02 memory: 28384 signal:0
1
3
5
7
9

6.2.8 ジェネレータ式

@shiracamus さんのコメントにて「generator式」と「generator関数」の違いについて指摘いただきました。また、next()についても教えていただきました。

情報感謝です。

1
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?