Tài liệu Python 2

Python v2. 7. 2 tài liệu

Chào mừng. Đây là tài liệu cho Python 2. 7. 2, cập nhật lần cuối ngày 19 tháng 10 năm 2011

Các bộ phận của tài liệu

Có gì mới trong Python 2. 7?
hoặc tất cả tài liệu "Có gì mới" kể từ 2. 0

Hướng dẫn
bắt đầu tại đây

Thư viện tham khảo
hãy giữ cái này dưới gối của bạn

Tham chiếu ngôn ngữ
mô tả các yếu tố ngôn ngữ và cú pháp

Thiết lập và sử dụng Python
cách sử dụng Python trên các nền tảng khác nhau

Python HOWTO
tài liệu chuyên sâu về các chủ đề cụ thể

Mở rộng và nhúng
hướng dẫn dành cho lập trình viên C/C++

API Python/C
tham khảo dành cho lập trình viên C/C++

Cài đặt mô-đun Python
thông tin dành cho người cài đặt & quản trị viên hệ thống

Phân phối mô-đun Python
chia sẻ mô-đun với người khác

Hướng dẫn lập tài liệu Python
dành cho tác giả tài liệu

Câu hỏi thường gặp
câu hỏi thường gặp [có câu trả lời. ]

Chỉ số và bảng

Chỉ mục mô-đun chung
truy cập nhanh vào tất cả các mô-đun

Chỉ mục chung
tất cả các hàm, lớp, thuật ngữ

Bảng thuật ngữ
các thuật ngữ quan trọng nhất được giải thích

Trang tìm kiếm
tìm kiếm tài liệu này

Mục lục đầy đủ
liệt kê tất cả các mục và tiểu mục

thông tin meta

Báo cáo lỗi

Về tài liệu

Lịch sử và Giấy phép của Python

bản quyền

Đọc Tài liệu là một dự án cộng đồng phổ biến lưu trữ tài liệu về phần mềm nguồn mở. Nó chứa tài liệu cho nhiều mô-đun Python, cả phổ biến và kỳ lạ

Đọc tài liệu

pydoc¶

pydoc là một tiện ích được cài đặt khi bạn cài đặt Python. Nó cho phép bạn nhanh chóng truy xuất và tìm kiếm tài liệu từ trình bao của mình. Ví dụ: nếu bạn cần xem lại nhanh mô-đun

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
71, việc lấy tài liệu sẽ đơn giản như

