Python face question

Source: Internet
Author: User

1. How to generate random numbers in Python

1> Generating Random decimals

Random.random () is a random decimal between 0-1, excluding 1

Random.uniform parameters can also be specified in the range, such as 1-2, excluding 2

2> generating random integers

Random.randint ()

Random.randrange ()

3> randomly takes an element in a sequence

Random.choice ()

4> randomly takes several elements in a sequence, which need to be determined by the parameters

Random.sample ()

5>.random.shuffle (a) #把一个序列打乱

2. Reverse string output

1> using slices

2> can also be operated as follows

3> can also be implemented using functions

3, judge whether a string is a palindrome string

A palindrome string is the same from the post-reading and backward-reading: EG:ABCDCBA

4, randomly generate 100 numbers, and then write to the file

Analysis:1> Open a new file, ready to write

2> randomly generates a number (integer or decimal) and writes that number to the file

3> cycle 2nd, until 100 random number generation is completed

5, given dict={' a ': 3, ' B ': 5, ' C ': 2, ' d ': 9, ' E ': 6}, sorted by its key value

Sorting nested lists can be done in this way

6, to a=[1,3,2,2,1,5,5,3] inside the elements go heavy, there are several ways, and write out

Python face question

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.