Python 007- Various functions of python

1.chr(), unichr() and ord()

The chr() function takes an integer in the range (256) (that is, 0 to 255) as a parameter, and returns a corresponding character.

unichr() is the same as it, except that it returns Unicode characters. The parameter range of unichr(), which was added since Python 2.0, depends on how your Python was compiled. If it is Unicode configured as USC2, then its allowable range is range (65536) or 0x0000-0xFFFF; if it is configured as UCS4, then this value should be range (1114112) or 0x000000-0x110000. If the provided parameter is not within the allowed range, a ValueError exception will be reported.

The ord() function is a pairing function of the chr() function (for 8-bit ASCII strings) or the unichr() function (for Unicode objects), which takes a character (a string of length 1) as an argument and returns the corresponding An ASCII value, or a Unicode value, will raise a TypeError if the given Unicode character is outside of your Python-defined range.

1 >>>ord('a')
2 97
3 >>> ord('b')
4 98
5 >>> ord('c')
6 99

2. getch() reads keyboard input characters

1 import  msvcrt
2 ch = msvcrt.getch()

3. randrange() returns a random number in the specified increasing base set

1  import random
 2  
3  #output even numbers between 100 <= number < 1000 
4  print  " randrange(100, 1000, 2) : " , random.randrange(100, 1000, 2 )
 5  
6  #output 100 <= number < 1000 
7  print  " randrange (100, 1000, 3) : " , random.randrange(100, 1000, 3)

4.assert

Python assert assertion is a statement that its boolean value must be true, and if an exception occurs, the expression is false. It can be understood that the assert assertion statement is raise-if-not, which is used to test the expression, and its return value is false, which will trigger an exception.

1 assert 1==1
2 assert 2+2==2*2
3 assert len(['my boy',12])<10
4 assert range(4)==[0,1,2,3]

5.transpose matrix transpose

 1 array([[[ 0,  1,  2],
 2         [ 3,  4,  5]],
 3 
 4        [[ 6,  7,  8],
 5         [ 9, 10, 11]]])
 6 
 7 arr1.transpose((1,0,2))
 8 array([[[ 0,  1,  2],
 9         [ 6,  7,  8]],
10 
11        [[ 3,  4,  5],
12         [ 9, 10, 11]]])

What is the process like?

arr1.shape should be (2, 2, 4) meaning 2-dimensional, 2*4 matrix

The parameters in arr1.transpose(*args) can be understood in this way, he is changing the order of arr1.shape, let’s mark the angle of arr1.shape, (2[0], 2[1], 4[2 ]) [ ] is the index of the shape, right, 

transpose((1, 0, 2)) means to reset the shape in this order that is (2[1], 2[0], 4[2])

Although it seems that the shapes before and after the transformation are 2, 2, 4, but the problem is, transpose is transpose

The shape is reset in the order of (1, 0, 2), and all elements in the array are also reconstituted into a new matrix according to this rule

For example, the index of 8 in arr1 is (1, 0, 0), then according to the transformation rules just now, it is (0, 1, 0) to see if the position of your result arr2 is the same

6.any() and all()

#any(x) Determine whether the x object is an empty object, if all are empty, 0, false, return false, if not all empty, 0, false, return true

#all(x) If all elements of the all(x) parameter x object are not 0, '', False or x is an empty object, return True, otherwise return False

1 >>> all([ ' a ' , ' b ' , ' c ' , ' d ' ])   #list list, none of the elements are empty or 0 
2  True
 3   >>> all([ ' a ' , ' b ' , '' , ' d ' ])   #List , there is an empty element 
4  False
 5   >>> any([ ' a ' , ' b ' ,'c', ' d ' ])   #List list, none of the elements are empty or 0 
6  True  
 7   >>> any([ ' a ' , ' b ' , '' , ' d ' ])   #List list, there is one that is empty element 
8   True

 

 

Guess you like

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