Python random number detailed use, push to and string, two-color ball applet, python two-color ball
# Use of random numbers
Import random # import random
Random. randint () # specify a random number ranging from 0 to 9.
I = random. sample (range (), 6) # output 6 random numbers, ranging from 1 to 34
I. sort () # sorting method. The original array is changed during sorting and no return value is returned.
Sorted (I) # sorting function. Sorting does not affect the original array and new sorted data is generated.
Print ('---------------- make a two-color ball with the above random number ---------------------')
Sj = random. sample (range (1, 34), 6)
Print (sj, random. randint (1, 17 ))
# If multi-value judgment
N = 4
If n in (4,5, 6,8 ):
Print ('values in the tuples include ', n ')
Else:
Print ('this value does not exist in the tuples ', n ')
# Use the while loop to output the sum of 1dao100
I = 0
He = 0
While I <100:
He = he + I
I + = 1
Print (he)
# List sets are sorted by bubble sort from Avenue small
Li = [,]
For I in range (0, len (li )):
For j in range (0, len (li)-i-1 ):
If li [j] <li [j + 1]:
Li [j], li [j + 1] = li [j + 1], li [j]
Print (li)
# Use of strings (methods and parameters)
Zf = ('My hometown is in Henan! ')
Print (zf [4]) # output with data subscript, with 4th
Print (zf [4:]) # character after subscript
Print (zf [2: 5]) # characters between two labels
Print (zf [] * 2) # output string x 2 times
Print (zf. index ('south') # output the subscript of the specified string.
Print (zf. replace (',' d', 1) # replace the string with only one
Print (zf. endswith ('south'), 5, 6) # Check whether the last one exists
Print ('home' in zf) # Whether the characters exist
# List
Print ('--------------- list ------------------')
Lb = ['abc', '123']
Print (lb. append ('20140901') # Add a single
Lb + = ['A', 'B'] # add multiple
Lb. remove ('A') # delete
O = lb. pop (0) # specify the subscript output
Lb. insert (2, '20140901') # specify the subscript to modify
Lb. clear () # delete all
Print (lb)
Print ('-------- derivation --------')
Td = ['hi', 'Hello', 'word', 'python', 'height', 'Wall', 'hiht ']
Print ([s for s in td if s. startswith ('H')])