Python common function records

Source: Internet
Author: User

Python common function/Method records

One, Python's random module:

Import module:

Import Random

1. Random () Method:

As shown above, this function returns a random floating-point number of "0,1"(left-closing right-open) .

to control a range of "Min,max", you can return a random floating-point number between 20~30 with min+ (max-min) *random.random () , as shown below:

2. Choice (seq) method:

The method returns a random element from the sequence seq:

3. Randint (A, B) method:

The method randomly randomly generates an integer between A~b, as follows:

4. Shuffle (seq) method:

Shuffle has the meaning of "scrambling", as the name implies, the function of this method is to randomly disrupt the elements of a sequence (seq) as shown in:

5. Sample (Seq,n) method:

The method is returned from the sequence (seq), randomly selecting the number of N, as shown in:

Second, commonly used advanced functions

1. The map function, based on the provided function, maps the specified sequence, returning the mapped result:

Grammar:

Map (function,iterable,...)

The examples are as follows:

2. The filter function filters out elements that do not meet the criteria and returns a new list of eligible elements:

Grammar:

Filter (function,iterable,...)

The examples are as follows:

3. A zip function that accepts an iterative object and converts it into a tuple (packaged).

Grammar:

Zip (iterable,iterable,....)

The examples are as follows:

----------------------------------------Pause, Subsequent update-----------------------------------------------------------

Python common function records

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.