Python Common Tips Summary

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.