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 six71, 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 future63 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 future63
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 future65 để 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 six0
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six1
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six2
Tăng ngoại lệ¶
futurize # pip install future pasteurize # pip install future0
futurize # pip install future pasteurize # pip install future1
Tăng ngoại lệ với truy nguyên
futurize # pip install future pasteurize # pip install future2
futurize # pip install future pasteurize # pip install future3
futurize # pip install future pasteurize # pip install future4
futurize # pip install future pasteurize # pip install future5
Chuỗi ngoại lệ [PEP 3134]
futurize # pip install future pasteurize # pip install future6
futurize # pip install future pasteurize # pip install future7
futurize # pip install future pasteurize # pip install future8
futurize # pip install future pasteurize # pip install future9
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 future66 đã trở thành
futurize # pip install future pasteurize # pip install future67 [không có dấu
futurize # pip install future pasteurize # pip install future68 trong
futurize # pip install future pasteurize # pip install future69]
# 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 future70
# Python 2 and 3: print['Hello']9
Các công cụ
futurize # pip install future pasteurize # pip install future71 và
futurize # pip install future pasteurize # pip install future72 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 future73 và
futurize # pip install future pasteurize # pip install future74 để 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 future75
# 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 six00
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six01
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 six02
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six03
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six04
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 six05
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six06
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six07
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 six08
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six09
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six10
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 six11
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six12
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 six13
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six14
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six15
phạm vi¶
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six16
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six17
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six18
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six19
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six20
bản đồ¶
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six21
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six22
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six23
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six24
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six25
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six26
hình ảnh¶
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six27
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six28
import future # pip install future import builtins # pip install future import past # pip install future import six # pip install six29
futurize # pip install future pasteurize # pip install future00
futurize # pip install future pasteurize # pip install future01
zip, izip¶
Như trên với
futurize # pip install future pasteurize # pip install future80 và
futurize # pip install future pasteurize # pip install future81
bộ lọc, ifilter¶
Như trên với
futurize # pip install future pasteurize # pip install future82 và
futurize # pip install future pasteurize # pip install future83 cũng vậy
Nội trang khác¶
Tệp IO với open[]¶
futurize # pip install future pasteurize # pip install future02
giảm[]¶
futurize # pip install future pasteurize # pip install future03
futurize # pip install future pasteurize # pip install future04
raw_input[]¶
futurize # pip install future pasteurize # pip install future05
futurize # pip install future pasteurize # pip install future06
đầu vào[]¶
futurize # pip install future pasteurize # pip install future07
futurize # pip install future pasteurize # pip install future08
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 future09
futurize # pip install future pasteurize # pip install future10
điều hành¶
futurize # pip install future pasteurize # pip install future11
futurize # pip install future pasteurize # pip install future12
futurize # pip install future pasteurize # pip install future13
execfile[]¶
futurize # pip install future pasteurize # pip install future14
futurize # pip install future pasteurize # pip install future15
futurize # pip install future pasteurize # pip install future16
unichr[]¶
futurize # pip install future pasteurize # pip install future17
futurize # pip install future pasteurize # pip install future18
futurize # pip install future pasteurize # pip install future19
thực tập sinh[]¶
futurize # pip install future pasteurize # pip install future20
futurize # pip install future pasteurize # pip install future21
futurize # pip install future pasteurize # pip install future22
futurize # pip install future pasteurize # pip install future23
futurize # pip install future pasteurize # pip install future24
futurize # pip install future pasteurize # pip install future25
ứng dụng[]¶
futurize # pip install future pasteurize # pip install future26
futurize # pip install future pasteurize # pip install future27
futurize # pip install future pasteurize # pip install future28
futurize # pip install future pasteurize # pip install future29
chr[]¶
futurize # pip install future pasteurize # pip install future30
futurize # pip install future pasteurize # pip install future31
futurize # pip install future pasteurize # pip install future32
futurize # pip install future pasteurize # pip install future33
futurize # pip install future pasteurize # pip install future34
cmp[]¶
futurize # pip install future pasteurize # pip install future35
futurize # pip install future pasteurize # pip install future36
futurize # pip install future pasteurize # pip install future37
tải lại[]¶
futurize # pip install future pasteurize # pip install future38
futurize # pip install future pasteurize # pip install future39
Thư viện tiêu chuẩn¶
mô-đun dbm¶
futurize # pip install future pasteurize # pip install future40
lệnh / mô-đun quy trình con¶
futurize # pip install future pasteurize # pip install future41
Mô-đun StringIO¶
futurize # pip install future pasteurize # pip install future42
futurize # pip install future pasteurize # pip install future43
mô-đun http¶
futurize # pip install future pasteurize # pip install future44
mô-đun xmlrpc¶
futurize # pip install future pasteurize # pip install future45
futurize # pip install future pasteurize # pip install future46
thoát html và các thực thể¶
futurize # pip install future pasteurize # pip install future47
phân tích cú pháp html¶
futurize # pip install future pasteurize # pip install future48
mô-đun urllib¶
futurize # pip install future pasteurize # pip install future84 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 đó