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