Mọi người ơi. Trong bài viết này, chúng ta sẽ tập trung vào biểu diễn Bát phân của một giá trị số nguyên bằng hàm Python oct[]
Bắt đầu với hàm oct[] trong Python
Python có nhiều hàm dựng sẵn khác nhau để thao tác và vận hành trên các giá trị dữ liệu.
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]1 được sử dụng để biểu diễn giá trị số nguyên thành định dạng biểu diễn Octal
cú pháp
oct[number]
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]
2. Giá trị số nguyên phải được truyền cho hàm. Nó có thể là giá trị thập phân, nhị phân hoặc thập lục phân
Thí dụ
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]
đầu ra
Octal representation of decimal number: 0o2 Octal representation of binary number: 0o2 Octal representation of decimal number: 0o27
Lỗi và ngoại lệ với hàm oct[] trong Python
Nếu một giá trị kiểu float được truyền cho hàm oct[] của Python, nó sẽ tạo ra một ngoại lệ
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]3 i. e. Hàm oct[] chỉ chấp nhận các giá trị số nguyên làm tham số
Thí dụ
dec_num = 2.4 print["Octal representation of decimal number:", oct[dec_num]]
đầu ra
TypeError Traceback [most recent call last] in 1 dec_num = 2.4 ----> 2 print["Octal representation of decimal number:", oct[dec_num]] TypeError: 'float' object cannot be interpreted as an integer
Biểu diễn bát phân của các phần tử của một mảng trong mô-đun NumPy
Các phần tử chứa trong cấu trúc dữ liệu như mảng NumPy, danh sách, v.v. có thể được chuyển đổi thành dạng Bát phân của nó bằng cách sử dụng chức năng tích hợp sẵn của NumPy
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]4 được sử dụng để chuyển đổi mọi phần tử của mảng sang dạng bát phân theo kiểu thông minh về phần tử
cú pháp
numpy.base_repr[number, base, padding]
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]
2. Phần tử của mảng có định dạng bát phân phải biểu diễndec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]
6. Giá trị đại diện cho hệ thống số kết quả của giá trị được chuyển đổi. Đối với biểu diễn bát phân, chúng ta cần đặt cơ số = 8dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]
7. Số lượng số không được thêm vào trục bên trái của số kết quả
Thí dụ
oct[number]3
đầu ra
oct[number]4
Biểu diễn bát phân của các giá trị dữ liệu bằng mô-đun Pandas
Mô-đun Python Pandas được sử dụng để đóng khung các phần tử ở dạng DataFrames và hoạt động trên các bộ dữ liệu. Python
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]8 có thể được sử dụng để biểu diễn các giá trị dữ liệu số nguyên của tập dữ liệu sang định dạng Bát phân của chúng
cú pháp
oct[number]6
Thí dụ
oct[number]7
đầu ra
dec_num = 2 print["Octal representation of decimal number:", oct[dec_num]] bin_num = 0b10 print["Octal representation of binary number:", oct[bin_num]] hex_num = 0x17 print["Octal representation of decimal number:", oct[hex_num]]0
Phần kết luận
Như vậy, trong bài viết này, chúng ta đã hiểu cách biểu diễn các giá trị số nguyên sang định dạng Bát phân của chúng bằng cách sử dụng hàm oct[] trong Python