List comprehensions
List comprehensions provide a concise way to create a list from the lists, tuples. grammar:
[Expression for if statement]
Creates and returns a list. if the statement is optional.
Example:
= List1 [1,2,3,4] # tuple row also List2 = [X * 2 for X in List1] Print (List2) # [2,. 4,. 6,. 8] list3 = [X * 2 +. 1 for X in List1] Print (list3) # [. 3,. 5,. 7,. 9]
= List1 [1,2,3,4 ] List2 = [X * 2 for X in List1 IF X> 2] # IF limit the scope of Print (List2) # [. 6,. 8]
= List1 [1,3,5,7] # tuple line, still get a list list2 = [2,4,6,8 ] list2 = [the X-* the y- for the X- in List1 for the y- in list2] # may be used for a plurality of statements Print (List2) # [2,. 4,. 6,. 8,. 6, 12 is, 18 is, 24, 10, 20 is, 30, 40, 14, 28, 42 is, 56 is]
matrix
Matrix can be implemented using nested lists \ tuples.
= Matrix [[l, 2,3], [4,5,6], [7,8,9], [10,11, 12]] # a 3 * 4 matrix "" ' . 1 2 3 4. 5 . 6 . 7. 8. 9 10 12 is. 11 "" " del Matrix [0] [0] # removes the first element of the first row Print (Matrix) # [[2,. 3], [. 4,. 5,. 6], [. 7 ,. 8,. 9], [10,. 11, 12 is]] del Matrix [0] # delete the first line Print (Matrix) # [[. 4,. 5,. 6], [. 7,. 8,. 9], [10,. 11 , 12 is]] matrix.clear () # empty matrix Print (matrix) # [] del matrix # delete the entire matrix # may be used to traverse the loop matrix
Formatted output
1, the common output
Print ( " the Download at The File " , end = " DONE \ the n- " ) # the Unpack DONE at The File "" " ends with the specified value, default default when the end is \ n, it will automatically wrap default end. " "" Print ( " * " * 50) # separate line effect List = [ " the Download at The File " , " the Unpack at The File " , " Open at The File " ] for the X- in List: Print (the X-, End = " done\n") """ Download the file done Unpack the file done Open the file done """
2, is converted to a string
STR = str1 (. 1) # is converted to a string Print (type (str1)) # <class 'STR'> A = 10 str2 = STR (A) # is converted to a string, a change itself Print (str2) # 10 Print (type (str2)) # <class 'STR'> Print (type (A)) # <class 'int'>
3, alignment
= str " the Hello " Print (str.ljust (20, " " )) # Left, less than 20 characters to the right padded with spaces (gather 20 characters) Print (str.rjust (20, " " )) # right-aligned when less than 20 characters, with spaces left Print (str.center (20, " " )) # when centered, less than 20 characters, with spaces both ends
4, the output format
The old version of the formatted output:
= name " Joe Smith " Age = 12 Score = 99 Print ( " % S% d years old this year, scores% .1f points " % (name, Age, Score)) # Joe Smith 12 years old, score 99.0 points . "" " print ( "format string"% (corresponding value)) when there are multiple values, the value to be placed in () If only one value may be a default ():. print ( "I S%"% name) " ' "
The new version of the formatted output:
= name " Joe Smith " Age = 12 Score = 99 Print ( " I was {0}, {1} years old this year, score points {2} " .format (name, Age, Score)) # I'm Joe Smith, this year 12 years old, score 99 points Print ( " I am {}, {} years old this year, score points {} " .format (name, Age, score)) # I'm Joe Smith, 12 years old, score 99 points {index} the index may default, default followed by 0,1,2,3 .... default Print ( " I was {0}, {1} years old this year, the results {2: .1f} points " .format (name, Age, score)) # I'm Joe Smith, 12 years old this year, scores 99.0 points can specify a specific format Print ( " I am {}, {} years old this year, the results {: .1f} points " .format (name , Age, Score)) #I'm Joe Smith, 12 years old this year, scores 99.0 points