Trong chương trình này, bạn sẽ học cách chuyển đổi thập phân thành nhị phân, bát phân và thập lục phân và hiển thị nó.
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Lập trình Python Chức năng tích hợp
Hệ thống thập phân là hệ thống số được sử dụng rộng rãi nhất. Tuy nhiên, máy tính chỉ hiểu nhị phân. Các hệ thống số nhị phân, bát phân và thập lục phân có liên quan chặt chẽ với nhau và chúng ta có thể yêu cầu chuyển đổi thập phân thành các hệ thống này.
Hệ thống thập phân là cơ sở 10 [mười ký hiệu, 0-9, được sử dụng để đại diện cho một số] và tương tự, nhị phân là cơ sở 2, octal là cơ sở 8 và thập lục phân là cơ sở 16.
Một số có tiền tố ________ 11 & nbsp; được coi là nhị phân, ________ 12 & nbsp; được coi là bát phân và ____ 13 & nbsp; như hình lục giác. Ví dụ:
60 = 0b11100 = 0o74 = 0x3c
Mã nguồn
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
Đầu ra
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.
Lưu ý: Để kiểm tra chương trình cho các số thập phân khác, thay đổi giá trị của DEC trong chương trình. To test the program for other decimal numbers, change the value of dec in the program.
Trong chương trình này, chúng tôi đã sử dụng các hàm tích hợp
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
4, # Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
5 và # Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
6 để chuyển đổi số thập phân đã cho thành các hệ thống số tương ứng.Các hàm này có một số nguyên [theo số thập phân] và trả về một chuỗi.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc takes an integer and returns the octal representation in a string format.
Python OCT [] Chức năng cú pháp
Cú pháp: OCT [x] oct[x]
Thông số :
- X - phải là một số nguyên và có thể ở định dạng nhị phân, thập phân hoặc thập lục phân. – Must be an integer number and can be in either binary, decimal or hexadecimal format.
Trả về: Biểu diễn bát phân của giá trị. octal representation of the value.
Lỗi và ngoại lệ: & nbsp;
- TypeError: Tăng TypeError khi bất cứ thứ gì khác ngoài hằng số loại số nguyên được truyền dưới dạng tham số.Raises TypeError when anything other than integer type constants are passed as parameters.
Ví dụ về chức năng Python OCT []
Python3
Output:
0o12
Ví dụ 1: Chuyển đổi cơ sở từ thập phân và nhị phân bằng hàm OCT [] conversion from decimal and binary using oct[] function
Sử dụng OCT [] để chuyển đổi số từ các cơ sở khác nhau sang Octal.
Python3
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
7# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
8# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
9# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
8The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.1
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.2
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
7# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
8# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
9# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
8The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.7
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.2
Đầu ra: & nbsp;
0o6 0o13
Ví dụ 2: Python OCT [] cho các đối tượng tùy chỉnh
Thực hiện __int __ [] Phương pháp ma thuật để hỗ trợ chuyển đổi bát phân trong lớp toán học.
Python3
The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal. 0x158 in hexadecimal.9
0o120
0o121
0o122
0o123
0o124
0o121
0o126
0o127
0o128
0o129
0o6 0o130
0o6 0o131
0o128
0o6 0o133
0o121
0o126
0o6 0o136
0o128
0o129
0o6 0o130
0o6 0o131
0o128
0o6 0o133
0o121
0o126
0o6 0o136
0o128
0o129
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
7# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
8# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
90o1149
Output:
0o114
0o114
30o12
3 0o114
5 : Demonstrate TypeError in oct[] method
Python3
Ví dụ 3: Thể hiện phương thức Kiểu trong OCT []
Đầu ra: & nbsp;
Traceback [most recent call last]: File "/home/5bf02b72de26687389763e9133669972.py", line 3, in print["The Octal representation of 29.5 is "+oct[29.5]] TypeError: 'float' object cannot be interpreted as an integer
Ví dụ 2: Python OCT [] cho các đối tượng tùy chỉnh Python oct[] is used in all types of standard conversion. For example, Conversion from decimal to octal, binary to octal, hexadecimal to octal forms respectively.
Vấn đề: Viết một chương trình bằng Python để chuyển đổi số thập phân thành biểu diễn bát phân tương ứng của nó. Write a Program in Python to convert a decimal number into its corresponding octal representation. Example:Input: 8
Output: 10
Input: 15
Output: 17
Chuyển đổi thập phân sang bát phân trong Python bằng cách sử dụng vòng lặp
Cách tiêu chuẩn để chuyển đổi số thập phân thành một bát phân là chia số thập phân cho 8 cho đến khi giảm xuống còn 0.
Sau khi phân chia kết thúc, nếu chúng ta xếp các phần còn lại theo cách từ dưới lên, giá trị kết quả sẽ là số octal tương đương.
decimal = int[input["Enter a decimal number: "]]
octal = 0
ctr = 0
temp = decimal #copying number
#computing octal using while loop
while[temp > 0]:
octal += [[temp%8]*[10**ctr]] #Stacking remainders
temp = int[temp/8] #updating dividend
ctr += 1
print["Binary of {x} is: {y}".format[x=decimal,y=octal]]
Output::
Nhập số thập phân: 15 nhị phân 15 là: 17
Binary of 15 is: 17
Chuyển đổi thập phân sang bát phân trong Python bằng cách sử dụng đệ quy
Để chuyển đổi thập phân thành bát phân bằng cách sử dụng đệ quy, chúng tôi chuyển chỉ số [cổ tức/8] sang cuộc gọi đệ quy tiếp theo và đầu ra giá trị còn lại [cổ tức%8].
Chúng tôi lặp lại quá trình cho đến khi số giảm xuống 0 [nghĩa là cho đến số thập phân> 0].
Kể từ khi đệ quy thực hiện ngăn xếp, phần còn lại được in theo cách từ dưới lên và chúng tôi nhận được số octal tương đương.
def dectoOct[decimal]:
if[decimal > 0]:
dectoOct[[int][decimal/8]]
print[decimal%8, end='']
decimal = int[input["Enter a decimal number: "]]
print["Octal: ", end='']
dectoOct[decimal]
Output::
Nhập số thập phân: 8 Octal: 10
Octal: 10
Chuyển đổi thập phân sang bát phân bằng OCT []
Phương pháp Python tích hợp
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
5 trả về biểu diễn bát phân của số thập phân được truyền dưới dạng tham số.Nó trả về một số bát phân dưới dạng
Traceback [most recent call last]: File "/home/5bf02b72de26687389763e9133669972.py", line 3, in print["The Octal representation of 29.5 is "+oct[29.5]] TypeError: 'float' object cannot be interpreted as an integer9, trong đó
Input: 8 Output: 10 Input: 15 Output: 170 là giá trị số octal thực tế.
# Python program to convert decimal into other number systems
dec = 344
print["The decimal value of", dec, "is:"]
print[bin[dec], "in binary."]
print[oct[dec], "in octal."]
print[hex[dec], "in hexadecimal."]
0Nhận xét bên dưới nghi ngờ hoặc đề xuất của bạn nếu bạn có bất kỳ.