Python built-in function learning summary (3)

Table of contents

1. slice() function

2. isinstance() function

3. int() function

4. open() function

5. str() function

6. bool() function

7. eval() function

8. exec() function

9. ord() function

10. sum() function


1. slice() function

The slice() function implements a slice object, which is mainly used for parameter passing in the slice operation function.

Syntax: slice(start, end, step)

>>>a=[1,2,3,4,5,6,7,8]

>>>print(a[slice(1,8,2)])
[2, 4, 6, 8]

2. isinstance() function

        isinstance() function to determine whether an object is a known type, similar to type(). Returns True if the type of the object is the same as the type of parameter two (classinfo), otherwise returns False.

>>>isinstance(10,int)
        
True
>>>isinstance("xing",str)
        
True

3. int() function

        The int() function is used to convert the specified value into an integer. If an argument that cannot be converted to an integer is read,  a ValueError exception will be thrown.

        Syntax: int(x, base);

        base: Specifies the base of the x parameter (default is 10)

        Return result: If no parameter is input, it will return 0; if a floating point number is input, it will return the value before the decimal point is intercepted.

>>>int()
        
0
>>>int(3.1415926)
        
3
>>>int("100",2)#指定基数为2即,2进制
        
4
>>>int("xingxing")
        
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    int("xingxing")
ValueError: invalid literal for int() with base 10: 'xingxing'

4. open() function

        The open() function is used to open the file. File handle = open('file path', 'mode', encoding method).

        'File path': There is an absolute path: f = open(r'd:\test\01\a.txt', 'r'); relative path. f = open('a.txt', 'r')

        'model':

# r : Open the file as read-only. This is the default mode. The file must exist, otherwise an error will be thrown

#rb: Open a file in binary format for read-only.

#r+ : Open a file for reading and writing. The file pointer will be placed at the beginning of the file. Add after reading.

#w : Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.

#w+: Open a file for reading and writing. Overwrite the file if it already exists. If the file does not exist, create a new file.

#a : Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.

#a+ : Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, create a new file for reading and writing.

Note: There is a method with b in the back, and there is no need to consider the encoding method. Those with a + sign can be read and written, but there are still differences between them.

        Encoding method: win system defaults to gbk encoding

5. str() function

        The str() function returns a string version of the specified object.

>>>str(90)
        
'90'

>>>str([1,2,3,4])
        
'[1, 2, 3, 4]'

6. bool() function

         The bool() function is used to return a Boolean value ( True  or  False )


>>>bool(537)
        
True
>>>bool("xingxing")
        
True
>>>bool(range(1))
        
True
>>>bool(range(0))
        
False

7. eval() function

        The eval() function is used to execute a string expression and return the value of the expression.

        Syntax: eval(expression, globals=None, locals=None, /)

        expression: Specifies a string that will be parsed and executed as a Python expression.

        globals: Global scope, this parameter must be a dictionary type.

        locals: Local scope, this parameter can be any mapping object.

>>>eval("1+2")
        
3
>>>eval("A+B",{'A':10,'B':20})
        
30
>>>eval("A+B",{'A':10,'B':20},{'A':'O','B':'K'})  #全局变量与局部变量同时存在时,后者更高级
        
'OK'

8. exec() function

 exec executes Python statements stored in strings or files. Compared with eval, exec can execute more complex Python code.

>>>x=10
        
>>>exe="""
z=30
sum=x+y+z
print(sum)
"""

>>>def func():
    y = 10
    exec(expr)
    exec(expr, {'x': 100, 'y': -20})
    exec(expr, {'x': 1, 'y': 2}, {'y': 3, 'z': 4})
    
>>>func()
50
110
34

9. ord() function

        ord()  is used to convert the specified single string into the corresponding Unicode encoding. chr()  is the inverse of this function.

>>>ord("i")
105
>>>chr(105)
'i'

10. sum() function

        The sum()  function is used to sum the parameters passed in.

>>>sum([1,2,3,4,6,8])
24
>>>a=[1,2,3]
>>>sum(a,100)
106

references: 

[1] Detailed usage of Python open() function - Yo, what about writing bugs? ? - Blog Park

Guess you like

Origin blog.csdn.net/weixin_51775350/article/details/128153381