Python [day 15] the basic data types -int str bool list

'' '' '' 
'' ' 
Defined in the Python 1 
    is an explanatory weak type level programming language 
    Advanced here is relatively lower (for example: assembly language, etc.) 

    high-level and low-level programming language programming language, the difference between 
    a former closer to human understanding - grammar letters 
    2, which is closer to understanding calculator - byte code, binary 

characteristics of python 2 
    life is short, I use python 
    concise 

Why learn python 3 
    hobby, artificial intelligence , machine learning, large data 

. 4 python first program 
    print ( 'hello world') 
    Method 1: enter in cmd python, the interactive command line, enter the above statement, enter 
    mode 2: Create a local disk d hello. py file, copy the file .py to the above statement, after storage 
           in cmd, into the disc d, Run python hello.py 
    Embodiment 3: After the above statements are written PyCharm, RUN 

. 5 the concept of variable 
    variable program is running intermediate value generated --temp (temporary attribute, open a memory for storing a value) 
        1, memory space name is the name of the variable 
naming 6 variables: 
        2, save memory space Value is the value of the variable
        For example: a = 10 variable name is the value of a variable is 10

    1, consists of: numbers, letters, underscores 
    2, beginning: can not start with a number 
    3, Keywords: Keywords can not be, for example: List 
    4, length: not too long 
    5, Chinese: Chinese is not 
    6, meaningful: to interesting (a look at the name, you probably know the meaning of the variables indicated) 
    7, case: strictly case-sensitive 
    8, writing: recommended underlined or hump law named 
        underlined: hello_world (between words, separated by an underscore) 
        hump method: the helloWorld (not capitalized first letter, starts the second word, the first letter capitalized) 