Sổ ghi chép này cho bạn thấy các thành ngữ để viết mã chứng minh trong tương lai tương thích với cả hai phiên bản Python. 2 và 3. Nó đi kèm với bài nói chuyện của Ed Schofield tại PyCon AU 2014, “Viết mã tương thích 2/3”. [Video ở đây. http. //www. youtube. com/watch?v=KOqk8j11aAI&t=10m14s. ]

Phiên bản tối thiểu

  • Trăn 2. 2. 7+

  • Trăn 3. 3. 4+

Thành lập¶

Các mục nhập bên dưới đề cập đến các gói có thể cài đặt

futurize             # pip install future
pasteurize           # pip install future
63 này trên PyPI

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six

Các tập lệnh sau đây cũng có thể cài đặt được bằng

futurize             # pip install future
pasteurize           # pip install future
63

futurize             # pip install future
pasteurize           # pip install future

xem http. // trăn-tương lai. tổ chức và https. //pythonhosted. org/sáu/ để biết thêm thông tin

Sự khác biệt cơ bản về cú pháp¶

in¶

# Python 2 only:
print 'Hello'

# Python 2 and 3:
print['Hello']

Để in nhiều chuỗi, hãy nhập

futurize             # pip install future
pasteurize           # pip install future
65 để ngăn Py2 diễn giải nó dưới dạng một bộ

# Python 2 only:
print 'Hello', 'Guido'

________số 8

# Python 2 only:
print >> sys.stderr, 'Hello'

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
0

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
1

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
2

Tăng ngoại lệ¶

futurize             # pip install future
pasteurize           # pip install future
0

futurize             # pip install future
pasteurize           # pip install future
1

Tăng ngoại lệ với truy nguyên

futurize             # pip install future
pasteurize           # pip install future
2

futurize             # pip install future
pasteurize           # pip install future
3

futurize             # pip install future
pasteurize           # pip install future
4

futurize             # pip install future
pasteurize           # pip install future
5

Chuỗi ngoại lệ [PEP 3134]

futurize             # pip install future
pasteurize           # pip install future
6

futurize             # pip install future
pasteurize           # pip install future
7

futurize             # pip install future
pasteurize           # pip install future
8

futurize             # pip install future
pasteurize           # pip install future
9

Bắt ngoại lệ¶

# Python 2 only:
print 'Hello'
0

# Python 2 only:
print 'Hello'
1

Phân công¶

Phép chia số nguyên [làm tròn xuống]

# Python 2 only:
print 'Hello'
2

# Python 2 only:
print 'Hello'
3

“Phép chia thực sự” [phép chia float]

# Python 2 only:
print 'Hello'
4

# Python 2 only:
print 'Hello'
5

“Sư đoàn cũ” [i. e. tương thích với hành vi Py2]

# Python 2 only:
print 'Hello'
6

# Python 2 only:
print 'Hello'
7

Số nguyên dài¶

Các số nguyên ngắn đã biến mất trong Python 3 và

futurize             # pip install future
pasteurize           # pip install future
66 đã trở thành
futurize             # pip install future
pasteurize           # pip install future
67 [không có dấu
futurize             # pip install future
pasteurize           # pip install future
68 trong
futurize             # pip install future
pasteurize           # pip install future
69]

# Python 2 only:
print 'Hello'
8

# Python 2 only:
print 'Hello'
9

Để kiểm tra xem một giá trị có phải là số nguyên [thuộc bất kỳ loại nào]

# Python 2 and 3:
print['Hello']
0

Hằng số bát phân¶

# Python 2 and 3:
print['Hello']
1

# Python 2 and 3:
print['Hello']
2

Backtick lặp lại¶

# Python 2 and 3:
print['Hello']
3

# Python 2 and 3:
print['Hello']
4

Siêu lớp¶

# Python 2 and 3:
print['Hello']
5

# Python 2 and 3:
print['Hello']
6

# Python 2 and 3:
print['Hello']
7

# Python 2 and 3:
print['Hello']
8

Chuỗi và byte¶

Chuỗi ký tự Unicode [văn bản]¶

Nếu bạn đang nâng cấp cơ sở mã Python 2 hiện có, có thể tốt hơn là đánh dấu rõ ràng tất cả các chuỗi ký tự dưới dạng unicode bằng tiền tố

futurize             # pip install future
pasteurize           # pip install future
70

# Python 2 and 3:
print['Hello']
9

Các công cụ

futurize             # pip install future
pasteurize           # pip install future
71 và
futurize             # pip install future
pasteurize           # pip install future
72 hiện không cung cấp tùy chọn để thực hiện việc này tự động

Nếu bạn đang viết mã cho một dự án mới hoặc cơ sở mã mới, bạn có thể sử dụng thành ngữ này để tạo tất cả các chuỗi ký tự trong chuỗi unicode của mô-đun

# Python 2 only:
print 'Hello', 'Guido'
0

xem http. // trăn-tương lai. org/unicode_literals. html để thảo luận thêm về kiểu sử dụng

Ký tự chuỗi byte¶

# Python 2 only:
print 'Hello', 'Guido'
1

Để lặp qua một chuỗi byte với các ký tự bit cao có thể, lấy mỗi ký tự dưới dạng một chuỗi byte có độ dài 1

# Python 2 only:
print 'Hello', 'Guido'
2

Thay vào đó, có thể sử dụng

futurize             # pip install future
pasteurize           # pip install future
73 và
futurize             # pip install future
pasteurize           # pip install future
74 để chuyển đổi một số nguyên thành chuỗi byte 1 ký tự

# Python 2 only:
print 'Hello', 'Guido'
3

dây cơ sở¶

# Python 2 only:
print 'Hello', 'Guido'
4

# Python 2 only:
print 'Hello', 'Guido'
5

unicode¶

# Python 2 only:
print 'Hello', 'Guido'
6

# Python 2 only:
print 'Hello', 'Guido'
7

# Python 2 only:
print 'Hello', 'Guido'
8

ChuỗiIO¶

# Python 2 only:
print 'Hello', 'Guido'
9

Nhập khẩu liên quan đến một gói¶

Giả sử gói là

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
0

và mã dưới đây là trong

futurize             # pip install future
pasteurize           # pip install future
75

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
1

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
2

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
3

Từ điển¶

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
4

Lặp lại qua
futurize             # pip install future
pasteurize           # pip install future
76 khóa/giá trị/mục¶

Các phím dict có thể lặp lại

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
5

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
6

Iterable dict giá trị

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
7

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
8

# Python 2 and 3:
from __future__ import print_function    # [at top of module]

print['Hello', 'Guido']
9

# Python 2 only:
print >> sys.stderr, 'Hello'
0

Các mục chính tả có thể lặp lại

# Python 2 only:
print >> sys.stderr, 'Hello'
1

# Python 2 only:
print >> sys.stderr, 'Hello'
2

# Python 2 only:
print >> sys.stderr, 'Hello'
3

# Python 2 only:
print >> sys.stderr, 'Hello'
4

khóa/giá trị/mục dict dưới dạng danh sách¶

phím dict dưới dạng danh sách

# Python 2 only:
print >> sys.stderr, 'Hello'
5

# Python 2 only:
print >> sys.stderr, 'Hello'
6

giá trị dict như một danh sách

# Python 2 only:
print >> sys.stderr, 'Hello'
7

# Python 2 only:
print >> sys.stderr, 'Hello'
8

# Python 2 only:
print >> sys.stderr, 'Hello'
9

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
00

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
01

các mục chính tả như một danh sách

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
02

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
03

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
04

Hành vi của lớp tùy chỉnh¶

Trình vòng lặp tùy chỉnh¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
05

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
06

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
07

Phương pháp
futurize             # pip install future
pasteurize           # pip install future
77 tùy chỉnh¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
08

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
09

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
10

Phương pháp
futurize             # pip install future
pasteurize           # pip install future
78 so với
futurize             # pip install future
pasteurize           # pip install future
79 tùy chỉnh. ¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
11

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
12

Danh sách so với iterator¶

xrange¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
13

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
14

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
15

phạm vi¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
16

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
17

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
18

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
19

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
20

bản đồ¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
21

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
22

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
23

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
24

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
25

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
26

hình ảnh¶

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
27

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
28

import future        # pip install future
import builtins      # pip install future
import past          # pip install future
import six           # pip install six
29

futurize             # pip install future
pasteurize           # pip install future
00

futurize             # pip install future
pasteurize           # pip install future
01

zip, izip¶

Như trên với

futurize             # pip install future
pasteurize           # pip install future
80 và
futurize             # pip install future
pasteurize           # pip install future
81

bộ lọc, ifilter¶

Như trên với

futurize             # pip install future
pasteurize           # pip install future
82 và
futurize             # pip install future
pasteurize           # pip install future
83 cũng vậy

Nội trang khác¶

Tệp IO với open[]¶

futurize             # pip install future
pasteurize           # pip install future
02

giảm[]¶

futurize             # pip install future
pasteurize           # pip install future
03

futurize             # pip install future
pasteurize           # pip install future
04

raw_input[]¶

futurize             # pip install future
pasteurize           # pip install future
05

futurize             # pip install future
pasteurize           # pip install future
06

đầu vào[]¶

futurize             # pip install future
pasteurize           # pip install future
07

futurize             # pip install future
pasteurize           # pip install future
08

Cảnh báo. sử dụng một trong hai thứ này là không an toàn với đầu vào không đáng tin cậy

tập tin[]¶

futurize             # pip install future
pasteurize           # pip install future
09

futurize             # pip install future
pasteurize           # pip install future
10

điều hành¶

futurize             # pip install future
pasteurize           # pip install future
11

futurize             # pip install future
pasteurize           # pip install future
12

futurize             # pip install future
pasteurize           # pip install future
13

execfile[]¶

futurize             # pip install future
pasteurize           # pip install future
14

futurize             # pip install future
pasteurize           # pip install future
15

futurize             # pip install future
pasteurize           # pip install future
16

unichr[]¶

futurize             # pip install future
pasteurize           # pip install future
17

futurize             # pip install future
pasteurize           # pip install future
18

futurize             # pip install future
pasteurize           # pip install future
19

thực tập sinh[]¶

futurize             # pip install future
pasteurize           # pip install future
20

futurize             # pip install future
pasteurize           # pip install future
21

futurize             # pip install future
pasteurize           # pip install future
22

futurize             # pip install future
pasteurize           # pip install future
23

futurize             # pip install future
pasteurize           # pip install future
24

futurize             # pip install future
pasteurize           # pip install future
25

ứng dụng[]¶

futurize             # pip install future
pasteurize           # pip install future
26

futurize             # pip install future
pasteurize           # pip install future
27

futurize             # pip install future
pasteurize           # pip install future
28

futurize             # pip install future
pasteurize           # pip install future
29

chr[]¶

futurize             # pip install future
pasteurize           # pip install future
30

futurize             # pip install future
pasteurize           # pip install future
31

futurize             # pip install future
pasteurize           # pip install future
32

futurize             # pip install future
pasteurize           # pip install future
33

futurize             # pip install future
pasteurize           # pip install future
34

cmp[]¶

futurize             # pip install future
pasteurize           # pip install future
35

futurize             # pip install future
pasteurize           # pip install future
36

futurize             # pip install future
pasteurize           # pip install future
37

tải lại[]¶

futurize             # pip install future
pasteurize           # pip install future
38

futurize             # pip install future
pasteurize           # pip install future
39

Thư viện tiêu chuẩn¶

mô-đun dbm¶

futurize             # pip install future
pasteurize           # pip install future
40

lệnh / mô-đun quy trình con¶

futurize             # pip install future
pasteurize           # pip install future
41

Mô-đun StringIO¶

futurize             # pip install future
pasteurize           # pip install future
42

futurize             # pip install future
pasteurize           # pip install future
43

mô-đun http¶

futurize             # pip install future
pasteurize           # pip install future
44

mô-đun xmlrpc¶

futurize             # pip install future
pasteurize           # pip install future
45

futurize             # pip install future
pasteurize           # pip install future
46

thoát html và các thực thể¶

futurize             # pip install future
pasteurize           # pip install future
47

phân tích cú pháp html¶

futurize             # pip install future
pasteurize           # pip install future
48

mô-đun urllib¶

futurize             # pip install future
pasteurize           # pip install future
84 là mô-đun khó sử dụng nhất từ ​​​​mã tương thích Python 2/3. Bạn có thể muốn chuyển sang Yêu cầu [http. // yêu cầu python. tổ chức] thay vào đó

Python 2 vẫn có sẵn chứ?

Python 2 có giống Python 3 không?

Python 3 có cú pháp dễ hơn so với Python 2 . Rất nhiều thư viện của Python 2 không tương thích về phía trước. Rất nhiều thư viện được tạo trong Python 3 để được sử dụng nghiêm ngặt với Python 3. Python 2 không còn được sử dụng kể từ năm 2020.

Python 2 hay 3 tốt hơn?

Python 3 là ngôn ngữ tốt hơn và đi kèm với bộ thư viện chuẩn tốt hơn Python 2. Ngoài ra, kể từ năm 2020, ngôn ngữ và thư viện tiêu chuẩn chỉ được cải thiện trong Python 3.

Tài liệu Python có tốt cho người mới bắt đầu không?

Python là ngôn ngữ lập trình mạnh mẽ, dễ học .

Chủ Đề