Python Learning Note __3.2 iteration

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1. Overview

Given a list or tuple, we can traverse the list or tuple through a for loop, which we call iteration (iteration).

of course, not only list and tuple can iterate. as long as it is an iterative object, there can be iterations, whether or not the subscript is available. including Dict, str, generator

in Python, an iteration is done through a for ... in. .

1.1, the iteration of the dictionary

# define a dictionary

D = {' A ': 1, ' B ': 2, ' C ': 3}

Key of an iterative dictionary

For key in D:

The value of the iteration dictionary

For value in D.values ():

Iterative key-value pairs

For K, V in d.items ()

1.2. String iteration

For ch in ' ABC ':

1.3. List Iteration

# define a list


Subscript of the Iteration list

For I in L:

Print (l.index (i))

The value of the iteration list

For in L:

Print (i)

The index of the iteration list and the element itself

For I, value in enumerate (L): # Enumerate function can turn a list into an index-element pair C7>for i in L:

Print (L.index (i), i)

Two iterations of a variable

For x, y in [(1, 1), (2, 4), (3, 9)]:

2. Examples

Write a function that uses iterations to find the minimum and maximum values in a list and return a tuple:


def findminandmax (L):

If Len (L) ==0:

Return (none, none)



For value in L:

If value > Max:


Elif Value < min:


Return Min,max

Python Learning Note __3.2 iteration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.