2.3. Type conversion functions

Reference
http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/Typeconversionfunctions.html

Sometimes it is necessary to convert values from one type to another. Python provides a few simple functions that will allow us to do that. The functions intfloat and str will (attempt to) convert their arguments into types intfloat and str respectively. We call these type conversion functions.
Đôi lúc thực sự cần thiết để chuyển đổi các giá trị từ định dạng này sang loại khác. Python cung cấp một vài hàm đơn giản sẽ giúp chúng ta làm điều đó. Những hàm int, float và str sẽ (cố gắng để) chuyển đổi các đối số thành dạng số nguyên (int) số thập phân (float) và chuỗi tương ứng. Chúng ta gọi chúng là các hàm chuyển đổi dạng (dữ liệu).
The int function can take a floating point number or a string, and turn it into an int. For floating point numbers, it discards the decimal portion of the number - a process we call truncation towards zero on the number line. Let us see this in action:
Hàm int (số nguyên) có thể nhận một số trước dấu chấm (đại loại lấy số nguyên của số thập phân) hoặc một chuỗi, và chuyển chúng thành số nguyên. Với các số hàng đơn vị của số thập phân, chúng (hàm int) sẽ loại bỏ phần thập phân của số - một quá trình mà chúng ta gọi là cắt ngắn số không trên trên dòng số. Chúng ta hãy cùng xem hành động này:
1
print(3.14, int(3.14))
2
print(3.9999, int(3.9999))       # This doesn't round to the closest int!
3
print(3.0, int(3.0))
4
print(-3.999, int(-3.999))        # Note that the result is closer to zero
5
6
print("2345", int("2345"))        # parse a string to produce an int
7
print(17, int(17))                # int even works on integers
8
print(int("23bottles"))
9
(ch02_20)
The last case shows that a string has to be a syntactically legal number, otherwise you’ll get one of those pesky runtime errors. Modify the example by deleting the bottles and rerun the program. You should see the integer 23.
Trường hợp cuối hiện ra một chuỗi thì phải là một số cú pháp chuẩn (hợp pháp), nếu không thì bạn sẽ nhận được những lỗi khó chịu khi chạy chương trình. Sửa đổi ví dụ bằng xóa đi bottles và chạy lại chương trình. Bạn sẽ nhìn thấy số nguyên 23.
The type converter float can turn an integer, a float, or a syntactically legal string into a float.
Chuyển đổi dạng float (số thập phân) có thể thành một số nguyên, một số thập thân, hay một chuỗi cú pháp chuẩn(hợp pháp)
1
print(float("123.45"))
2
print(type(float("123.45")))
3
(ch02_21)
The type converter str turns its argument into a string. Remember that when we print a string, the quotes are removed. However, if we print the type, we can see that it is definitely str.
Loại chuyển đổi str chuyển đối số thành một chuỗi. Hãy nhớ rằng khi chúng ta hiển thị một chuỗi, trích dẫn (cái dấu ngoặc + ") sẽ bị biến mất. Tuy nhiên, nếu chúng ta in ra loại, chúng ta có thể nhìn thấy nó khi định nghĩa chuỗi.
1
print(str(17))
2
print(str(123.45))
3
print(type(str(123.45)))
4
(ch02_22)

Nhận xét

Bài đăng phổ biến từ blog này

2.7. Operators and Operands - toán tử và toán hạng

1.11. Formal and Natural Languages

3.10 Stack diagrams - sơ đồ xếp chồng