Home  >  Article  >  Backend Development  >  What are iteration and iteration objects in python?

What are iteration and iteration objects in python?

乌拉乌拉~
乌拉乌拉~Original
2018-08-22 16:18:044063browse

In the following article, we will learn about iteration in python. Understand what iteration means and what role it can play in python programming.

What is iteration in python

If a list or tuple is given, we can traverse the list or tuple through a for loop, this This kind of traversal is called iteration (Iteration).

(In Python, iteration is done through for...in)

Python’s for loop is more abstract than C’s for loop because Python's for loop can be used not only on lists or tuples, but also on other

iterable objects.

(Objects that can be directly used in for loops are collectively called iterable objects (Iterable), such as list, tuple, dict, set, str, etc.)

list Although this data type has subscripts, many other data types do not have subscripts. However, as long as it is

python's iterable object, it can be iterated regardless of whether it has a subscript or not. , for example, dict can be iterated:

>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> for key in d:...     print(key)
...
a
c
b

Because dict is not stored in the order of a list, the order of iterated results is likely to be different.

By default, dict iterates over key. If you want to iterate value, you can use for value in d.values(). If you want to iterate key and value at the same time, you can use for k, v in d.items().

Since strings are also iterable objects, they can also be used in for loops:

>>> for ch in 'ABC':
...     print(ch)
...ABC

So, when we use for loops, as long as they act on an iterable object, the for loop It can run normally, and we don't care much whether the object is a list or another data type.

So, how to determine whether an object is an iterable object? The method is to judge by the Iterable type of the collections module:

>>> from collections import Iterable
>>> isinstance('abc', Iterable) # str是否可迭代
True
>>> isinstance([1,2,3], Iterable) # list是否可迭代
True
>>> isinstance(123, Iterable) # 整数是否可迭代
False

The last little question, what if you want to implement a subscript loop similar to Java for the list? Python's built-in enumerate function can turn a list into an index-element pair, so that the index and the element itself can be iterated at the same time in a for loop:

>>> for i, value in enumerate(['A', 'B', 'C']):
...     print(i, value)
...
0 A
1 B
2 C

In the for loop above, two variables are referenced at the same time, It is very common in Python, such as the following code:

>>> for x, y in [(1, 1), (2, 4), (3, 9)]:
...     print(x, y)
...
1 1
2 4
3 9

The above is all the content described in this article. This article mainly introduces the iteration and iteration objects in

python knowledge, I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the

Python tutorial column on the php Chinese website.

The above is the detailed content of What are iteration and iteration objects in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn