Python development day4 and python development day4

Source: Internet
Author: User

Python development day4 and python development day4

Python development day4

 

I. Review character encoding in Python2

2: how to convert UTF-8 to gbk, utf8 first decode to unicode, and then encode to gbk

Step 1: decode utf8 to unicode first

Name = "China" print ([name. decode ("UTF-8")]) result [U' \ u4e2d \ u56fd ']

Step 2: encode unicode to gbk

Name = "China" print ([name. decode ("UTF-8"). encode ("gbk")]) Result: ['\ xd6 \ xd0 \ xb9 \ xfa']

  

In Python3, Question 1: What encoding is the text in the file?

The magic thing happened, and no error was reported. The encode is gbk, so it can be determined that the variable encoding in Python3 is unicode, and there is no decode method in Python, data can be directly converted to other formats

The Python file format is UTF-8, and after the data is imported into the memory, the data encoding will be converted to unicode, why to convert to unicode because windows default encoding is gbk, the windows codes in other countries are the same in different departments. The default encoding in Linux is UTF-8. Therefore, if Python3 is converted to unicode in the memory, the formats of all platforms can be supported.

In short, in Python3, the data in the file is encoded in UTF-8 format, while in the memory, the data is unicode.

Question 2: When the encode of Python3 changes data to bytes? Question 3: What is the encoding function?

The scenarios with many encoding applications should be crawlers. Many websites on the Internet use complicated encoding formats. Although the overall trend is changed to UTF-8, it is still very complicated, therefore, when crawling a webpage, You need to convert various codes.

Question 4: Why is unicode not used in python2?

The first version of python was invented in 1989. The first public version was released in 1991, while unicode was officially released in 1994. Therefore, Python2 is definitely not catching up. However, there must be a format for data usage. Therefore, we use the C language's bytes

 

Ii. Functions

Last class review:

  • Function Parameters

     

    Global and local variables

    The variables defined in the subroutine are called local variables, and the variables defined at the beginning of the program are called global variables. The global variable scope is the whole program, and the local variable scope is the subroutine that defines the variable. When a global variable has the same name as a local variable, the local variable takes effect in the subroutine that defines the local variable, and the global variable takes effect elsewhere. For other explanations, see the following code:
    1 # From the Perspective of arguments 2 def foo (x, y): 3 print (x, y) 4 5 foo (1, 2) # By position 6 foo (y = 2, x = 1) # Form 7 8 according to the key = value keyword # For the same parameter, we either pass the value 9 foo (1, x = 1, y = 2) 10 11 # The location must be 12 foo (y =) # error 13 14 15 # from the angle of the parameter: location parameter, default parameter, variable length parameter, ** kwargs16 def foo (x, y, z): # location parameter: required value parameter 17 print (x) 18 print (y) 19 print (z) 20 21 foo (y = 2, x = 1, z = 3) 22 23 24 def foo (x, y = 1): # default parameter 25 print (x) 26 print (y) 27 # foo (1, 2) 28 foo (1) 29 30 def SQL (cmd, type = 'select'): 31 if type = 'select ': 32 print ('select function') 33 elif type = 'insert': 34 pass35 36 SQL ('select * from mysql. user') 37 38 def auth (user, passwd, group = 'default', project = 'default'): 39 pass40 auth ('Tom ', 123) 41 42 # ------------------- * args43 def foo (x, y, * args): 44 print (x) 45 print (y) 46 print (args) 47 # foo (1, 2, 3, 3, ,) 48 49 l = ['A', 'B'] 50 foo (, * l) # * The args format is equal, 551 foo (552, 'A', 'B') # * The args format is equal to, 53 54 def foo (x, y, z ): 55 print (x) 56 print (y) 57 print (z) 58 59 # foo (, 3) 60 l = [,] 61 foo (* l) # foo (1, 2, 3, 4) 62 63 def foo (x, ** kwargs): 64 print (x) 65 print (kwargs) 66 67 foo (1, y = 3, z = 1) 68 dic = {'A': 1, 'B': 2} 69 foo (1, ** dic) # foo (1, a = 1, B = 2) 70 71 def foo (x, y, z): 72 print (x, y, z) 73 74 foo (** {'Z': 1, 'y': 2, 'x': 3}) # foo (z = 1, B = 2, x = 3) 75 76 77 78 79 # default parameters in location parameters-* args, ** kwargs80 def func (x, * args, y = 3): 81 print (x, args, y) 82 83 func (100,200) 84 85 86 def func (x, * args, ** kwargs): 87 print (x) 88 print (args) 89 print (kwargs) 90 91 # note 92 ''' 93 * sym is equivalent to expanding by position to write 94 95 ** sym is equivalent to 'sym expanding by keyword to write 96 97 '''
    View Code
  • Iii. function-based first-class Object 4: function nesting

    Function Nesting is divided into two types:

    V. Closure Functions

    A closure function refers to the use of nested definitions: the second layer in the nested definition and the above internal functions include references to external function names.

    VI:

     

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.