Python built-in functions (iii)

Source: Internet
Author: User
Tags iterable

Python provides a number of built-in functions internally. Let's start with A-Z to learn about Python's built-in functions

The elements of the 1.1filter (function,iterable) parameter iterable are constructed as an iterator with all elements returned by function functions as true, and the parameter iterable can be any object that can be iterated, If the parameter function is none, then delete all elements that are judged to be false, see the following instance # # # # # # # # # # # # # # # # # # # # # # # First constructs a generator of odd sequence #python starting at 3 generator,def _odd_ ITER (): N=1while true:n=n+2yield ndef _not_divisible (n):   return lambda x:x% N>0#lambda an anonymous function that corresponds to a normal function of a function body as a single return statement #lambda does not use the return keyword, and developers can use a lambda expression where the function is applied # When a developer wants to use a simple function as a parameter or return value , using lambda expressions is very handy #lambda with very few, most use Defdef primes ():yield 2  #把第一个2加进生成器it  = _odd_iter ()     #初始化序列while  true:n=next (IT)     #返回序列的第一个数yield  n# because primes is an infinite sequence, So call when you need to set an exit loop condition # Print a prime number within 100 for n in primes (): If n<100:print (n) else:break an instance: l=[ 2,11,5,3,4,7,56,21,32,29,31,0];d ef func (x): if (x > 10):return trueelse:return  The result of the Falseprint (List filter (func,l)) output is: [11, 56, 21, 32, 29, 31]l=[ 2,11,5,3,4,7,56,21,32,29,31,0];d ef func (x): if (x&nBSP;&GT;&NBSP;10): The result of the Return trueelse:return falseprint (filter (none,l)) output is: [2, 11, 5, &NBSP;3,&NBSP;4,&NBSP;7,&NBSP;56,&NBSP;21,&NBSP;32,&NBSP;29,&NBSP;31] In addition, I need to mention one is I see on the other people's website an example, A very complicated way to do this is to read from left to right and read from the back to the same number, for example, 12321,909. Please filter out non-return numbers using filter (): Def is_palindrome (N): if (str (n) [:: -1] == str (N)): Return trueelse:return The result of the  falseoutput = filter (Is_palindrome, range (1, 100)) print output is: [1,  2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, &NBSP;66,&NBSP;77,&NBSP;88,&NBSP;99] Didn't think of this method, I used a very complicated way. Python is really simple. 1.2float ([x]) cast, cast an integer or a string into a floating-point number 1.3format (Value[,format_spec]) This function formats the value values as Format_spec , however, the function interpretation Format_spec is determined by the type of value, and different types have different formatting interpretations. When the parameter format_spec is empty, this function is equivalent to the way the function str (value) is. In fact, when this function is called, the format (VALUE,&NBSP;FORMAT_SPEC) is converted to type (value). __format__ (Format_spec) is called, so the method is found in the value type __ Format__ (), if this method is not found, an exception typeerror is returned. Where Format_spec is written in the following form: Format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]fill ::=align ::= <  | > | = | ^sign ::= + | - |width ::=  integerprecision ::= integertype ::= b | c | d | e |  e | f | f | g | g | n | o | s  | x | x | %fill is an indication that any character can be filled in. Align is the alignment,< is left-aligned, > is right-aligned, ^ is center-aligned. Sign is a symbol,  + represents a plus, and  -represents a minus sign. Width is the number of digits, indicating how many digits are output in total. Precision is the number of decimal reserved digits. Type is the representation of the output numeric value, for example, B is a binary representation, for example, E is an exponential representation, for example, X is a hexadecimal representation. Example: Print (Format (3.14, "0=10")) #保留十位, the remainder is populated with 0, and the print (Format (3.1515926, ' E ')) print (Format (3.1415926, ' 05.3 ')) is filled in front of the # Leave five bits, including the decimal point, two digits after the decimal point print (Format (' Hello ', ' <20 ')) #左对齐print (format (' Hello ', ' ^20 ')) #居中对齐print (format (' Hello '), ' >20 ') #右对齐print (format (3.1515926, ' + ')) #添加符号输出的结果是:0000003.143.151593e+0003.14hello                       hello                         Hello+3.15159261.4getattr (Object,name[,default]) returns the name of the Name property of object If no instance of this property returns false: Class pep:passp  = pep () print (GetAttr (p, ' OK ')) SetAttr (p, ' OK ', ' hello ') print (GetAttr (p, ' OK ')) The result of the output is: Attributeerror:   ' pep '  object has no attribute  ' OK ' hello1.5globals () returns a dictionary representing all the current symbol tables. This is the current module (a dictionary in a function or method), which is the module it is defined in, not the module it is called. 1.6hasattr (Object, name) determines whether the name is an object property, or False if it returns true. Class pep:passp = pep () print (Hasattr (p, ' OK ')) SetAttr (p, ' OK ', ' hello ') print (Hasattr (p, ' OK ')) The result returned is: Falsetrue1.7hash (object) returns the hash value of the object parameter, the hash value is an integer, they can be used to quickly dictionary query instance: print (hash (' str ')) Print (hash (1995)) The result of the print (hash (0.65)) output is: 926707834525168157199514987979559889011201.8hex (x) converts integer integers to 16 binary numbers, not float, Otherwise it will error print (Hex (2)) print (hex) 0x20x42

Zhongzhiyuan Nanjing 904727147, Jiangsu


Python built-in functions (iii)

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.