python small note -1

In Python, data types can be processed directly are the following:

1. integer 

    Multi-use hexadecimal by the prefix 0x, 0-9, af composition. Example: 0xff00

2. Float (decimal)

    Scientific notation written: 1.23x10 ^ 9 -> 1.23e + 9

3. String

    With '/' "any text enclosed

  (1) If the string contains both internal 'and contains ",can escape character \is identified, for example: 'I\'m \"OK\"!'

           To simplify, Python also allows r''represented by ''the string does not escape the inside of the default

     If the internal string has a lot of line breaks, with \nwrite one line is not good reading, in order to simplify, Python allows '''...'''format represents a number of lines

4. Boolean value

    A Boolean value only True, Falsetwo kinds of value

  • andOperation with operation, only if all are True, andthe operation result isTrue ——短路逻辑
  • orA calculation or operation, as long as one is True, orthe operation result isTrue
  • notCalculating a non-operation, which is a unary operator, put Trueinto False, FalseintoTrue

5. null

    Null is a special value in Python, with Nonerepresentation. NoneCan not understand it is 0, because 0it makes sense, but Noneis a special null value.

Python, there are two division a division is/:

     /Floating-point division calculation result, even if the two exactly divisible integers, floating point result is

Another division is//:

     Called floor addition, two integers is an integer division still

Character Encoding

character ASCII Unicode UTF-8
A 01000001 00000000 01000001 01000001
in x 01001110 00101101 11100100 10111000 10101101

Unified computer memory using Unicode encoding

To be saved to the hard disk or transmitted when required, it is converted to UTF-8 encoding

String

ord ( 'A') = 65 - Get character integer

chr (66) = 'B' - transcoding the corresponding character

  • Python is a string type str, a Unicode representation in memory, to be transmitted over the network, saved to disk, str -> (in bytes) bytes

           Python of bytesdata types tape brepresented prefix single or double quotation marks:

       x = b'ABC'

           To distinguish between 'ABC'and b'ABC', the former str, although the latter have to display the contents of the former and the same, but byteseach character only one byte.

  • In Unicode strby the encode()method may be coded as specified bytes, for example:

    >>> 'ABC'.encode('ascii')
         b'ABC'
    >>> '中文'.encode('utf-8')
         b'\xe4\xb8\xad\xe6\x96\x87'
  • Plain English strcan be ASCIIencoded as bytesthe content is the same, containing the Chinese strcan be UTF-8encoded asbytes
  • If we read from the disk or network byte stream, then the data is read bytes. Should bytesbecome str, we need to use decode()methods:

    >>> b'ABC'.decode('ascii')
        'ABC'
    >>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')  '中文'
  • Since Python source code is a text file, so when your source code contains Chinese, and when you save the source code, you need to be sure to specify saved as UTF-8 encoding. When the Python interpreter to read the source code to make it according to UTF-8 code reading, we usually write two lines in the beginning of the file:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
  • The first line is a comment to tell Linux / OS X system, which is a Python executable, Windows will ignore the comment;

    The second line comments is to tell the Python interpreter, according to the UTF-8 encoding to read the source code, or else, you write the source code in Chinese output may be garbled.

    Affirming the UTF-8 encoding does not mean your .pyfile is UTF-8 encoded, and must be sure that the text editor is using UTF-8 without BOM code:

                     

format

Common placeholders are:

Placeholder Replace content
%d Integer
%f Float
%s String
%x Hexadecimal integer

 

 

 

 

 

    

 

>>>print('%.2f ' %3.1415926)

>>>3.14

  • If the string in% is an ordinary character, you need to escape
  • >>>%d %% %7
  • >>>7%

format () - a placeholder

  >>> 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125)

  'Hello, 小明, 成绩提升了 17.1%'

Operator precedence:

  1. Exponentiation **
  2. + Sign -
  3. Arithmetic operators * / // + -
  4. Comparison operators <<=>> = ==! =
  5. Logical operators and or not

The conditional expression (ternary operator)

 x, y = 4.5

if x<y:

   small = x

else:

   small = y

————>

small = x if x<y else y

small =(x if x<y and x<z) else (y if y<z else z)

Assert assertion

When the assert condition is false, the program automatically ignored crash --AssertionError-- Conversely - [into] Checkpoint

 

Guess you like

Origin www.cnblogs.com/Aurakkk-8/p/11620300.html