程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

6、 Python learning notes module random module

編輯:Python
"""
1、 A module for generating random numbers
"""

# Introduce modules
import random
# Print random numbers by default , Range 0-1
print(random.random())
# Print integer random numbers , Specified scope ( Including the left and right numbers , and range Different )
print(random.randint(1, 8))
# Select one randomly from the specified content , Can be a string , List etc.
print(random.choice('abcdef'))
# Select randomly from the specified content n individual ( You can specify ), Can be a string , List etc. , The return value is a list
print(random.sample('abcdef', 3))
# Print integer random numbers , Specified scope ( Including the left , Excluding the right , and range Same as )
print(random.randrange(1, 8))

Generate verification code

import random
def y_code(): # Defined function
code = '' # The initial value of the verification code is an empty string
for i in range(4): # To generate 4 Bit verification code , So the cycle 4 Time
j = random.choice([1, 2]) # Take random number , take 1 Then increase the number , take 2 Then add letters
if j == 1:
addnum = random.randrange(10) # Take random number
code += str(addnum) # Add to verification code
else:
addal = chr(random.randrange(65, 91)) # Take random number , Convert to letters
code += addal # Add to verification code
print(code) # Print verification code
y_code()
# Supplementary information
# chr Numbers can be mapped to ASCII Code table character printing , stay ASCII In the code table 65-90 It's the letters
print(chr(65))
print(chr(90))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved