File operations & functions of python basic learning

1. Document processing related

1. Coding problem

①The default encoding in python2 and python3:

py2 uses ASCII by default, py3 uses utf-8 by default

②Why Chinese garbled characters appear, and what are the situations of Chinese garbled characters?

#sys.stdout.encoding, the default is the locale encoding, print will use sys.stdout.encoding to encode() into a byte stream, and display it to the terminal. Therefore, the locale needs to be consistent with the terminal in order to correctly print out Chinese

The following is a direct introduction to the way to correctly print Chinese:

Terminal is utf-8, locale is zh_CN.GBK

Terminal is utf-8, locale is zh_CN.UTF-8

The terminal is GBK, and the locale is zh_CN.GBK

The terminal is GBK, the locale is zh_CN.UTF-8

2. How to perform code conversion

Strings are encoded in unicode in python, so other languages ​​are first converted to unicode encoding, and then encoded to utf-8 encoding.

3. The role of #-*-coding:utf-8-*-

acts as an encoding declaration

4. Explain the difference between py2 bytes vs py3 bytes

python2 handles strings as native bytes type, not unicode (python2 str == bytes)

All strings in python3 are of unicode type (python3 needs to pass unicode)

string -> encode -> bytes

bytes -> decode -> string

5. File processing

①The difference between r and rb: r is the read mode, while rb is the binary read mode, that is, the content read by the data is directly in the binary bytes mode

② Explain the role of the following three parameters in open:

open(f_name,'r',encoding='utf-8'): f_name is the file name, r is the mode, encoding is the encoding method

2. Function basis

1. Write a function to calculate the sum of the incoming parameters. (dynamic parameter transfer)

def func_sum(x,y):

  return x+y             或 lambda x,y:x+y

2. Write a function, the user passes in the modified file name, and the content to be modified, executes the function, and completes the batch modification operation of the entire file

#Modify the string in the list (the first letter is capitalized)

def file_daxie(file):

  a = []

  for i in file:

  b = i.capitalize()

  a.append(b)

print(a)
View Code

3. Write a function to check whether each element of the object (string, list, tuple) passed in by the user has empty content

def file_k(file):
    n = 0
    for i in file:
        if i == ' ':
            n += 1
     print ( ' There are %s spaces ' %n)
View Code

4. Write a function to check the length of each value passed into the dictionary. If it is greater than 2, only the contents of the first two lengths are retained, and the new contents are returned to the caller.

dic = {'k1':'v1v1','k2':[11,22,33,44]}
def func(i):
    for k,v in i.items():
        if len(v) > 2:
            dic[k] = v[ :2]
        else:
                continue
    return i

print (func (dic))
View Code

5. Explain the concept of closures

Closures are an important syntactic construct in functional programming. Functional programming is a programming paradigm (note: procedural programming and object-oriented programming are also programming paradigms)

Closures are a structure for organizing code, which also improves code reusability.

3. Advanced function

1. Write a function that returns a list of playing cards with 52 items, each of which is a tuple

For example: [('Hearts', 2), ('Flowers', 2),...('Spades', 'A')]

def cards():
    num = []
    for i in range(2, 11):
        num.append(i)
    num.extend(['J', 'Q', 'K', 'A'])
    type = [ ' Hearts ' , ' Flowers ' , ' Diamonds ' , ' Spades ' ]
    result = []
    for i in num:
        for j in type:
            result.append((j,i))
    return result
print(cards())
View Code

2. Write a function, pass in n numbers, and return a dictionary {'max': maximum value, 'min': minimum value}

For example: min_max(2,5,7,8,4)

Returns: {'max': 8, 'min': 2}

def max_min(*args):
    the_max = args[0]
    the_min = args[0]
    for i in args:
        if i > the_max:
            the_max = i
        else:
            the_min = i
    return {'max': the_max, 'min': the_min}
res = max_min(2, 4, 6, 48, -16, 486)
print(res)
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324445341&siteId=291194637