python头歌数制转换答案
时间: 2023-12-12 10:35:24 浏览: 315
以下是Python中常见数制转换的方法:
1. 十进制转二进制、八进制、十六进制
```python
num = 10
print(bin(num)) # 输出:0b1010
print(oct(num)) # 输出:0o12
print(hex(num)) # 输出:0xa
```
2. 二进制、八进制、十六进制转十进制
```python
num1 = '1010'
num2 = '12'
num3 = 'a'
print(int(num1, 2)) # 输出:10
print(int(num2, 8)) # 输出:10
print(int(num3, 16)) # 输出:10
```
3. 十进制转任意进制
```python
num = 10
base = 2
result = ''
while num:
result = str(num % base) + result
num //= base
print(result) # 输出:1010
```
4. 任意进制转十进制
```python
num = '1010'
base = 2
result = 0
for i in range(len(num)):
result += int(num[i]) * (base ** (len(num) - i - 1))
print(result) # 输出:10
```
关于华氏度与摄氏度的转换,可以使用以下代码实现:
```python
F = 95.2345
C = round((F - 32) * 5 / 9,2)
print("华氏度:{:.2f},摄氏度:{:.2f}".format(F, C)) # 输出:华氏度:95.23,摄氏度:35.13
```
阅读全文