Tôi viết blog này với ý định cung cấp một bối cảnh sâu sắc và tầm quan trọng hơn để in mà nó hiếm khi nhận được.
Khá nhiều lập trình viên học cách viết mã bằng cách in
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
9. Tuy nhiên, hiếm khi bất kỳ blog hoặc video nào trên internet đi sâu vào những bí ẩn của bản in trong Python.rarely any blog or video on the internet takes a deep dive into the mysteries of print in Python.Để giúp các lập trình viên Python 2 chuyển sang Python 3, tôi sẽ cung cấp các ví dụ cho cả hai phiên bản của Python 2 và 3. Bằng cách đó bạn biết về lịch sử của
# Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
0 và nó đã thay đổi như thế nào trong Python 3.Nếu bạn là một lập trình viên Python 2, tôi khuyên bạn nên chuyển sang Python 3. Kiểm tra bài viết của tôi về Benfits of Python 3 qua Python 2.
Mục lục
- Python được gõ linh hoạt
- Viết chương trình Hello World
- Cú pháp chung của bản in
- In nhiều yếu tố
- In vào một dòng mới
- In vào một tệp
- Đỏ bừng mặt
- Ví dụ in Python
- Chương trình Python để in trên cùng một dòng
Python được gõ linh hoạt
Viết chương trình Hello World refers to type-checking in programming languages. There are 2 types of type-checking.
- Cú pháp chung của bản in - Data type is checked during compilation
- In nhiều yếu tố - Data type is checked during execution
In vào một dòng mới It executes each statement line by line and thus type-checking happens on the fly, during execution.
In vào một tệp
Viết chương trình Hello World
Cú pháp chung của bản in
#Python 2
print 'Hello World'
>>> Hello World
# Python 3
print['Hello World']
>>> Hello World
In nhiều yếu tố. It is that easy.
In vào một dòng mớifunction in Python 3. It was a statement in Python 2.
In vào một tệp
Đỏ bừng mặt
Ví dụ in Python
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
Chương trình Python để in trên cùng một dòngwithout using print.
# Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
Gõ đề cập đến việc kiểm tra loại trong các ngôn ngữ lập trình. Có 2 loại kiểm tra loại.sys.stdout to output your message to the standard output stream.
TUYỆT VỜI - Kiểu dữ liệu được kiểm tra trong quá trình biên dịchPrint is just easier to use.
Cú pháp chung của bản in
In nhiều yếu tố
# Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
In vào một dòng mớiPython converts the objects into strings[if they are not strings] and then writes to the standard output stream.
In vào một tệp
Đỏ bừng mặt
# Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
Ví dụ in Python
Chương trình Python để in trên cùng một dòng
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
print['Python', 3, 'Rocks', sep=' ']
>>> Python 3 Rocks
Gõ đề cập đến việc kiểm tra loại trong các ngôn ngữ lập trình. Có 2 loại kiểm tra loại.there is no
# Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
6 argument in Python 2. White space is added in between objects by default.TUYỆT VỜI - Kiểu dữ liệu được kiểm tra trong quá trình biên dịch
print['Python', 3, 'Rocks', sep='|']
>>> Python|3|Rocks
Được đánh máy động - Kiểu dữ liệu được kiểm tra trong quá trình thực thi
# Python 2
from __future__ import print_function
print["Python","Rocks", sep="|"]
In vào một dòng mới
In vào một tệp
# Python 2
print 'Python', 2, 'Rocks'
print 'I love Python'
>>> Python 2 Rocks
>>> I love Python
# Python 3
print['Python', 3, 'Rocks']
print['I love Python']
>>> Python 3 Rocks
>>> I love Python
Đỏ bừng mặtPython by default prints it to a newline.
Ví dụ in Python
Chương trình Python để in trên cùng một dòng
Gõ đề cập đến việc kiểm tra loại trong các ngôn ngữ lập trình. Có 2 loại kiểm tra loại., you can end the print statement in Python 2 with a comma [
# Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
2]. TUYỆT VỜI - Kiểu dữ liệu được kiểm tra trong quá trình biên dịchwhitespace character string to print to the same line in Python 3.
# Python 2
print 'Python', 2, 'Rocks',
print 'I love Python'
>>> Python 2 Rocks I love Python
# Python 3
print['Python', 3, 'Rocks', end=' ']
print['I love Python']
>>> Python 3 Rocks I love Python
Được đánh máy động - Kiểu dữ liệu được kiểm tra trong quá trình thực thi
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
0Python là một ngôn ngữ được giải thích. Nó thực thi từng dòng câu lệnh theo từng dòng và do đó kiểm tra loại xảy ra khi đang bay, trong quá trình thực thi.
Do đó, Python là một ngôn ngữ được đánh máy động.
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
1In vào một tệp
Đỏ bừng mặtfile using print in Python. For this purpose, use the
# Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
8 argument.# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
2Trong ví dụ trên, tin nhắn được ghi vào một tệp có tên
# Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
9 trong cùng một thư mục. Lưu ý rằng việc sử dụng chức năng tích hợp # Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
0 để lưu các tin nhắn vào tệp.Nếu bạn muốn gửi tin nhắn của mình đến bất kỳ nơi nào khác ngoài đầu ra tiêu chuẩn, hãy đảm bảo cung cấp một đối tượng tệp có phương thức ghi cho nó.write method to it.
Để ghi vào một tệp trong một thư mục khác, hãy cung cấp đường dẫn đầy đủ của tệp trong chức năng in của bạn.
Flushing in in
Đây có lẽ là một trong những khái niệm bị bỏ qua nhất. Có lẽ bởi vì chúng tôi không thấy bất kỳ tác động trực tiếp nào trong khi chúng tôi đang in. Nhưng nó là một khái niệm quan trọng.
Thông thường, Python đệm các tin nhắn mà bạn muốn in cho đến khi nó nhận được một dòng mới [____ 29].
Bộ đệm là gì?
Một bộ đệm tạm thời lưu trữ dữ liệu đang được truyền từ nơi này sang nơi khác trong bộ nhớ chính.
Đối số
# Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
2 đảm bảo rằng mọi thứ trong bộ đệm ngay lập tức được gửi đến đích.Hãy cùng xem điều này thông qua một ví dụ. Thay vì giá trị mặc định cho đối số
# Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
0 [# Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
9], chúng ta sẽ để nó trống.# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
3Khi bạn chạy đoạn trích ở trên, thông báo in sẽ chỉ hiển thị sau khi 5 giây ngủ kết thúc. Tại sao? Bởi vì Print mong đợi một
# Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
9 hoặc một dòng mới ở cuối mỗi câu lệnh in. Do đó, thông điệp của bạn nằm trong bộ đệm.Why? because print expects a # Python 2 and Python 3
import sys
sys.stdout.write['Hello World']
>>> Hello World
9 or a newline at the end of every print statement. Hence, your message is in the buffer.Sử dụng đối số
# Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
2, trong Python 3, bạn có thể in trực tiếp thông báo vào luồng đầu ra tiêu chuẩn mà không phải đợi thời gian ngủ kết thúc.Thử thứ này đi.
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
4Ví dụ in Python
Chương trình Python để in trên cùng một dòng
Giả sử bạn có một loạt các câu lệnh in.
Mã số
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
5Đầu ra sẽ trông giống như dưới đây.
Đầu ra
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
6Tuy nhiên, nếu bạn muốn in đi lặp lại cùng một dòng trong Python, bạn phải sử dụng biểu tượng Return Return
# Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
7 với đối số # Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
0.if you want to print over and over the same line in Python, you have to
use the carriage return # Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
7 symbol with the # Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
0 argument.Mã số
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
7Đầu ra
# Python 2
$ my-folder: python
Python 2.7.10 [default, Aug 17 2018, 19:45:58]
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 [clang-1000.0.42]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
# Python 3
$ my-folder: python3
Python 3.6.5 [v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55]
[GCC 4.2.1 [Apple Inc. build 5666] [dot 3]] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'Hello World'
'Hello World'
8Tuy nhiên, nếu bạn muốn in đi lặp lại cùng một dòng trong Python, bạn phải sử dụng biểu tượng Return Return
# Python 2
print 'Python', 2, 'Rocks'
>>> Python 2 Rocks
# Python 3
print['Python', 3, 'Rocks']
>>> Python 3 Rocks
7 với đối số # Python 2
"print" [[expression ["," expression]* [","]]
| ">>" expression [["," expression]+ [","]]]
# Python 3
print[*objects, sep=' ', end='\n', file=sys.stdout, flush=False]
0.stdout line start at the beginning of the current line.Mặc dù 2 câu lệnh in đầu tiên được thực hiện, nhưng việc trả lại vận chuyển làm cho dòng stdout tiếp theo bắt đầu ở đầu dòng hiện tại.n at the end.