Python 2.4 vs. Python 3.0

Source: Internet
Author: User
Tags chr ord



Turn around and save it for future viewing.



"Turn from:" Http://hi.baidu.com/autoitcn/blog/item/5f41973294b5fc4fac4b5f77.html



Python 2.4 vs. Python 3.0

One, the print from the statement into a function

Original: Print 1, 2+3

Instead: Print (1, 2+3)

Two, range and xrange

Original: Range (0, 4) result is list [0,1,2,3]

Instead: List (range (0,4))

Original: xrange (0, 4) variable control for the For loop

Instead: Range (0,4)

Three, string

Original: string stored in 8-bit string

Instead: string is stored in 16-bit Unicode string



Iv. changes to the try except statement

Original: Try:

......

Except Exception, E:

......

Switch

Try

......

Except Exception as E:

......

V. Open File

Original: File (...)

or open (...)

Switch

Only open (...)

Six, input a string from the keyboard

Original: Raw_input ("hint message")

Instead: input ("hint message")

Vii.. Bytes data types





A Bytes Object is an immutable array. The items is 8-bit bytes, represented by integers in the range 0 <= x < 256.

Bytes can be thought of as "byte array" objects, each element is a 8-bit byte, and the value range 0~255.

Since strings are stored in Unicode encoding in Python 3.0, when a binary file is written, the string cannot be written directly (or read) and must be encoded in some way into a sequence of bytes before it can be written.

(a) string encoding (encode) is bytes

Example: s = "Zhang San abc12"

b = s.encode (encoded)

# b is the bytes type of data

# Common encoding methods are: "Uft-16", "Utf-8", "GBK", "gb2312", "ASCII", "latin1" and so on.

# Note: An exception is thrown when a string cannot be encoded as a specified "encoding method"

(ii) bytes decoding (decode) as a string

s = "Zhang San abc12"

b = S.encode ("GBK") # string s encoded as a sequence of bytes in GBK format

S1 = B.decode ("GBK") # decodes byte sequence B into a string in GBK format

# description, an exception is thrown when a sequence of bytes cannot be decoded in the specified encoding format

(iii) Examples of using methods

#coding =GBK

f = open ("C:\\1234.txt", "WB")
s = "Dick and Harry Abcd1234"
# -------------------------------
# in python2.4 we can write this:
# F.write (s)
# but throws an exception in Python 3.0
# -------------------------------
b = S.encode ("GBK")
F.write (b)
F.close ()

Input ("?")

An example of reading this file:

#coding =GBK

f = open ("C:\\1234.txt", "RB")
F.seek (0,2) #定位至文件尾
n = F.tell () #读取文件的字节数
F.seek (0,0) #重新定位至文件开始处
b = F.read (n)
# ------------------------------
# in Python 2.4 b is a string type
# to Python 3.0, B is the bytes type
# Therefore, you need to specify the encoding method to confirm the code
# ------------------------------
s = B.decode ("GBK")
Print (s)
# ------------------------------
# in Python 2.4 You can write print s or print (s)
# to Python 3.0 must write print (s)
# ------------------------------
F.close ()
Input ("?")

Should be displayed after running:

Dick and Harry Abcd1234







(iv) bytes sequence, one but formed, whose contents are immutable

Cases:

S= "ABCD"

B=s.encode ("GBK")

Print B[0] # show 65

B[0] = 66

# Execute the sentence, an exception occurred: ' bytes ' object does not the support item assignment





Viii. chr (K) and Ord (c)

Python 2.4.2 before

Chr (k) converts the encoded k to characters, the range of K is 0 ~ 255

Ord (c) takes a single character encoding, the range of return values: 0 ~ 255

Python 3.0

Chr (k) converts the encoded k to characters, the range of K is 0 ~ 65535

Ord (c) takes a single character encoding, the range of return values: 0 ~ 65535

Nine, division operator

Python 2.4.2 before

10/3 Results of 3

Python 3.0

10/3 Results of 3.3333333333333335

10//3 result is 3

Ten, byte array object---New

(i) initialization

A = ByteArray (10)

# A is an array of 10 bytes, each of which is a byte, and the type borrows an int

# at this point, each element has an initial value of 0

(b) The byte array is mutable

A = ByteArray (10)

A[0] = 25

# You can change its elements with an assignment statement, but the assigned value must be between 0 and 255

(c) A slice of a byte array is still an array of bytes

(iv) Converting a string into a byte array

#coding =GBK

s = "Hello"

b = S.encode ("GBK") # first converts a string into bytes by some sort of "GBK" encoding

c = ByteArray (b) #再将 bytes into a byte array

can also write

c = ByteArray ("Hello", "GBK")



(v) byte arrays converted to strings

c = ByteArray (4)

C[0] = 65; c[1]=66; C[2]= 67; C[3]= 68

s = C.decode ("GBK")

Print (s)

# should show: ABCD



(vi) Byte array can be used to write to a text file

#coding =GBK

f = open ("C:\\1234.txt", "WB")
s = "Dick and Harry Abcd1234"
# -------------------------------
# in python2.4 we can write this:
# F.write (s)
# but throws an exception in Python 3.0
# -------------------------------
b = S.encode ("GBK")

F.write (b)
C=bytearray ("Harry", "GBK")
F.write (c)
F.close ()

Input ("?")





Rookiedong's Supplement

1, "Import thread" problem, 2.x module thread in 3.x programming "_thread" (need to be preceded by an underscore). Otherwise, the Importerror:no module named thread will appear.

Python 2.4 vs. Python 3.0

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.