Built-in functions learned by the python tribe, python tribe

Source: Internet
Author: User
Tags iterable

Built-in functions learned by the python tribe, python tribe

Recently, I joined the python tribe. I felt that it was very interesting. I had a bit of fun. I learned a lot of built-in functions that I didn't know before, and I felt that I was about to remember them. So I started to display them.

1. divmod (a, B): Take the quotient and remainder of a divided by B, and the effect is equivalent to (a/B, a % B );

2. dir (): the parameter is the function name and class name. It tells us what parameters are contained in the corresponding function.

3. enumerate: The serial number is generated when the list is traversed. For example:

1 a = [1, 2, 3]2 for index,item in enumerate(a):3     print index4     print item

4. complex: process the plural values in python, such as complex (2, 3) --> 2 + 3j; complex (2 + 3j) --> 2 + 3j note that the expressions in the brackets cannot contain spaces !!

5. cmp (x, y): The function is used to compare two objects. If x <y returns-1, if x = y, 0 is returned. If x> y, 1 is returned.

6. chr (I): returns the ASCII character corresponding to the integer I. Opposite to ord.

7. isinstance (a, obj): used to determine the type of an object, which indicates whether a is of the obj type. Here, pay attention to the type function. One of the most important differences between them is: type can only be used to directly determine the type, while isinstance is more powerful than type, and can be used to subclass

8. Differences between any and all: any and all () functions. any is arbitrary, and all is all. Any ----> if no iterable element is 0, '', False, all (iterable), True is returned. If iterable is null, False is returned. Functions are equivalent:

def any(iterable):     for element in iterable:        if  element:            return False    return True

 

 

 

However, if all the elements of iterable are not 0, '', False, or iterable is null, all (iterable) returns True; otherwise, False is returned. The function is equivalent:

1 def all(iterable):2     for element in iterable:3         if not element:4             return False5     return True

9. _ doc __: python function Description document string, print (function. _ doc _), which outputs the description enclosed by three quotation marks in the function.

10. eval: Evaluate the string 'str' as a valid expression and return the calculation result. You can also execute string code.

11: exec, execfile,execThe statement is used to execute the Python statement stored in a string or file. The execfile (filename [, globals [, locals]) function can be used to execute a file.

12: format -----> python built-in Formatting Function. usage found on the Internet:

 

1 age = 25 2 name = 'caroline '3 def test: 4 print '{0} is {1} years old. '. format (name, age) # output parameter 5 print '{0} is a girl. '. format (name) 6 print '{0 :. 3} is a decimal. '. format (1/3) # three digits after the decimal point 7 print '{0: _ ^ 11} is a 11 length. '. format (name) # Use _ to fill the blank space 8 # Replace the alias with 9 print '{first} is as {second }. '. format (first = name, second = 'wendy ') 10 print 'My name is 255.0.name+'.format(open('out.txt', 'w') # Call method 11 print 'My name is }. '. format ('fred ') # specify the width

 

 

 

I learned this first, and I have to go to work again ....

----- Python pupils

 

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.