Python code indentation and looping statements 2

Source: Internet
Author: User

Let's continue with the for function.

The range () function and the Len () function are often used together for the string index, where we show each element and its index value.

#小插曲, in Cmd, the way to clear the screen is to enter the CLS, which is clean screens.

Let's analyze this statement.

foo= ' abc '

For I in range (len (foo)):

Print Foo[i], '%d '%i #值得注意的地方是, this%d, after, to add a%i, meaning,%d to take a value from the inside. [called formatted output.] ]

A ' 0 '

B ' 1 '

C ' 2 '

First output A, we want to understand foo[i] what it means. When we assign a string value to Foo, then Foo[0]=a,foo[1]=b,foo[3]=c, at this time, Len (foo) is equivalent to 3, so I first assigned a value of 0, so

Foo[i]=foo[0]=a

followed by and so on.

We have learned the comparison, then, what is the disadvantage of this code, the book said that the above program loop index, there is no loop element, then how to do?

python2.3 has a new function.

About the enumerate () function:

Through the Help function, you can know

Python code indentation and looping statements


C:\users\lege>python
Python 2.7.8 (default, June, 16:03:49) [MSC v.1500 + bit (Intel)] on win
32type "Help", "copyright", "credits" or "license" for more information.
>>> Help (Enumerate)
Help on Class enumerate in module __builtin__:

Class Enumerate (object)
| Enumerate (iterable[, start]), iterator for index, value of iterable
|
| Return an Enumerate object. Iterable must is another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| Enumerate is useful-obtaining an indexed list:
| (0, Seq[0]), (1, seq[1]), (2, seq[2]), ...
|
| Methods defined here:
|
| __getattribute__ (...)
| x.__getattribute__ (' name ') <==> x.name
|
| __iter__ (...)
| x.__iter__ () <==> iter (x)
|
| next (...)
| X.next (), the next value, or raise Stopiteration
|
|------------------------------------------------- ---------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type Object>
| T.__NEW__ (S, ...)-A new object with type S, a subtype of T

>>>
>>>

First of all, it means enumerate, enumerating the meanings enumerated.

Python code indentation and looping statements 2

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.