Common syntax skills in Python

Source: Internet
Author: User

I recently read from many colleaguesCode, Encountered some Python statements that have never been used before, accumulate it.

1. Obtain the local MAC address:

Import UUID

MAC = UUID. uuid1 (). HEX [-12:]

2 del usage

A = ['B', 'C', 'D']

Del a [0]

Then a = ['C', 'D']

Del a [0: 1]

Then a = ['D']

Del

Then a is undefined.

3 A = ['C', 'd]

A. Reverse ()

A = ['D', 'C']

B = ','. Join ()

B = 'd, C'

4 import random

X = random. randint (1,100)

Y = random. Choice ('abcd ')

5 A = [1, 2, 3]

B = ['A', 'B', 'C']

C = dict (ZIP (a, B ))

Then c = {1: 'A', 2: 'B', 3: 'C '}

6 A = '1-2-4-4'

B = map (INT, A. Split ('-'))

Then B = [1, 2, 4]

7 [] Use

[]. Remove (value)

[]. Pop (INDEX) = Value

[]. Count (x) = x number in the list

{} Use

{}. Pop (key) = Value

{}. Get (key) = value or {}. Get (Key, 0) set the default value

8 A = Str. Decode ('utf-8 ')

B = Str. encode ('utf-8 ')

Str. isdigit () whether it is a numerical value

Str1 = 'abc % s' % str2

9 Import string

X = string. ascii_lowercase

Then X = 'abcdefghijklmnopqrstuvwxy'

D = enumerate (X)

C = List (d)

Then c = [(0, 'A'), (1, 'B') ......]

For I, J in D:

Then I = 0, 1, 2 ,.....

J = 'A', 'B '......

10 A = test () [0]
When the length of the result returned by test () is 1, the preceding statement is equivalent
A, = test ()

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.