Python Learning Notes (iii)--Iteration

Source: Internet
Author: User

    • Knowledge points
    1. Iteration

    • Iteration

What is an iteration? What's the difference between being recursive and recursion?

According to Wikipedia and online information:

Iterations: iterations are activities that repeat the feedback process and are often intended to close and reach the desired goal or result. Each repetition of the process is called an "iteration",

The result of each iteration is used as the initial value for the next iteration.

If the 1+2+3+4+5 and:

What about recursion? Refers to the method of using the function itself in the definition of a function. That is, the function keeps calling itself until a certain condition is met.

And how is recursion to seek 1--5?

Recursive and iterative implementations of the Fibonacci sequence:

1 deffib (n):2     ifN>0:3         if(n==1orn==2):4             return15         Else:6             returnFIB (n-1) +fib (n-2)7     Else:8          return-19 deffib_2 (n):Ten     ifn<=0: One         return-1 A     Else: -A=1 -B=1 theS=1 -          forIinchRange (n-2): -s=a+b -A=b +b=s -         returns + PrintFIB (int (raw_input ('Please input a number from want to calculate the FIB number:')))         A Printfib_2 (int (raw_input ('Please input a number from want to calculate the FIB number:')))

    • Which of the python is iterative?

The way in which objects are scanned from left to right is iterative .

How can you tell if an object is not iterative?

That's what StackOverflow said. Http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable

    1. Using Python's built-in function, Hasattr, hasattr (object,name) Returns True when object has the Name property. The Iteration property is __iter__.

2. Use the iterable type of the collections module to determine

    • For general iterative types, you can iterate over them with for-in
    • But what about a dictionary with key-value pairs? For value values, the Itervalues () function is required to manipulate the value
1capitals={' China':'Beijing','America':'Washington','Russia':'Moscow'}2 Print 'countries:'3  forCountryinchCapitals:4     PrintCountry5 Print 'Capitals:'6  forCapitalinchcapitals.itervalues ():7     PrintCapital

You can see that when the dictionary iterates, the resulting values do not necessarily follow the order.

Python Learning Notes (iii)--Iteration

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: info-contact@alibabacloud.com 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.