Python (eight) advanced features

Source: Internet
Author: User

Iteration (Iteration)

To judge an object to iterate:

1. You can traverse through the for loop

2. Judging by the iterable type of the collections module

What if you want to implement subscript loops like Java for a list:

Python's built-in enumeration method, enumerate, turns a list into an index-element pair

List-Generated

The result of the For loop output (after all integers in output 1-10 are squared):

in [+]: [i**2 for I in Range (1,11)]
OUT[41]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
For Loop +if statement (all integers between output 1-10 squared, followed by 2 with the result of 0):
In [all]: [i**2 for I in range (1,11) if (i**2)%2==0]
OUT[42]: [4, 16, 36, 64, 100]
For loop +for loops (full array of output ' abc ' and ' 123 '):
in [+]: [I+j for I in ' ABC ' for j in "123"]
OUT[46]: [' A1 ', ' A2 ', ' A3 ', ' B1 ', ' B2 ', ' B3 ', ' C1 ', ' C2 ', ' C3 ']





Python (eight) advanced features

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.