Python study notes 5--random

1. random module

import random,string

print(random.randint(1,199))#1-199 takes a random integer
print(string.digits) #All numbers 0-9
print(string.ascii_lowercase) #All lowercase letters
print(string.ascii_uppercase) #all uppercase letters
print(string.ascii_letters) #all lowercase letters + all uppercase letters
print(string.punctuation) #all special characters

s = random.choice(['ybq','mpp','zhx','df'])#Randomly take an element
s = random.choice()# randomly pick an element
res = random.sample(string.digits,3) #Randomly take N elements
print(''.join(res))

res = random.uniform(1,9)# Take random decimals? ?
print(res)
print(round(res,2))# Keep several decimal places, if the last decimal place is 0 after rounding, then it will not be displayed
print(random.random()) # Take random decimals between 0-1

s = ['a','b','c','d','e']
random.shuffle(s) #Shuffle, shuffle the order, only pass list
print(s)                

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930242&siteId=291194637