What are iterable objects in python

爱喝马黛茶的安东尼
Release: 2019-06-20 09:39:44
Original
12416 people have browsed it

What are the iterable objects in Python? Iterable objects in Python include: lists, tuples, dictionaries, and strings; often used in combination with for loops;

What are iterable objects in python

Determine whether an object is Iterable object:

from collections import Iterable

isinstance(list(range(100)), Iterable)
isinstance('Say YOLO Again.')
Copy after login

List:

Related recommendations: "python video tutorial"

L = list(range(100))for i in L:
    print(i)
Copy after login

Tuple:

T = tuple(range(100))for i in T:
    print(i)
Copy after login

Dictionary:

dic = {'name': 'chen', 'age': 25, 'loc': 'Tianjin'}
# 以列表的形式返回
keylist(dic.keys())
# 以列表的形式返回
valuelist(dic.values())
# 循环key
for key in dic:
    print(key)    
# 循环value
for value in dic.values():
    print(value)    
# 循环key, value
for key, value in dic.items():
    print(key, value)
Copy after login

String:

S = 'Say YOLO Again!'for s in S:    
print(s)
返回'索引-元素'对:
for i, value in enumerate('Say YOLO Again.'):    
print(i, value)
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template