Use random, range and function len

Use random, range and function len

A, random function

1、random.random()和random.Random():

Import random 
NUM = random.random () # generates random floating point 0-1 .6161288183675745 
num2 = random.Random () # generates a random instance of Random class obtained in the module, so the value returned is the address character string <random.Random AT 0x036CFE38 Object> 
# random.random () equivalent random.Random (). Random () 
Print (NUM)
 Print (num2)

2, random.uniform (x, y) generating a random float within a specified range

Import Random 
NUM = random.uniform (1,100) # floating point numbers within a specified range 79.55634204719212 
Print (NUM)

3, random.randint (x, y) generating a random integer within a specified range

import random
num = random.randint(1,2)#1
print(num)

4, random.randrange ([start], [end], stept) generated within a specified range, a random number is incremented by stept

Import Random 
NUM = random.randrange (10,20,2) # generates 2 has a random number is incremented within 10 ~ 20. Equivalent to 2-increment function 
Print (NUM)

5, random.choice (sequence) selecting a random number from the sequence

Import Random 
NUM = random.choices (( ' haihai ' , ' baibai ' , ' Kun ' )) # selecting a random number from the sequence, the results Kun --- 
Print (NUM)

6, random.shuffle (sequence) for the elements of a sequence upset

Import Random 
List1 = [ " known fate " , " sixties " , " ancient rare ' , ' oldest ' ]
 Print (random.shuffle (List1)) # is upset the sequence, there is no return value ---- result: [ 'known fate', 'sixties', 'oldest', 'ancient rare']

Two, range () function

1, Python comes with range function, without importing. Format: range (start, end, step), the function package is not reported to the front range, such as the range (0,5) refers to a range [0,5)

for I in Range (0,5 ):
     Print (I)
 # ---- operation results 
0
 . 1 
2 
. 3 
. 4

Three, len () function

1, len function of counting the number. The number of elements in the sequence, the length of the total number of keys of the dictionary, the string

= List [ ' Xue ' , ' Shou ' , ' Zhi ' , 1,45] # sequence 
dict = { ' name ' : ' xiaxia ' , ' Age ' : ' 200 is ' } # dictionary 
STR = " abcder567 WE @ " # string 
Print (len (List)) # ---. 5 
Print (len (dict)) # ---- 2 
Print (len (STR)) # ---- 12 is

 

Guess you like

Origin www.cnblogs.com/xswt/p/11458735.html
Recommended