Python random module from the entry to the autistic

  1. Import: import random

  2. Random Decimal: random.random (): decimal between greater than 0 and less than 1

  3. Decimal between specified number does not include the specified maximum: random.uniform ()

  4. Random integer: random.randint (1,5): greater than or equal to 1 and less than equal to an integer between 5

  5. Designated odd or even, using steps: random.randrange (1,19, step)

  6. And randomly select a return, there will be a repeating element, returns a list form: random.choice ((1,2,3,), k = 3), 3 elements appear

  7. Arbitrarily selected three elements appear, will not be repeated: random.sample ([1 "23" 3432] k = 3)

  8. random.shuffle (): scramble input, in situ (primary memory) disrupted

    练习:随机生成验证码
    import random
    
    def v_code():
    
        code = ''
        for i in range(5):
    
            num=random.randint(0,9)
            alf=chr(random.randint(65,90))
            add=random.choice([num,alf])
            code="".join([code,str(add)])
    
        return code
    
    print(v_code())

Guess you like

Origin www.cnblogs.com/heyulong1214/p/11528133.html