Python Learning (iv)

Source: Internet
Author: User
Tags true true

First, enumerate

Enumerate,for I in enumerate (an iterative object), returns a tuple with the contents (serial number, each element that can be iterated)

For I,j in enumerate (Can iterate object, start serial number), return serial number, iterate over each element

A ='ABC'b= {'a': 1,'b': 2} forIinchEnumerate (a):Print(i)#Output:#(0, ' a ')#(1, ' B ')#(2, ' C ') forI,jinchEnumerate (a,1):    Print(I,J)#Output:#1 A#2 b#3 C

Second, memory address: ID ()

= = Compares the value, is the memory address

Small Data pool: int:-5~256

Str:1. Cannot have spaces

2. Length cannot exceed 20

3. Cannot have special characters

A =[]b= []Print(A = = B,a isb#True Falsea='ABC'b='ABC'Print(A = = B,a isb#true Truea='-6'b='-6'Print(A = = B,a isb#True False

Third, the Code

In Python3, the expression of STR is a string, the expression of bytes is B string, the actual encoding of STR is unicode,bytes encoding method is Utf-8, GBK, gb2312 ...

In Python3, the unicode:1 characters (whether Chinese, English, or numeric) are represented by 4 bytes

Utf-8: A character with a minimum of bytes, 1 bytes in English, 2 bytes in European text, 3 bytes in Asian text

GBK: English is 1 bytes, Chinese is 2 bytes

In Python3, strings exist in memory that is Unicode encoded, cannot be transferred directly or stored on the hard disk, to be converted to bytes type, Unicode to Bytes:a.endode (' encoded '), bytes to Unicode:a.decode ( ' Encoding ')

Utf-8 and GBK, gb2312 and other coding methods do not recognize, if you want to convert, first decoding (decode) into Unicode mode, and then encode (encode) into the corresponding way.

A ='ABC'b= A.encode ('Utf-8')Print(B,type (b))#b ' abc ' <class ' bytes ' >a='Hello'b= A.encode ('Utf-8')Print(B,type (b))#b ' \xe4\xbd\xa0\xe5\xa5\xbd ' <class ' bytes ' >C=B.decode ()Print(C,type (c))#Hello <class ' str ' >D = C.encode ('GBK')Print(D,type (d))#b ' \xc4\xe3\xba\xc3 ' <class ' bytes ' >

Python Learning (iv)

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.