Python study notes - the basics summary 01

1. What Python for

   a. Web sites and a variety of network services

   b. The system tools and scripts

   c. As a glue language to another language developed modules packaged for ease of use

   Python does not fit what

   a. code is not suitable for hardware close

   b. is not suitable for mobile development

   c. Game Development

   Python Cons: Source can not be encrypted

2. Python data types

    a. Python data types include plastic , float , string (may be '' or '' enclosed), Boolean , null None

        . Ps For strings, generally in single quotes' 'enclosed, but if the string includes the time (') and other special characters in combination, may be used double quotation marks "" and escape character (\) for processing;

    b. Raw string (if a lot of need to escape a string of characters, each character will have escaped a lot of trouble, you can use raw string, before the string plus a r, so inside you do not need the escape character):

        Example: print r '\ (~ _ ~) / \ (~ _ ~) /'

    . c Multiline strings can be used '' '...' '' represents:

        Example: '' 'Line 1

                 Line 2

                 Line 3'''

              Are exactly the same representation of the string above and below are:

              'Line 1 \ nline two \ nline 3'

   . d string encoding :

        Since the birth of the release of Python Unicode standard than the time even earlier, so the earliest Python only supports ASCII encoding, normal string 'ABC' inside Python are ASCII encoding. Python later adds support for Unicode and Unicode strings to represent the '...' is represented by u, for example: print u 'Chinese'

         Ps. If you encounter UnicodeDecodeError Chinese string at the Python environment, which is saved because .py file format issue. Can add a comment on the first line # - * - Coding: UTF-. 8 - * -  . Python purpose is to tell the interpreter to read the source code in UTF-8 encoding. Notepad ++ then select Save As ... and save the UTF-8 format.

    e. multi-line unicode

        u'''Line 1

            Line 2'''

     f. ur combination

        ur '' 'Python Unicode strings support the "Chinese"

       "Japanese"

       "Korean" other languages' ''

3. Python interpreter prompt, not part of the code:

    >>>

4. print statement

    . A print statement to keep a plurality of strings, a comma "," separated, outputs can be concatenated, such as: '! World' print 'Hello',

        Ps.print statement encounters a comma "," will output a space,

    b Print the results:. print 75 +25

5. Comment # 

6. variables

    . A variable letters (uppercase and lowercase), a combination of numbers and underscores, and can not begin with a number;

    . B Python is a dynamic language;

         Dynamic languages: the variable itself is not the type of fixed assets language, otherwise it is a static language;

    . C variables in memory, said:

        Such as: a = 'ABC', Python to a 'ABC' of the strings created in memory and built in a variable named a memory, and point 'ABC';

   . D Example variables:

  a = 'ABC'
  b = a
  a = 'XYZ'
  print b

      The last line prints out the contents of the variable b in the end is the 'ABC' or do 'XYZ'? If you understand the mathematical sense, mistakenly come to the same b and a, it should be 'XYZ', but in fact the value of b is 'ABC', let's execute the code line by line, you can see happen in the end What happened:

      Execution a = 'ABC', the interpreter creates a String 'ABC' and a variable, and a point to the 'ABC':

      

     Execution b = a, the interpreter creates the variable b, a pointer to the string and the points b 'ABC':

     

    Execution a = 'XYZ', the interpreter creates a string 'XYZ', and point instead to a 'XYZ', but b does not change:

     

    Therefore, the final print variable bresult is naturally 'ABC'a.

7. Python in integer and floating point

    Python integer arithmetic result is still an integer, floating-point arithmetic result is still float. But the mixed results of integer and floating point arithmetic becomes a floating-point number:

8. Python Boolean type:

    . A in Python, Boolean type can do and, or and not operator with other data types, see the following code:

   a = True
   print a and 'a=T' or 'a=F'

        The results are not Boolean, but the string 'a = T', this is why? Because the Python 0, 空字符串''and Noneas False, values and other non-empty strings as True, so:

   True and 'a = T' calculation result is 'a = T' 
   continues to calculate 'a = T' or 'a = F' calculation result is 'a = T' 
to explain the above results, it also involves the and and the or operator of a important rule: short circuit calculations.

   B. short circuit calculations.

       . b-1 in the calculation  a and b , if a is False, then in accordance with the algorithm, the entire result is necessarily False, so a return; if a is True, the entire calculation must depends b, so the return b.

       b-2 in the calculation  a or b , if a is True, the algorithm or in accordance with, the entire calculation must result is True, and therefore a return; if a is False, the result will depend on the overall calculation b, so the return to B.

 9. List Usage

     L = [ 'Joye', 20, true] #List ordered list as a

     . Ps Since Python is a dynamic language, so the elements of the list are not necessarily the same data type;

     a. positive sequence access element

     print L[2]

     b. reverse access elements

     print L [-1] # -1 represents the last element

     c. Add a new element

         the append (): adds a new element to the end of the List

         Extend ([List]): Add a list to the end of the list

         insert before adding an element to the list of specified location: (pos, ele)

     d. Removing elements

         L. POP (2) delete the second element #

      e. Alternatively elements

          The [-1] = 'Paul'

10. tuple (component) Usage:

      another tuple is an ordered list List other outside;

      List with a different point:. tuple with () instead of the [], once the value of the tuple is determined will not change, because it can not add / delete / update elements, but for getting value are the same, such as L [0], L [-1] and the like;

      . b. Single element in the form of the tuple (0), and () and can be identified when the operation stage is optimized, so that (0) is in the form of ambiguous at this point, so we use a single element for one more comma behind a single element, such as (0,);

      c. tuple of variable

    = T ( 'A', 'B', [ 'A', 'B']) 
L = T [2]
L [0] = 'X-'
L [. 1] = 'the Y'
Ps of. such elements may be refers to a change list element [ 'a', 'B' ] is changed, the so-called tuple element refers to its constant reference does not change at this time t may be modified to t = ( 'a' , 'b', ( 'a ', 'B')) to ensure that does not change the value of t;

11. the rules of the Python code indentation:
. a codes having the same code block is regarded as indented;
. B the Python indented writing: four spaces, you can not use the Tab, Tab not to mix and spaces to prevent syntax errors thus triggering;
c develop code in Python interactive environment, you need to knock a carriage return line when exiting the indentation.

12 after then if statement expression, then: indicates the start block, the else if said elif;

   if Condition A:
       print 'A'
   elif Condition B:
       print 'B'
   elif Condition C:
       print 'C'
   else:
       print 'baby'

13. for statement
    L = ['Adam', 'Lisa', 'Bart']
    for name in L:
        print name

14. while循环
10 = N 
    X = 0 
    the while X <N: 
        Print X 
        X = X +. 1 
BREAK exits the loop continue to skip the subsequent cycle, the next cycle continues

Guess you like

Origin www.cnblogs.com/sccd/p/10099704.html