Getting started with Python second article

Source: Internet
Author: User

Python has two operations for slicing and iterating

Supports list and tuple of two data types

List

The first operation [0:1]: Starting with the No. 0 index, to the end of index 1, [:] Nothing is written, then starting from 0.

The second operation [::] The second semicolon refers to the frequency of sampling.

Operation of the Patch:
The so-called iteration is the loop:

#!/usr/bin/pythonb=='a': 1,'b': 2,'C': 3,'D': 5}PrintD forKeyinchD:PrintKey forValueinchd.itervalues ():Printvalue forKey,valinchD.iteritems ():PrintKey,val~

Of the Enumerate function:

    • Enumerate () is a python built-in function
    • Enumerate are enumerated and enumerated in the dictionary.
    • For an iterative (iterable)/Ergodic object (such as a list, string), enumerate makes it an index sequence that can be used to obtain both the index and the value
    • Enumerate multiple for counting in a for loop
 for  in Enumerate (['A''B''C'  ]):     print  i, value for in [(1, 1), (2, 4), (3, 9) ]:    print x, y

Examples of enumerate functions:

list=[1,2,3,4,5,6,7,8] for in Enumerate (list):    print  i,value  print list[::1]print list[::-1]

1, output index and value, 2, positive sequence output, 3 reverse output

List Builder:

 for  in range (1, onefor in'ABC' for in ' XYZ ' ]ifforif x% 2 = = 0]

In summary, is divided into three parts, the first part of the final form, the second part of the cycle, the loop can be nested, the third part is can make conditional statements

Example:

L = ['Hello',' World','Apple','None']m= [S.lower () forSinchL]PrintMN= ['Hello',' World', 18,'Apple','None']hh= [S.lower () forSinchNifIsinstance (s,str) = =True]Printhh

The print results are: [' Hello ', ' world ', ' apple ', ' none ']

Getting started with Python second 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: 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.