Python basic summary article

Variables and Data Types

Numbers digital divided: int int, long long integer, float float, complex complex

String character string by the string of numbers, letters, the underscore, for indicating the data type of text

bool Boolean: True, False, for making a judgment (True actual value is 1, False actual value is 0)

List List support characters, numbers, strings containing a list (i.e. nested), with [] identification, sequence object

Tuple tuples, with () ID, not the second assignment, as will be appreciated immutable list (read-only list), ordered objects

Dict dictionary, with {} identified by the index (key) value and a value corresponding to its composition, unordered objects

Simple data type conversion

  • int (x) function: The x into an integer, if rounding is round (x) function
  • float (x) function: x into the float
  • STR (x) function: into a string of x

Sequence and strings

Sequence and the general procedure

Print (LST1 + lst2)   # "+": the sequence link 
Print (LST1 *. 3, lst2 * 2) # "*": sequence repeats
Print (LST [0], LST [2], LST [. 9]) # Index starts from 0, EG lst [2], the subscript index is 2, the third point value lst
Print (lst [-1]) # index value -1 is the last
Print (lst [2:. 5]) # slice will be appreciated that the value of the interval as a list, and a left-closed right-open interval, where lst [2: 5] interval represents: index 2 - the index value of 4
Print (LST [0:. 5: 2]) # List [I: J: n] Representative: index i - index j, as in steps n

Feature list

  • It may comprise any type of objects: numbers, strings, lists, etc.
  • By sequentially access sequence index value index → ​​sequence can not be changed, through the index to locate an element of the list
  • Be free to change the length of the list, the new list corresponding to the specified arbitrary
  • Nestable
  • Situ change
  • List generator: range () is a generator, it points to a range #range (0,10,2) of the representative points 0,2,4,6,8 these values ​​represents the final step

 The variable list list common operations

  • list.append () to add elements
  • Add multiple elements with .extend () method: with a new list of extensions of the original list (direct expansion)
  • .remove method: remove the first occurrence of a value in the list
  • del statement: delete the list of corresponding index values
  • lst.clear () : Removes all values
  • x.insert (i, m) Method: inserting the index i m, i where the index of (i + 1) th value represents
  • list.copy () method: Copy a new list (pointing to the two lists)
  • .sort () : Default ascending sort / alphabetically (parameter reverse: reverse the order, for digital)
  • Function sorted () : Sort and copy

String associated notes

  •  Double quotes single quotes no difference, but when a quoted text to alternately use
  • Need more time with three rows quoted string '' '' '' "" "" ""

Escape character

  •  \ ", \": Outputs single double quotes
  • \ N: an empty line
  • Output \, the file path with "/" or "\\"
  • Extending - # local file read and write data to define: a point to locally stored file, or a link to a map
  • path1 = 'C: /Users/Hjx/Desktop/text.txt' # single backslash: /
  • path2 = 'C: \\ Users \\ Hjx \\ Desktop \\ text.txt' # two slashes: \\ (first \ is the escape character)
  • path3 = r'C: \ Users \ Hjx \ Desktop \ text.txt '# r for preventing the escape character

Sequence common functions

  • in / not in: determining whether there is
  • Text Links: print ( 'I am handsome' + "Yes"
  • Copy Text: print ( 'handsome' * 4)
  • .index () Method: print (st.index ( 'g'
  • String length is calculated: len ()

Common features of the string

  • .replace (Old, new new, COUNT) : modify the string, count: replace several
  • .split (obj) : split string, a list of
  • .join () : the connection string, object list
  • .startswith ( "xie")  determines whether to "xie" begin; if str.endswith ( "xie") is determined to "xie" end
  • .upper () all caps
  • .lower () all lowercase
  • .swapcase () invert case
  • .capitalize ()  first letter capitalized
  • .isnumeric () If the string contains only digit returns True, otherwise False
  • .isalpha () if there is at least one character string and all the characters are the letters return True, otherwise False
  • .rstrip () delete the character at the end of the space
  • % s% i% f are formatted string, s / i / f represent the source data string is inserted Type: integer / character / float
  • % +. 2f   output plus two decimal places and displays % e% E 
  • % g less scientific notation of decimal floating-point number automatically when the identification data when the automatic recognition complex scientific notation
  • # More powerful formatting method .format 
    
    Print ( " the User ID: {0} " .format ( " the root " ))
     Print ( " {}} Oh { " .format ( " A " , " B " ))
     # { } here placeholder for which there may be no digital 
    
    Print ( " {} {} {} " .format ( ' A ' , ' B ' , ' C ' ), ' \ n- ' ,
     "{0}{1}{2}{0}".format ( ' A ' , ' B ' , ' C ' ), ' \ n- ' )
     # Print ( "{} {} {} {}." the format ( 'A', 'B', 'C')) 
    # {0} and {} distinction: are placeholders, which has explicitly 
    
    Print ( " my job is work} { " .format (= work ' design ' ))
     # can also be used to indicate a variable

     


Guess you like

Origin www.cnblogs.com/dabaige/p/11800409.html