Python second question Learn how to use random

Source: Internet
Author: User

As an independent developer of the Apple Store App, you need to engage in limited-time promotions, generate an activation code (or coupon) for your app, and use Python to generate 200 activation codes (or coupons)


Import String,random
Poolofchars = string.ascii_letters + string.digits
#ascii_letters是生成所有字母, from A-Z and a-z,digits are generated for all numbers 0-9.
Random_codes = lambda x, y: '. Join ([Random.choice (x) for I in range (y)])
For I in range (200):
Print (Random_codes (Poolofchars, 15))

randomA function in the library randint(inf, sup) can produce a random integer within the specified range;

choice(sequence)You can list tuple string select an element randomly in an ordered type (such as, or).

Ascii_letters is to generate all the letters, from A-Z and a-z,digits is to generate all the numbers 0-9.
The activation code is a random combination of numbers and letters, each time from String.ascii_letters + string.digits is the total number and letter combination, that randomly selected one, in the string join link, link to specify the number of digits on the line
Lambda is a knowledge point and also has learning on Liaoche's website. Lambda is an anonymous function  that can make your code more concise.
Refer to: http://blog.csdn.net/yezonggang/article/details/50978114

Learn about string reference https://www.cnblogs.com/rollenholt/archive/2011/11/25/2263722.html

Python second question Learn how to use random

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.