# 有StopIteration的情况

``````it = iter([1,2,3,4,5])
while True:
try:
#获取下一个值
x = next(it)
print(x)
except StopIteration:
#遇到StopIteration就退出循环
break
`````` # 没有StopIteration的情况

``````it = iter([1,2,3,4,5])
while True:

#获取下一个值
x = next(it)
print(x)
# except StopIteration:
# 	#遇到StopIteration就退出循环
# 	break
`````` # 在next()中增加第二个参数

``````it = iter([1,2,3,4,5])
while True:

#获取下一个值
x = next(it,None)
print(x)
if x == None:
break
# 	# except StopIteration:
# 	# 	#遇到StopIteration就退出循环
# 	# 	break
print("hahahaha")
`````` # 执行一次next()输出多少个元素:一个

## case one 以迭代器的形式

``````it = iter([1,2,3,4,5]) # 以迭代器的形式
print(next(it,None))
`````` ## case two 以列表形式作为输入: 不可以

``````it = [2,1,3,4,5]
print(next(it,None))
`````` ## case three 以元组形式作为输入： 不可以

``````it = (1,2,3,4,5)
print(next(it,None))
`````` ## case four : 利用iter + for 可以

``````it = iter([1,2,3,4,5])
a = next((name for name in it),None)
print(a)
`````` ## case five: （name for name in a）的数据类型到底是什么？：generator

``````a = [1,2,3,4,5]
b = [4,5,6,7,8]
print(name for name in a if name not in b)
c = (name for name in a if name not in b)
print("the type of c:",type(c))
print("the output of next() function:",next(c,None))
`````` ## 相关:  