Python 3 Study Notes (4) ---- character encoding, functions and parameters, python Study Notes

Source: Internet
Author: User

Python 3 Study Notes (4) ---- character encoding, functions and parameters, python Study Notes

I. character encoding and Transcoding

1. In python2, the default encoding is ASCII, and in python3, the default encoding is unicode.
2. unicode is divided into utf-32 (4 bytes), UTF-16 (2 bytes), UTF-8 (1-4 bytes), so UTF-16 is now the most commonly used unicode version, however, UTF-8 is stored in the file because utf8 saves space.
3. encode in py3 converts the string type to the bytes type during transcoding, And the decode converts the bytes type back to the string type during decoding.

In python2.x:

1-*-coding: UTF-8-*-2 import sys 3 print (sys. getdefaultencoding () # view the system's default code 4 5 msg = "learning makes progress" 6 msg_gbk = msg. decode ("UTF-8 "). encode ("gbk") # By "UTF-8" format to "gbk", first to the UTF-8 to unicode, and then encoded by unicode into gbk. During decoding, declare the current format of 7 msg_gb2312 = msg_gbk.decode ("gbk "). encode ("gb2312") 8 9 print (msg) 10 print (msg_gbk) 11 print (msg_gb2312)

In python3.x:

1 #-*-coding: gbk-*-2 3 import sys 4 print (sys. getdefaultencoding () 5 # If the file encoding is changed to gbk, the header must be declared as gbk (that is, in the lower right corner of pycharm) 6 msg = "" # The "" format is Unicode, because only file encoding is changed, python encoding is always Unicode 7 print (msg) 8 9 10 ''' 11 # converted from UTF-8 format to gbk format 12 13 s = "sci fi Fiction" 14 print (s. encode () # print UTF-8 encoding 15 s_gbk = s. encode ("gbk") 16 print (s_gbk) # unicode by default, no longer decode (UTF-8 is a Unicode Extension Set) 17 18 # By gbk format and then converted back to UTF-8 format 19 20 s_utf8 = s_gbk.decode ("gbk "). encode ("UTF-8") 21 print (s_utf8) 22 '''

Ii. Function and functional programming

Programming Method:

1. Object-oriented: Features --> class
2. process-oriented: Features --> process --> def
3. Functional Programming: --> function --> def

Function Definition:

In mathematics: Generally, In a change process, assume there are two variables x and y. If there is a unique and definite y corresponding to any x, then x is the independent variable, and y is the function of x. The value range of x is the definition field of the function, and the value range of y is the value field of the function.

Programming: functions are a programming method of logical structuring and procedural.

Function Definition in Python:

 

1 def test (x): 2 ''' The function definitions ''' 3 x + = 1 4 return x 5 6 7 def: defines The function's keyword 8 test: function Name 9 (): You can define the 10 ''' parameter in it: Description of the document (optional, but it is strongly recommended that you add description information for your function) 11 x + = 1: block of code or program processing logic 12 return: defines the return value

 

Function Type:

1 def func1():2     '''test1'''3     print("this is func1")4     return 05 

Procedural (define every function in the program as a def inclusion process, and use the function name + () directly when using it)

1 def func2 (): 2 ''' test2' 3 print ("this is func2") 4 # The process is a function 5 6 y = func2 () with no return value () # Call a function

Use a function to replace repeated code:

1 import time 2 def logger (): 3 time_format = "% Y-% m-% d % X" # X indicates time, minute, and second 4 time_now = time. strftime (time_format) 5 with open ("log.txt", "a +") as f: 6 f. write ("% s The end \ n" % time_now) 7''' 8 def test1 (): 9 '''testing''' 10 print ("start append log... ") 11 with open (" log.txt "," a + ") as f: 12 f. write ("The end") 13 14 def test2 (): 15 '''testing''' 16 print ("start append log... ") 17 with open (" log.txt "," a + ") as f: 18 f. write ("The end") 19 20 def test3 (): 21 '''testing''' 22 print ("start append log... ") 23 with open (" log.txt "," a + ") as f: 24 f. write ("The end") 25 ''' 26 27 def test1 (): 28''' testing ''' 29 print ("start append log... ") 30 logger () 31 32 def test2 (): 33 '''testing''' 34 print (" start append log... ") 35 logger () 36 37 def test3 (): 38 '''testing''' 39 print (" start append log... ") 40 logger () 41 42 x = test1 () 43 y = test2 () 44 z = test3 ()

Three major advantages of functions:

1. code reuse
2. Maintain consistency
3. scalability

Function return value:

1 def test1 (): 2 '''return test''' 3 msg = "Liu cixin Earth events" 4 print (msg) 5 6 def test2 (): 7 '''return test''' 8 msg = "Liu cixin Earth's past events" 9 print (msg) 10 return msg11 12 a = test1 () 13 B = test2 () 14 15 print ("Return Value: [% s]" % a) 16 print ("Return Value: [% s]" % B) 17 18 # When a function (procedural) when the return value is not defined by return, the python interpreter implicitly returns None, so in python, even the process can be considered as function 19 20, the number of returned values = 0; return None21 return value = 1 (for example, "0"); Return object22 return value> 1 (for example, "0", "[2.3,]" more than 1 ); returns tupl 23 24 # Is there a return value? Because you want the execution result of a function

Iii. Parameters

Parameters and parameters:

Form parameter: A form parameter. It does not actually exist. It is a virtual variable. Use the form parameter when defining the function and function body to receive the real parameter during function call.
Real parameters: the actual parameters passed to the function when the function is called. They can be constants, variables, expressions, and functions.

Difference: the parameter is virtual and does not occupy memory space. The parameter variable allocates memory units only when called. The parameter is a variable in real time, which occupies memory space. The parameter is passed to the parameter, the parameter cannot be passed to the real parameter.

 

Location parameters and keywords: (standard call: the location of the Real-participation parameter corresponds to one by one; keyword call: the location does not need to be fixed)

1 def test (x, y, z): # Here xyz is called the form parameter 2 print (x) 3 print (y) 4 print (z) 5 6 test (1, 3, 5) # Here, 135 is called a real parameter. This method is called the location parameter 7 test (y = 1, x = 4, z = 5) # The keyword parameter 8 test (1, 3, z = 6) # This method can also be used, however, the keyword must be after the location parameter and cannot be repeated.

Default parameters:

1 def test1 (x, z, y = 2): 2 print (x) 3 print (y) 4 print (z) 5 6 test1 (1, 5) 7 test1 (1, 3) 8 9 # default parameter features: when calling a function, the default parameter is not required to pass 10 # default parameter purpose: the default option during software installation, the default port used to connect to the database

Parameter group:

1 def test2 (* args): # * args can only receive location parameters, but cannot receive keyword parameters 2 print (args) 3 4 test2) 5 test2 (* [2, 4, 6, 2]) # args = tuple [2, 4, 6] 6 7 def test3 (x, * args): 8 print (x) 9 print (args) 10 11 test3 (, 6, 9,) # here 1 is passed to x, and the rest is converted into tuples and passed to args12 13 def test4 (** kwargs ): # ** kwargs: converts N keyword parameters into a dictionary. 14 print (kwargs) 15 print (kwargs ["name"]) 16 print (kwargs ["age"]) 17 print (kwargs ["IQ"]) 18 19 test4 (name = "Irlo", age = 26, sex = "F", IQ = 220) 20 test4 (** {"name": 'irlo', "age": '26', "IQ": '000000'}) 21 22 23 def test5 (name, ** kwargs): 24 print (name) 25 print (kwargs) 26 27 test5 ("Irlo ") # If kwargs is not passed, an empty dictionary 28 test5 ("hehe", age = 26, IQ = 220) is returned) # ** kwargs cannot receive the location parameter 29 30 31 def test6 (name, age = 26, ** kwargs): # The parameter group must be placed in the last 32 print (name) 33 print (age) 34 print (kwargs) 35 36 test6 ("Irlo", 35, IQ = 220, sex = "m ") # default parameters can be assigned by location parameters or keywords. If no value is assigned, the default parameter 37 38 39 def test7 (name, age = 26, * args, ** kwargs): 40 print (name) 41 print (age) 42 print (args) 43 print (kwargs) 44 logger ("Force") 45 46 def logger (source ): # A logger is nested in function test7. The declaration of this logger must be earlier than the call of function test7; otherwise, the error 47 print ("from % s" % source) is reported) 48 49 test7 ("Irlo", 54,5, IQ = 220, sex = "M ")

 

Related Article

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.