Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define iteration in for Loop in Python

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to define iteration in for cycle in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to define iteration in for cycle in Python" can help you solve your doubts.

For cycle of Python

Python is a collection-based iteration.

For in: # is a collection of objects-- for example, lists or tuples. # body of the loop a = ['Cao Cao', 'print', 'equipment'] for i in a: print (I)

Output:

Cao Cao

Cymbals

Equipment

An iterable object that can be passed to the built-in Python function iter () for processing and returned as an iterator

Iter ('Cao Cao Yaoyi') # Stringiter (['Cao Cao', 'Yueji', 'Jiaoji']) # Listiter (('Cao Cao', 'Yueyi', 'Jieqi') # Tupleiter ({'Cao Cao', 'Yueji'}) # Setiter ({'Cao Cao': 1 "Dict": 2, "equipment": 3}) #

Objects that cannot be iterated

Iter (1) # IntegerTraceback (most recent call last): File ", line 1, in TypeError: 'int' object is not iterableiter (1.1) # FloatTraceback (most recent call last): File", line 1 In TypeError: 'float' object is not iterableiter (len) # Built-in functionTraceback (most recent call last): File ", line 1, in TypeError:' builtin_function_or_method' object is not iterable

An iterator is a value producer that produces continuous values from its associated iterable objects. The built-in function next () is used to get the next value from the iterator.

List_ = ['Cao Cao', 'itr', 'itr'] itr = iter (list_) itrnext (itr), 'Cao Cao' next (itr), 'next (itr)'

Use the built-in list (), tuple (), and set () functions to get all values from the iterator at once.

List_ = ['Cao Cao', 'Jiaoyi', 'Jiaojie'] itr = iter (list_) list (itr) ['Cao Cao', 'Jiaoyi', 'Jiaoqi'] itr = iter (list_) tuple (itr) ('Cao Cao', 'Jiaoyi', 'Jiaoqi') itr = iter (list_) set (itr) {'Jiaogui', 'Cao Cao', 'Jiaoyi'} traversing the dictionary

A dictionary traversal loop in which loop variables are assigned to dictionary keys.

D = {'Cao Cao': 1, 'equipment': 2, 'equipment': 3} for k in d: print (k)

Output:

Cao Cao

Cymbals

Equipment

The operation of accessing dictionary values

For k in d: print (d [k]) # output: # 1#2#3for v in d.values (): print (v) # output: # 1 / 2 / 3

Traverse the keys and values of the dictionary at the same time. The loop variable for is not limited to a single variable, but can be a tuple.

I, j = (1,2) print (I, j) 1 2for I, j in [(1,2), (3,4), (5,6)]: print (I, j)

Output:

1 2

3 4

5 6

The dictionary method items () effectively returns the list of key / value pairs as a tuple.

D = {'Cao Cao': 1, 'weapon': 2, 'equipment': 3} d.items () dict_items ([('Cao Cao', 1), ('bike', 2), ('Jia Jie', 3)]) d = {'Cao Cao': 1, 'Qing': 2,'Qi': 3} for k, v in d.items (): print ('k =', k,', v =', v)

Output:

K = Cao Cao, v = 1

K = recently, v = 2

K = equipment, v = 3

Range () function

Python provides a built-in range () function that returns an iteration that produces a sequence of integers.

Range () returns an iteration that produces an integer that begins with 0, up to but not including the numeric value

X = range (5) xrange (0,5) type (x)

Range objects are iterable, so you can iterate through them through for loops to get values.

For n in x: print (n)

Output:

0

one

two

three

four

List () and tuple () get the values of all the loop traverses at once.

List (x) [0,1,2,3,4] tuple (x) (0,1,2,3,4)

Range (,) returns an iteration result.

List (range (5, 20, 3)) [5, 8, 11, 14, 17] # defaults to 1list (range (5, 10, 1)) [5, 6, 7, 8, 9] list (range (5, 10)) [5, 6, 7, 8, 9] # the numerical range must be list (range (- 5, 5)) [- 5, 4,-3,-2,-1, 0, 1, 2, 3, 4] list (range (5) -5)) [] list (range (5,-5,-1)) [5, 4, 3, 2, 1, 0,-1,-2,-3,-4] break statement and continue statement

Break terminates the loop completely.

For i in ['Cao Cao', 'Fengyi', 'Jijie']: if'in I: break print (I)

Output:

Cao Cao

Continue terminates the current iteration and proceeds to the next iteration.

For i in ['Cao Cao', 'Fengyi', 'Jijie']: if'in I: continue print (I)

Output:

Cao Cao

Equipment

Else statement

The clause else of the for loop is similar to the use of the while loop.

For i in ['Cao Cao', 'Jiaoyi', 'Jijie']: print (I) else: print ('over.')

Output:

Cao Cao

Cymbals

Equipment

End.

Else will not be executed if the list is break by a statement.

For i in ['Cao Cao', 'Jiaoyi', 'Jijie']: if I = = 'thanks': break print (I) else: print ('over.')

Output:

Cao Cao

After reading this, the article "how to define iterations in for Loop in Python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report