- Nhà
- Hướng dẫn Python
- Số trong Python
Cập nhật lần cuối vào ngày 17 tháng 9 năm 2020
Số trong Python #
Trong Python, các số có 4 loại:
- Integer.
- Điểm nổi hoặc số thực.
- Số phức.
- Boolean.
Các số nguyên hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 148 là những con số không có dấu thập phân. Ví dụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 149,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>0,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>1 là
1 2 3 4 5 6 7 8 9 10 11 12 13 148 nhưng
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>3,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>4,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>5 thì không.
Điểm nổi hoặc thực hoặc
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>6 là những con số có dấu thập phân. Ví dụ,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>7,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>8,
>>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>9 là nổi nhưng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 170,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 171 thì không. Chúng ta cũng có thể viết số điểm nổi bằng cách sử dụng ký hiệu khoa học. Các số được viết ở dạng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 172 được gọi là ký hiệu khoa học. Ký hiệu khoa học là khá hữu ích để viết số rất nhỏ hoặc rất lớn. Ví dụ, float
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 có thể được viết ngắn gọn trong ký hiệu khoa học là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 174. Python sử dụng một cú pháp đặc biệt để viết số trong ký hiệu khoa học. Ví dụ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 173 có thể được viết là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 176. Chữ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 177 được gọi là số mũ và không quan trọng bạn sử dụng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 178 hay
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 177.
Các số phức là các số mà chúng ta không thể biểu diễn trên một dòng số. Một số phức tạp có dạng
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>0, trong đó
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>1 là phần thực và
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>2 là phần tưởng tượng. Ví dụ,
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>3 là số phức. Python cũng sử dụng một cú pháp đặc biệt cho các số phức. Một số nguyên hoặc float với dấu vết
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>4 được coi là một số phức trong Python, do đó
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>5,
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>6 là các số phức tạp.
>>> >>> type[5] # an integer >>> >>> type[3.4] # a float >>> >>> type[5j] # a complex number >>> |
Thử ngay bây giờ
Lưu ý rằng
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>3 có thể được viết bằng Python là
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>9.
Loại Boolean được thảo luận sau trong chương này.
Các chức năng toán học phổ biến #
Python cung cấp chức năng tích hợp sau để giúp bạn hoàn thành các nhiệm vụ lập trình chung:
>>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>0 | Trả về giá trị tuyệt đối của số. Nói cách khác, hàm >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>1 chỉ trả về số mà không có bất kỳ dấu hiệu nào. | >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>2 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>3, >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>4 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>5. |
>>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>6 | Trả lại >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>7. | >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>8 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>9, >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>0 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>1 |
>>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>2 | Làm tròn số vào số nguyên gần nhất. | >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>3 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>4, >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>5 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>6 |
>>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>7 | Vòng tròn >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>8 đến >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>9 sau khi Decimal Point | 1 2 3 4 5 6 7 8 9 10 11 12 130 là 1 2 3 4 5 6 7 8 9 10 11 12 131, 1 2 3 4 5 6 7 8 9 10 11 12 132 là 1 2 3 4 5 6 7 8 9 10 11 12 133 |
1 2 3 4 5 6 7 8 9 10 11 12 134 | Trả về mặt hàng nhỏ nhất trong số 1 2 3 4 5 6 7 8 9 10 11 12 135, 1 2 3 4 5 6 7 8 9 10 11 12 136, ... 1 2 3 4 5 6 7 8 9 10 11 12 137 | 1 2 3 4 5 6 7 8 9 10 11 12 138 là 1 2 3 4 5 6 7 8 9 10 11 12 139, >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>00 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>01 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>02 | Trả về mục lớn nhất trong số 1 2 3 4 5 6 7 8 9 10 11 12 135, 1 2 3 4 5 6 7 8 9 10 11 12 136, ... 1 2 3 4 5 6 7 8 9 10 11 12 137 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>06 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>07, >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>08 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>09 |
hàm abs [] #
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>> |
Thử ngay bây giờ
Lưu ý rằng >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
3 có thể được viết bằng Python là >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
9.
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>> |
Thử ngay bây giờ
Lưu ý rằng >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
3 có thể được viết bằng Python là >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>> |
Thử ngay bây giờ
Lưu ý rằng >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
3 có thể được viết bằng Python là >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
9.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>> |
Thử ngay bây giờ
Lưu ý rằng
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>3 có thể được viết bằng Python là
>>> >>> max[1, 4, 100] # Find the largest among 1, 4 and 100 100 >>> >>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12 91.12 >>> >>> max[-67, -17, 0] # Find the largest among -67, 417 and 0 0 >>> >>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921 -12300000000.0 >>> >>> >>> min[92, 6, -102] # Find the largest among 92, 6, -102 -102 >>>9.
Loại Boolean được thảo luận sau trong chương này.
>>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>0 | Trả về giá trị tuyệt đối của số. Nói cách khác, hàm >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>1 chỉ trả về số mà không có bất kỳ dấu hiệu nào. | >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>2 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>3, >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>4 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>5. |
>>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>6 | Trả lại >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>7. | >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>8 là >>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>>9, >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>0 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>1 |
>>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>2 | Làm tròn số vào số nguyên gần nhất. | >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>3 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>4, >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>5 là >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>6 |
>>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>7 | Vòng tròn >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>8 đến >>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>>9 sau khi Decimal Point | 1 2 3 4 5 6 7 8 9 10 11 12 130 là 1 2 3 4 5 6 7 8 9 10 11 12 131, 1 2 3 4 5 6 7 8 9 10 11 12 132 là 1 2 3 4 5 6 7 8 9 10 11 12 133 |
1 2 3 4 5 6 7 8 9 10 11 12 134 | Trả về mặt hàng nhỏ nhất trong số 1 2 3 4 5 6 7 8 9 10 11 12 135, 1 2 3 4 5 6 7 8 9 10 11 12 136, ... 1 2 3 4 5 6 7 8 9 10 11 12 137 | 1 2 3 4 5 6 7 8 9 10 11 12 138 là 1 2 3 4 5 6 7 8 9 10 11 12 139, >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>00 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>01 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>02 | Trả về mục lớn nhất trong số 1 2 3 4 5 6 7 8 9 10 11 12 135, 1 2 3 4 5 6 7 8 9 10 11 12 136, ... 1 2 3 4 5 6 7 8 9 10 11 12 137 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>06 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>07, >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>08 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>09 |
hàm abs [] # | hàm pow [] # | chức năng vòng [] # |
hàm tối đa [] và min [] # | Mô -đun >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>10 của Python cũng cung cấp một số hàm và hằng số toán học tiêu chuẩn. Hãy nhớ lại rằng, để sử dụng mô -đun toán học, trước tiên chúng ta cần nhập nó bằng câu lệnh >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>11 như sau: | Bảng sau liệt kê một số hàm và hằng số tiêu chuẩn trong mô -đun >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>10. |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>13 | Trả về giá trị của >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>14 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>13 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>16 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>17 | Trả về giá trị của 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 178 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>17 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>20 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>21 | Trả về số nguyên nhỏ nhất lớn hơn hoặc bằng >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>22 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>23 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>24 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>25 | Trả về số nguyên lớn nhất nhỏ hơn hoặc bằng >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>22 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>27 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>28 |
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>29 | Trả về giá trị tuyệt đối là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>30 là >>> >>> round[32.3] # round 32.3 to the nearest integer 32 >>> round[99.7] # round 99.7 to the nearest integer 100 >>> round[-5.23] # round -5.23 to the nearest integer -5 >>> >>> round[3.14159, 2] # round 3.14159 to 2 decimal places 3.14 >>> >>> round[2.71828, 3] # round 2.71828 to 3 decimal places 2.718 >>>6 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>32 là >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>33 |
Thử ngay bây giờ
Lưu ý rằng >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
7 chỉ đại diện cho phần tưởng tượng của số phức. Để tạo một số phức tạp với phần thực và tưởng tượng, chỉ cần thêm một số vào phần tưởng tượng. Ví dụ, số phức >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
3 có thể được viết bằng Python là >>>
>>> max[1, 4, 100] # Find the largest among 1, 4 and 100
100
>>>
>>> max[-21, 4.5, 91.12] # Find the largest among -21, 4.5 and 91.12
91.12
>>>
>>> max[-67, -17, 0] # Find the largest among -67, 417 and 0
0
>>>
>>> min[0, -1.23e10, -9921] # Find the smallest among 0, -1.23e10 and -9921
-12300000000.0
>>>
>>>
>>> min[92, 6, -102] # Find the largest among 92, 6, -102
-102
>>>
9.
>>> >>> math.pi 3.141592653589793 >>> >>> math.e 2.718281828459045 >>> |
Loại Boolean được thảo luận sau trong chương này.
>>> >>> math.ceil[3.5] # find the smallest integer greater than or equal to 3.5 4 >>> math.floor[3.5] # find the largest integer smaller than or equal to 3.5 3 >>> |
Các chức năng toán học phổ biến #
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>0 |
Python cung cấp chức năng tích hợp sau để giúp bạn hoàn thành các nhiệm vụ lập trình chung:
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>1 |
Math.sin [], Math.cos [] và Math.tan [] hàm #
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>2 |
Chức năng Math.Degrees [] và Math.Radians [] #
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>3 |
Đây chỉ là một hàm danh sách ngắn và hằng số trong mô -đun toán học, để xem danh sách đầy đủ, hãy truy cập //docs.python.org/dev/l Library/math.html.
Định dạng số #
Đôi khi nó là mong muốn để in số theo một định dạng cụ thể. Xem xét ví dụ sau:
python101/Chapter-05/simple_interest_calculator.py
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>4 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>5 |
Output:
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>6
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>7 |
Chúng tôi muốn xuất ra
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>67 không phải
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69.
python101/Chapter-05/simple_interest_calculator_using_format_function.py
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>4 | >>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>9 |
Output:
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>0
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
Chúng tôi muốn xuất ra >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
67 không phải >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69.
Hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 được giải thích trong phần tiếp theo.
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>1
định dạng [] hàm #
Cú pháp của hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 như sau:
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>73 là dữ liệu chúng tôi muốn định dạng.
>>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
74 là một chuỗi xác định cách định dạng giá trị được truyền đến hàm >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69.
Khi thành công
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 trả về một chuỗi được định dạng.
Định dạng số điểm nổi #
Để định dạng số điểm thả nổi, chúng tôi sử dụng trình mô tả định dạng sau.
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>77 là số lượng ký tự tối thiểu dành cho một giá trị và
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>78 đề cập đến số lượng ký tự sau điểm thập phân.
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>77 bao gồm các chữ số trước và sau ký tự thập phân và số thập phân. Ký tự
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>80 theo sau là
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>78 thể hiện rằng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 sẽ xuất ra giá trị dưới dạng số điểm nổi. Ký tự
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>80 còn được gọi là mã loại hoặc trình xác định. Có nhiều nhà xác định khác, như chúng ta sẽ thấy.
Theo mặc định, tất cả các loại số được căn chỉnh đúng. Nếu chiều rộng lớn hơn chiều dài của giá trị thì các số được in đúng với các không gian hàng đầu được xác định bằng cách trừ chiều dài của giá trị từ chiều rộng. Mặt khác, nếu chiều rộng nhỏ hơn chiều dài của giá trị thì chiều dài của chiều rộng được tự động tăng lên để phù hợp với chiều dài của giá trị và không có khoảng trống nào được thêm:
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>2 |
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
Chúng tôi muốn xuất ra >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
67 không phải >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69.:
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>3 |
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
Chúng tôi muốn xuất ra
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>67 không phải
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69.
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>4 |
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
Chúng tôi muốn xuất ra >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
67 không phải >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69.
Hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 được giải thích trong phần tiếp theo.
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>6 |
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
65, nhưng hàm >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
Chúng tôi muốn xuất ra
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>67 không phải
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69.
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>7 |
Thử ngay bây giờ
Lưu ý cách số tiền được hiển thị trong đầu ra, nó chứa
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>64 chữ số sau thập phân. Đây là vấn đề rất phổ biến khi số điểm nổi được in sau khi thực hiện các tính toán. Vì số tiền là tiền tệ, nên định dạng nó thành hai vị trí thập phân. Chúng ta có thể dễ dàng làm tròn số đến 2 số thập phân bằng cách sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65, nhưng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>65 sẽ không luôn luôn đưa ra câu trả lời chính xác. Xem xét mã sau:
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>8 |
Chúng tôi muốn xuất ra >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
67 không phải >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
68. Chúng ta có thể khắc phục sự cố này bằng cách sử dụng chức năng >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69. Dưới đây là phiên bản sửa đổi của chương trình trên sử dụng phương thức >>>
>>> abs[-100] # absolute value of -100
100
>>>
>>> abs[291.121] # absolute value of 291.121
291.121
>>>
>>> abs[88] # absolute value of 88
88
>>>
69.
Hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 được giải thích trong phần tiếp theo.
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 140 |
Thử ngay bây giờ
Cài đặt căn chỉnh #
Chúng tôi đã thảo luận rằng theo mặc định, các số được in đúng. Ví dụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 141 |
Thử ngay bây giờ
Chúng ta có thể thay đổi căn chỉnh mặc định bằng cách sử dụng hai ký hiệu sau:
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>06 | Đầu ra giá trị đúng biện minh trong chiều rộng được chỉ định |
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>07 | Đầu ra, giá trị còn lại hợp lý trong chiều rộng được chỉ định |
Biểu tượng căn chỉnh phải đến trước chiều rộng được chỉ định.
1 2 3 4 5 6 7 8 9 10 11 12 13 142 |
Ở đây chúng tôi đang in số còn lại chính đáng, do kết quả không gian kéo dài được thêm vào thay vì không gian hàng đầu.
Lưu ý rằng câu lệnh
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>08 và
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>09 giống như đúng là định dạng mặc định cho các số in.
Định dạng số nguyên #
Chúng ta cũng có thể sử dụng hàm
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>69 để định dạng số nguyên. Loại mã
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>11,
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>12,
>>> >>> pow[3, 3] # calculate 3^3 27 >>> >>> pow[0.35, 2] # calculate 0.35^2 0.12249999999999998 >>> >>> pow[9, -2] # calculate 9^-2 0.012345679012345678 >>>13,
>>> >>> abs[-100] # absolute value of -100 100 >>> >>> abs[291.121] # absolute value of 291.121 291.121 >>> >>> abs[88] # absolute value of 88 88 >>>30 có thể được sử dụng để định dạng theo số thập phân, nhị phân, octal và thập lục phân tương ứng. Hãy nhớ rằng trong khi định dạng số nguyên chỉ được phép không chính xác.
1 2 3 4 5 6 7 8 9 10 11 12 13 143 |
Thử ngay bây giờ
1 2 3 4 5 6 7 8 9 10 11 12 13 144 |
1 2 3 4 5 6 7 8 9 10 11 12 13 145 |
1 2 3 4 5 6 7 8 9 10 11 12 13 146 |
1 2 3 4 5 6 7 8 9 10 11 12 13 147 |