data type of the variable 7 
    an integer int + - * / // BIT_LENGTH () 
    2 string STR 
        1, represented by '' \ '' \ ' "" Precautions: \ escape herein indicates 
            a single quotation marks '' 
            two pairs of marks "" 
            3 three single quotes 
        2, repeated * 
        3 + stitching 
        4,% s format'' '  ' '' 
            4 three double quotation marks "", "", "" 
        5, indexing and slicing 
            S1 [ start: end:step] left and right open closed 
            1 start start index - subscript 
            2 end end index - subscript (not eligible) 
            . 3 Start step (not written, is 1 by default) 

        6, common built-in method 
            1 Upper () to uppercase 

            2 strip () to remove the string ends of the blank 
               strip ( 'ab') removing the ends of the string 'ab &' 

            3 replace (Old, new new, COUNT) 
                parameter 3 from left to right COUNT represents the number of alternative 
                parameters 3 is not written, the default is to replace all 

            4 split () splits the string into several sub-strings, each substring as elements of the list, returns a list of 
                default separator is a space 
                split ( '_') delimiter is underlined 

            5 '_'. join () the parameter iterable strings through a connector '_' splice into strings 
                written: 'connector' .join (iterable) 
                Note point: 
                    . 1 Iterable the elements must be str, int is not
                    Method 2 join is not built string 

            6 startswith () 
                written: s1.startwith ( 'a') 
                action: determining whether the string starts with the letter 'A' 

            . 7 Find index COUNT 
                written: s1.find ( 'a') 
                acting: Find character string s1 'a', returns the index number; if not, it returns -1 - more robust recommended 

                wording: s1.index ( 'a') 
                acting: Find the character string s1 'a ', returns the index number; if not, then an error 

                writing: s1.count (' a ') 
                role: from statistics character strings s1' number of occurrences of a ', the number of int return to appear 
                    if it is 0, representatives not found (there are functions to find) 
            8 isdigit () 
                writing: s1.isdigit () 
                function: to determine whether a string is in digital form, it is, returns True 

            9 len () 
                writing: len (s1)
                Action: Returns the length of the string 
 
        7, string Iterable 
            for I in S1:
                loop (BREAK Continue) 
            the else: 
                Pass 

        . 8, the encoding string 
            1 ascii 8 bits 1 byte 
            2 gbk gb2312 16 GB code bit 2 of byte 
            3 unicode Unicode 32 4 byte 
            4 utf-8 Unicode variable 
                . 1 English one byte characters 8 
                2 2 16 European character bytes (German, French, Spanish, etc. character) 
                3 24 3 Chinese character byte 
            string bytes into byte encoded 
                coding - encryption - compression 
            after byte string decoded into 
                decoded - - after extracting 
                bytes.decode () # decoder 

    3, bool Boolean type 
        values: True False 
        role: for determining the 
        type of conversion : 
            False: 0 or empty ( '' [ ] () {} set () None) } set () None) 
                        empty string, an empty list, empty tuple, empty dictionary, a null set, null None 
                Note: space, \ r \ n \ t are true 
            true: above, in addition to others are true 
            Precautions: type conversion may be used directly Analyzing False 
        Notes: 
            examples. 1 
            content1 = iNPUT ( 'enter your name: ") # input string 
            if content1: # Analyzing If you do not enter an empty content, directly enter, to suggest - you do not pay attention to the name of the point: the results of the following line and not the same as 
            # if content1 == '': # If you do not enter content directly enter, print - your name is the 
                print ( 'your name is', content1) 
            the else: # If you do not enter anything, this is an empty string content1 
                print ( 'you no name') 

            example 2 
            of Li1 = [, 2, 3] 
            IF of Li1: # here is deciding whether the list is empty, and the following line equivalent
            IF in Li1 == # []: 
                Print (in Li1) 
            the else: #  
                print ( 'Empty list ')

    . 4, in list 
        represents: [], are separated by commas between the elements of 
        the concept: a listing is a container that can store any data type 
            array array and other development languages similar 
        features: 
            1, the list is variable 
            2, the indexing and slicing function list 
        common operations: 
            1 by 
                1 the append (element values) 
                    in the list of additional elements rearmost 
                2 insert (i, element values) 
                    in the list before the specified position of the insertion element 
                3 extend (iterable) 
                    the elements in iterable in turn added to the original list of 
            2 deleted 
                1 pop () 
                    parameter does not write, the default is to remove the last element
                    Parameter may specify the index number, to remove 
                2 remove 
                    the element Removes the specified value 
                3 del 
                    slice or delete a single 
                4 Clear 
                    1 pop
                    Empty element 
            3 to change 
                an index according to - subscript modified 
                    slice modified 
                    single index modification 
            4 check 
                1, a single index value 
                2, for looping through 

                01 list - Summary adding elements 
                    1 at the end of the append is to add a new element - the most commonly 
                    2 insert is before the specified location to insert a new element 
                    3 extend is sequentially added iterable elements to the original list of 
                02 list - delete an element summary 
                        parameters: element value (not position) 
                        effect: Deletes the specified element value
                        Without parameters, default to the last element of the deletion list, and acquires the last element to be deleted 
                        specified parameters, to delete the specified location - the element the element index number, and acquires the specified location of the deleted 
                    2 Remove 
                            wording: del dictionary name [key]
                    Del 3 
                        . 1 Parameters: single location - index 
                          wording: del list (position) 
                          effect: to delete a single location element 
                        2 parameters: Slice 
                          written: del list (sliced) 
                          action: a plurality of slice locations remove elements 
                        3, arguments: name 
                           written: del list name 
                           role: delete the entire list, recovery of memory space (empty clear and are different) 
                        4 parameters: key dictionary 
                            function: delete the specified key dictionary corresponding key-value pair 
                        summary, the role of del 
                        1, individual elements can be deleted position 
                        2, you can remove a plurality of slice elements 
                        3 can delete the entire list, recovering memory space
                        4, not only a list, also applies to the dictionary 
                    4 clear 
                        parameters: null 
                        wording: li1.clear () 
                        function: to clear the elements of the list into an empty list 
                             empty - a list of the memory space is not recovered (and del li1 not the same) 
                list 03 - Summary modifications element 
                    1 position modified single - single element Review 
                        written: li3 [1] = 'james ' 
                        effect: the designated location - a list of index modifications individual elements 

                    2, a plurality of elements to modify the slice 
                        12 elements change elements into a [ 'james', 'kevin'] 
                        32 elements into three elements 
                            the correct wording: li3 [1: 3] = [' james']
                            Error writing: li3 [1: 3] = 'james' attention point 

                        22 element into two elements 
                            written: li3 [1: 3] = 
                3 sort () to sort 
                    the list of elements to sort in ascending order by default, the list itself modified 
                4 reverse () inverted

                            Wording: LI3 [1:. 3] = [ 'James', 'Kevin', 'Lucy'] 
                04 List - Summary of query elements in 
                    a single query - single value - according to a single location - index - subscript 
                    plurality of query 2 - multiple values - in accordance with sections 
                    3 all inquiries - all values -for loop through 

            5, other common operations 
                1 count () counts 
                    the number of calculations appear in the list an element 
                2 index () lookup 
                    returns an element in the list position - the index - index 
                    to the list of elements is reversed, modify the list itself 

            6, the process loops through the list, you can not remove elements, because it involves moving element 
               solution: 
               the elements to be deleted, added to the new list, circulating a new list, delete the old list 

            7, the depth of copying and assignment 
                1 = assignment, two variables point to the same memory address, different variables but different aliases for the same memory address
                2, shallow copy, copy only the first layer (second layer and above point to the same memory space is not achieved copies) 
                    li1.copy () 
                    in Li1 [:] 
                    test whether the memory space is a way: See the value of the variable id 
                3, deep copy, copy only the first layer, the second layer and above are copied 
                    li1.deepcopy () 
                extension: in other programming languages (such as: Java), named clone, also cloned into the depth 

                shallow deep copy of analogy 
                1, a shallow copy is separate from the first memory layer, the second layer and above the same memory space is a 
                    [the results for the cycle if the judge] 
                example: 
                         - a first separate layer, upper layer, and a second change has changed
                          The first layer is the family financial couples AA 
                          second layer is a joint account set up by husband and wife family 
                2, a deep copy is separate from the first memory layer, the second layer and above separate memory space - each is completely independent 
                          analogy: Family couple Finance complete AA, does not have a joint account 

            8, list comprehensions 
                written
                    = LI7 [l, 2,3] 
                    # demand: the odd list, the number of squares which becomes 
                    LI8 = [I * for I I I in LI7% 2 == IF. 1] 
                    Print (LI8) # [. 1,. 9 ] 

'' '

 

Guess you like

Origin www.cnblogs.com/wangtp/p/11832658.html