PYTHON Learning 0030: built-in method functions --- ---- 2019-6-23

PYTHON Learning 0030: built-in method functions --- ---- 2019-6-23
.
. 1, ABS (): absolute value. Examples:
Print "ABS (-45):", ABS (-45)
output is:
ABS (-45): 45
2, dict (): turn it into a data dictionary.
3, min () takes the minimum value from a list. max () maximum removed from the list.
4, all () is used to determine all the elements can be given iteration parameters in the whether iterable are TRUE, if it is to return True, otherwise False.
In addition to the elements is zero, empty, None, False are considered True.
5, bool (): only a single parameter, for a given parameter to a Boolean type, if the parameter is 0, empty, None, False, returns False. bool is a subclass of int.
6, any (): function for determining a given iteration may be iterable whether all parameter to False, all return False, if there is a True, True is returned.
In addition to the elements is 0, empty, FALSE are considered TRUE.
7, dir (): a function with no arguments, return variable in the current scope, the type and method definitions list; when the parameters, the parameters return properties, methods.
If the parameter method comprising the dir (), which will be called. If the parameter does not contain the dir (), which will maximize the collection of parameter information.
8, hex (): function for converting a decimal integer to hexadecimal 10, as a string.
9, slice (): function to achieve the object slices, mainly used in the slicing operation in the function parameter.
slice syntax:
Slice class (STOP)
class Slice (Start, STOP [, STEP])
Parameters: start - start position; stop - end position; step - spacing
Return Value:
returns a slice object.

Examples
The following example shows how to use the slice of:

myslice = slice (5) # Set taken five elements slices
myslice
Slice (None,. 5, None)
ARR = Range (10)
ARR
[0,. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9 ]
ARR [myslice] # 5 taken elements
[0, 1, 2, 3, 4]

10, divmod () receives two digital, the calculation result divisor and remainder combined, returns a tuple containing the quotient and remainder of (a // b, a% b ).
11, sorted (): function sort operation on all objects iterations.

sort and sorted differences:

The method is applied on the sort of the list, sorted can be sorted objects operate all iterations.

list sort method returns a list of the existing operation, no return value, and built-in functions sorted method returns a new list, rather than operation performed on the basis of the original.
Syntax
sorted syntax:

sorted (iterable, key = None, reverse = False)
Parameters:

iterable - iterables.
key - is mainly used for the comparison element, only one parameter, the specific parameter is a function of iteration may be taken from the object, specify one of the elements in the iteration to be sorted.
reverse - collation, reverse = True descending, reverse = False ascending (default).
Return Value:
Returns the reordered list.

Examples
The following example shows the simplest use of sorted:

the sorted ([. 5, 2,. 3,. 1,. 4])
[. 1, 2,. 3,. 4,. 5] # default ascending
you can also use the list of the list.sort () method. This method modifies the original list (return value None). This method is usually not as good as sorted () convenience - if you do not need the original list, list.sort () method efficiency will be slightly higher.

= A [5,2,3,1,4]
a.sort ()
A
[1,2,3,4,5]
Another difference is that the list.sort () method is defined only for the list. And sorted () function may receive any iterable.

the sorted ({. 1: 'D', 2: 'B',. 3: 'B',. 4: 'E',. 5: 'A'})
[. 1, 2,. 3,. 4,. 5]
using the key for reverse order

example_list = [5, 0, 6, 1, 2, 7, 3, 4]
result_list = sorted(example_list, key=lambda x: x*-1)
print(result_list)
[7, 6, 5, 4, 3, 2, 1, 0]

To reverse the order, also passed through the third parameter reverse = True:

example_list = [5, 0, 6, 1, 2, 7, 3, 4]
sorted(example_list, reverse=True)
[7, 6, 5, 4, 3, 2, 1, 0]

12, eval (): used to perform a string expression, and returns the value of the expression. We can only handle a single line of code.
13, exec (): used to perform a string expression, but expression does not return a value. You can handle multiple lines
such as the following code:
code = '' '
DEF foo ():
Print ( "RUN foo")
return 1234

foo()
'''

exec (code)
RES = exec (code)
Print (RES)
output results:
RUN foo
RUN foo
None
i.e. exec function expression in brackets may be performed, but no return value, or return None.
Another example:
RES = the eval ( "+. 1. 3. 3 +")
Print (RES)

res=exec("1+3+3")
print(res)

The output is:
. 7
None
14, the ord (): It starts with a character (a string of length 1) as a parameter and returns the value corresponding to ASCII or Unicode value, if the given Unicode character beyond your definition Python , a TypeError exception is thrown in.

the ord ( 'A')
97
the ord ( 'B')
98
the ord ( 'C')
99
15, CHR (): returns the ASCII character currently received corresponding to an integer.
CHR (97)
'A'

16, sum (): sum function.
17, bytearray (): Generally, elements in the string can not be modified, but this function can be converted by the first bar string utf-8 character set is then modified to achieve the purpose of modifying the string. This is a modification of the memory address level, which is the memory address has not changed, not the re-assignment.
17, map (): do mapping function according to a designated sequence provided.
The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list of function return values.
Example:
The following example shows a map () method is used:

def square (x): # calculate the number of squares
... return X 2
...
Map (Square, [1,2,3,4,5]) # calculates the square of each element of the list
[1, 4, 9, 16 25]
Map (lambda X: X
2, [1, 2,. 3, 4,. 5]) using lambda # anonymous function
[1, 4, 9, 16, 25]

Two lists, a list of data of the same position are summed

Map (the lambda X, Y: X + Y, [. 1,. 3,. 5,. 7,. 9], [2,. 4,. 6,. 8, 10])
[. 3,. 7,. 11, 15,. 19]
18 is, filter ( ): function for filtering sequences, filtered ineligible element returns an iterator object, if you want to convert to the list, you can use list () to convert.

The receiving two parameters, as a function of a first, a second sequence, each element of the sequence as an argument to a function arbitrates, then return True or False, and finally returns True new elements into the list.
The following example demonstrates the use of filter functions:

Filter out all the odd list:

def is_odd(n):
return n % 2 == 1

filter = tmplist (is_odd, [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10])
newlist = List (tmplist)
Print (newlist)
output:

[1, 3, 5, 7, 9]

Usage and map () function the same.
. 19, the reduce (): the reduce to a function on a sequence [x1, x2, x3, ... ] , the function must receive two parameters, and the reduce the results continue to the next element in the sequence is calculated as the accumulator, which The effect is:

reduce (f, [x1, x2 , x3, x4]) = f (f (f (x1, x2), x3), x4)
example of a sequence summing reduce implementation can be used:

Import the reduce functools from
DEF the Add (X, Y):
... return X + Y
...
the reduce (the Add, [. 1,. 3,. 5,. 7,. 9])
25
Of course, the summation function may be built directly Python sum (), no need to use reduce.

But if we want the sequence [1, 3, 5, 7, 9], the elements are multiplied, the reduce can come in handy:
from the reduce functools Import
DEF Fn (X, Y):
return X * Y

the reduce = RES (Fn, [. 1,. 3,. 5,. 7,. 9])
Print (RES)
output 945.

20, pow (): the number of digits required power.
pow (8,3) = 512.
21, print ()

Guess you like

Origin blog.51cto.com/13543767/2412475