Python built-in functions (4)

Source: Internet
Author: User
Tags iterable variable scope

python built-in functions (4)
1.copyright

Interactive Prompt object Print license text, a list
Contributors and copyright notices

2.credits
Interactive Prompt object Print license text, a list of contributors and copyright notices

3.delattr(object, name ) Object--objects. Name--Must be a property of the object.

classcoordinate:x= 10y=-5Z=0 Point1=coordinate ()Print('x =', point1.x)Print('y =', POINT1.Y)Print('z =', point1.z) delattr (coordinate,'Z') Print('-- After deleting the z attribute--')Print('x =', point1.x)Print('y =', POINT1.Y)#Trigger ErrorPrint('z =', point1.z)
View Code

Operation Result:

('x =', 10)('y =',-5)('z =', 0)--After deleting the Z attribute--('x =', 10)('y =',-5) Traceback (most recent): File"test.py", line 22,inch<module>Print('z =', Point1.z) Attributeerror:coordinate instance has no attribute'Z'

4.dict ()

Used to create a dictionary

>>>dict ()#Create an empty dictionary{}>>> Dict (a='a', b='b', t='T')#Incoming keyword{'a':'a','b':'b','T':'T'}>>> dict (Zip ([' One',' Both','three'], [1, 2, 3])#map function mode to construct a dictionary{'three': 3,' Both': 2,' One': 1} >>> Dict ([(' One', 1), (' Both', 2), ('three', 3)])#can iterate over objects to construct dictionaries{'three': 3,' Both': 2,' One': 1}>>>

5.divmod (A, B) A and B are numbers

The Python Divmod () function combines the divisor and remainder operations to return a tuple containing quotient and remainder (A//b, a% b)

>>>divmod (7, 2) (3, 1)>>> divmod (8, 2) (4, 0)>>> divmod (1+2j,1+0.5j) ((1+0j), 1.5j)

6.enumerate (sequence, [start=0]) sequence--a sequence, iterator, or other supporting iteration object, start-subscript start position

Used to combine a traversed data object (such as a list, tuple, or string) into an index sequence, while listing data and data subscripts, typically used in a for loop

>>>seasons = ['Spring','Summer','Fall','Winter']>>>List (Enumerate (seasons)) [(0,'Spring'), (1,'Summer'), (2,'Fall'), (3,'Winter')]>>>list (Enumerate (seasons, start=1))#Small label starting from 1[(1,'Spring'), (2,'Summer'), (3,'Fall'), (4,'Winter')]

For the For Loop

>>>seq = ['one', 'three ' ' ]>>> for in enumerate (    seq): ... Print (i, seq[i]) ... 0One 1 2 three>>>

7.eval (expression[, globals[, locals]])

function is used to execute a string expression and return the value of the expression

    • Expression--expressions.
    • Globals--variable scope, global namespace, if supplied, must be a Dictionary object.
    • Locals--variable scope, local namespace, if provided, can be any mapping object.
 >>>x = 7>>> eval ( " 3 * x   " )  21>>> eval ( " pow (2,2)  "  Span style= "COLOR: #000000" >)  4>>> eval ( " 2 + 2   " )  4>>> n=81>>> eval ( " Span style= "COLOR: #800000" >n + 4   " )  $ 

8.exec (object[, globals[, locals]])

    • Object: A required parameter that represents the python code that needs to be specified. It must be a string or a code object. If object is a string, the string is first parsed into a set of Python statements and then executed (unless a syntax error occurs). If object is a code object, it is simply executed.
    • Globals: An optional parameter that represents the global namespace (which holds the global variable) and, if provided, a Dictionary object.
    • Locals: An optional parameter that represents the current local namespace (which holds local variables) and, if provided, can be any mapping object. If the parameter is ignored, then it will take the same value as Globals
    • exec return value is always None
>>>exec('print ("Hello Keys")') Hello Keys#single-line statement string#multi-line statement string>>>exec("""For I in range (5): ... print ("Iter time:%d"% i) ..."""iter time:0iter Time:1iter time:2iter time:3iter time:4x= 10Expr="""z = 30sum = x + y + zprint (sum)"""deffunc (): Y= 20exec(expr)exec(Expr, {'x': 1,'y': 2})    exec(Expr, {'x': 1,'y': 2}, {'y': 3,'Z': 4}) func ()

Output Result:

603334

9.filter (unction, iterable) function--judgment Iterable--Can iterate objects

The filter () function filters the sequence, filters out elements that are not eligible, returns an Iterator object, and, if you want to convert to a list, you can use list () to convert.

The two parameters are received, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function, and then returns True or False, and finally the element that returns true is placed in the new list

# !/usr/bin/python3 Import Math def IS_SQR (x):     return math.sqrt (x)% 1 == = Filter (IS_SQR, Range (1,101 = list (tmplist)Print (NewList)

Operation Result:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

10.exit (N)

Exit the program, return by default 0 for the program to run successfully, not 0 to indicate an exception, or you can define the return value when the program exits

Python built-in functions (4)

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.