A triplet of expressions and a list comprehension in python

 A triplet of expressions:

1 age = 10
2 name = "tom" if age ==10 else "jane"
3 print(name)  #tom

 List comprehension:

1 LS = [ " Male} { " .format (I) for I in Range (10 )]
 2  Print (LS)
 . 3  '' ' 
. 4  Output: [' handsome 0 ',' guy 1 ',' 2 guy ' 'guy 3' 'guy 4', '5 guy', 'handsome 6', '7 guy' 'guy 8', '9 guy']
 5  '' '

It is actually the following abbreviations:

1 ls =[]
2 for i in range(10):
3     ls.append("帅哥{}".format(i))
4 print(ls)

The list can also add a condition when parsing (expressed eligible to be added to the list)

. 1 LS = [ " Male} { " .format (I) for I in Range (10) IF I>. 5 ]
 2  Print (LS)
 . 3  '' ' 
. 4  Output: [' handsome 6 ',' 7 guy '' guy 8 ',' 9 guy ']
 5  ' ''

 

Guess you like

Origin www.cnblogs.com/zach0812/p/11320525.html