Python built-in functions

Source: Internet
Author: User

Built-in functions

Next, let's take a look at the built-in functions in Python. As of Python version 3.6.2, Python now provides us with 68 built-in functions altogether. They are all the functions that Python gives you to use directly. Some of these functions we have used, some of which we have not, and some are sealed, must wait for us to learn new knowledge to unlock the seal. Well, let's meet today. Python's built-in functions. So many functions, where should we learn from?

built-in Functions
ABS () Dict () Help () Min () SetAttr ()
All () Dir () Hex () Next () Slice ()
Any () Divmod () ID () Object () Sorted ()
ASCII () Enumerate () Input () Oct () Staticmethod ()
Bin () Eval () Int () Open () STR ()
BOOL () EXEC () Isinstance () Ord () SUM ()
ByteArray () Filter () Issubclass () POW () Super ()
Bytes () Float () ITER () Print () Tuple ()
Callable () Format () Len () Property () Type ()
Chr () Frozenset () List () Range () VARs ()
Classmethod () GetAttr () Locals () Repr () Zip ()
Compile () Globals () Map () Reversed () __import__ ()
Complex () Hasattr () Max () Round ()
Delattr () Hash () Memoryview () Set ()

The table above is the built-in function, and 68 functions are here. The Order of the table is in the order of the first letter, and you will find all the clutter stacked together. For example, the Oct and bin and hex are all binary conversions, but they are written in three places ... This is very bad for everyone to summarize and learn. Then I divide these functions into 6 categories. Look at this picture below, what kind of classes do you think we will learn today?

Filter

The filter () function receives a function f and a list, the function of which is to judge each element, return True or False,filter () automatically filters out the non-conforming elements according to the result of the decision, and returns a new list that is composed of qualifying elements.

For example, to delete an even number from a list [1, 4, 6, 7, 9, 12, 17] and keep an odd number, first, write a function that judges the odd number:

def is_odd (x):    return x% 2 = = 1
For I in filter (is_odd, [1, 4, 6, 7, 9, 12, 17]):
Print (i) -----> results: 1,7,9,17


Map

The map function in Python is applied to each iteration of an item and returns a list of results. If other iterative parameters are passed in, the map function iterates through each parameter with the corresponding processing function. The map () function receives two parameters, one is a function, the other is a sequence, and map passes the incoming function to each element of the sequence sequentially, returning the result as a new list.

There is a list, L = [1,2,3,4,5,6,7,8], we want to apply f (x) =x^2 to this list, then we can use the map function to process. def Test (s): return s**2

For I in Map (test,l):
Print (i) ----> input result is 1,4,9,16,25,36,49,64




Sort, sorting the data

L = [1,2,-3,4,-5,6,7,5]
L.sort (Key=abs)
Print (L)---"[1, 2,-3, 4,-5, 5, 6, 7]

Divmod, pick-up and remainder such as Divmod (100,3)---> (33.1) 

Enumerate:

    • For an iterative (iterable)/Ergodic object (such as a list, string), enumerate makes it an index sequence that can be used to obtain both the index and the value
    • Enumerate multiple for counting in a for loop

L = [' A ', ' B ', ' C ']

For I in Enumerate (l):

Print (i)------> (0, ' a ') (1, ' B ') (2, ' C ')

For x, y in enumerate (l):

  Print (i)------>0 a

1 b

2 C

Say that learning built-in functions, rather than tidy up their own knowledge system. In fact, the process of organizing these built-in functions is to organize their own knowledge system.

When we lecture, we will classify: commonly used or not used, mainly or according to the scene.

A good programmer should be able to use this method at the time, the use of every built-in function is just right.

To do this, at least first understand, in order to remember when needed, and then to use it in the place.

But here, I still with a little experience of my own, to some of the usual work in the relatively more commonly used methods recommended, please be sure to focus on:

Others: Input,print,type,hash,open,import,dir

STR type code execution: eval,exec

Numbers: Bool,int,float,abs,divmod,min,max,sum,round,pow

Sequence--Lists and tuples Related: list and tuple

Sequence--string-Related: Str,bytes,repr

Sequence: Reversed,slice

Data collection--dictionaries and collections: Dict,set,frozenset

Data collection: Len,sorted,enumerate,zip,filter,map

Python built-in functions

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.