The examples in this paper summarize the techniques commonly used in Python. Share to everyone for your reference. The specific analysis is as follows:
1. Get the local MAC address:
Import Uuidmac = Uuid.uuid1 (). Hex[-12:]print (MAC)
Running Result: e0cb4e077585
2. Use of Del
A = [' B ', ' C ', ' d ']del A[0]print (a) # output [' C ', ' d ']
A = [' B ', ' C ', ' d ']del a[0:2] # Delete starting from 1th element, to 2nd element print (a) # output [' d ']
A = [' B ', ' C ', ' d ']del Aprint (a) # at this time a is undefined
3. Use of Join
A = [' C ', ' d ']a.reverse () a = [' d ', ' C ']b = ', '. Join (a) print (b) # output D,c
4. Random number usage:
Import RANDOMX = Random.randint (1,100) y = Random.choice (' ABCD ') print (x) print (y)
The result of the operation is:
68
B
5. Use of Dict:
A=[1,2,3]b=[' A ', ' B ', ' C ']c=dict (Zip (b)) print (c) # output: {1: ' A ', 2: ' B ', 3: ' C '}
6. Use of Map:
A= ' 1-2-3-4 ' B=map (int,a.split ('-')) print (b) # output: [1,2,3,4]
7. [] Use:
[].remove (value)
[].pop (index) = value
[].count (x) = x number in list
{} use
{}.pop (key) = value
{}.get (key) = value or {}.get (key, 0) Set default value
8. String manipulation
A = Str.decode (' utf-8 ') b = str.encode (' utf-8 ') str.isdigit () # is the value str1 = ' abc%s '%str2
9. String Traversal:
Import stringx= string.ascii_lowercase# Print (x) # output: Abcdefghijklmnopqrstuvwxyzd = Enumerate (x) c = List (d) print (c)
Output:
[(0, ' a '), (1, ' B '), (2, ' C '), (3, ' d '), (4, ' E '), (5, ' F '), (6, ' G '), (7, ' H '), (8, ' I '), (9, ' J '), (Ten, ' K '), (one, ' l '), (A, ' m '), (n '), (+, ' O '), (+, ' P '), (+, ' Q '), (p, ' R '), (+, ' s '), (+, ' t '), (+, ' U '), (+, ' V '), (+, ' W '), (+, ' x '), (+, ' Y '), (+, ' z ')]
For I, J in D:
At this time
i = 0,1,2,....., 25
j = ' A ', ' B ' ..., ' z '
Hopefully this article will help you with Python programming.