To determine the syntax of a looping statement:
1,if
If Boolean_expression:
...
Elif Boolean_expression:
...
Else
...
2,while
While Boolean_expression:
While_suite
If Boolean_expression2:continue
If Boolean_expression3:break
Else
Else_suite
3,for
For expression in object:
For_suite
If Boolean_expression2:continue
If Boolean_expression3:break
Else
Else_suite
While True:
x = Raw_input (' Enter an entry: ')
if x = = ' Q ' or x = = ' Quit ':
Break
Else
Test.append (x)
List resolution and builder expressions:
The difference between the two:
1, list parsing using [], generator using ()
2, list resolution all production, if the volume of data is large, compared to consumption of resources, and the generator each time an entry, this entry yield, high efficiency, low memory consumption. (similar to Range,xrange)
List parsing
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m02/7d/eb/wkiol1bztr-s7yfsaactt3ud34w977.png "title=" 1.png "alt=" Wkiol1bztr-s7yfsaactt3ud34w977.png "/>
Example 1: list files for all. Log suffix names in the/var/log directory
s1= ' Hello.log '
S1.endswith ('. log ') #endswith () function to determine the suffix name
True
Import OS #加载os模块, there are many
Filelist1 = Os.listdir ('/var/log ') #os. Listdir () function lists all files in the directory
Help (Str.endswith) #查看帮助
Filelist2 = [i-I in Filelist1 if I.endswith ('. log ')]
Print Filelist2
[' Boot.log ', ' pm-powersave.log ', ' anaconda.yum.log ', ' xorg.0.log ', ' anaconda.ifcfg.log ', ' xorg.9.log ', ' Anaconda.storage.log ', ' dracut.log ', ' yum.log ', ' spice-vdagent.log ', ' wpa_supplicant.log ', ' anaconda.log ', ' Anaconda.program.log ']
Example 2: Implementing the L1,L2 list once in the opposite order
L1 = [' x ', ' y ', ' Z ']
L2 = [A]
L3 = [(i,j) for I in L1 for J in L2]
Print L3
[(' X ', 1), (' X ', 2), (' X ', 3), (' Y ', 1), (' Y ', 2), (' Y ', 3), (' Z ', 1), (' Z ', 2), (' Z ', 3)]
Builder expression
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/EB/wKioL1bzTTjw_UCQAAGQVYXZ3NE967.png "title=" 2.png " alt= "Wkiol1bzttjw_ucqaagqvyxz3ne967.png"/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Grammar:
(Expr for Iter_var in iterable)
(Expr for Iter_var in iterable if cond_expr)
exponentiation operation
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/EB/wKioL1bzTUzwuODPAAA80ylmyv4202.png "title=" 3.png " alt= "Wkiol1bztuzwuodpaaa80ylmyv4202.png"/>
Gets the index and the value itself enumerate, this function can get the column index and value, you can generate a dictionary
url = ' www.baidu.com '
in [+]: en
%env Enumerate
in [+]: Enumerate (URL)
OUT[84]: <enumerate at 0x1eb81e0>
In [G1]: = Enumerate (URL)
In [the]: G1.next ()
OUT[86]: (0, ' W ')
In [All]: G1.next ()
OUT[87]: (1, ' W ')
in [+]: G1.next ()
OUT[88]: (2, ' W ')
In []: G1.next ()
OUT[89]: (3, '. ')
Python Basics (3)--conditional judgment Loop statement and list parsing, generator