使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。

先看Python官方文档中对这几个内置函数的描述:

bin(x)
Convert an integer number to a binary
string. The result is a valid Python expression. If x is not a Python
int object, it has to define an __index__() method that returns an
integer.

oct(x)
Convert an integer number to an octal
string. The result is a valid Python expression. If x is not a Python
int object, it has to define an __index__() method that returns an
integer.

int([number | string[, base]])
Convert a number
or string to an integer. If no arguments are given, return 0. If a
number is given, return number.__int__(). Conversion of floating point
numbers to integers truncates towards zero. A string must be a
base-radix integer literal optionally preceded by ‘+’ or ‘-‘ (with no
space in between) and optionally surrounded by whitespace. A base-n
literal consists of the digits 0 to n-1, with ‘a’ to ‘z’ (or ‘A’ to ‘Z’)
having values 10 to 35. The default base is 10. The allowed values are 0
and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with
0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means
to interpret exactly as a code literal, so that the actual base is 2, 8,
10, or 16, and so that int('010', 0) is not legal, while int('010') is,
as well as int('010', 8).

hex(x)
Convert an integer number to a hexadecimal
string. The result is a valid Python expression. If x is not a Python
int object, it has to define an __index__() method that returns an
integer.

  2进制 8进制 10进制 16进制
2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16))
8进制 oct(int(x, 2)) - oct(int(x, 10)) oct(int(x, 16))
10进制 int(x, 2) int(x, 8) - int(x, 16)
16进制 hex(int(x, 2)) hex(int(x, 8)) hex(int(x, 10)) -

bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。

Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)的更多相关文章

  1. Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

    ↓ 2进制 8进制 10进制 16进制 2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16)) 8进制 oct(int(x, 2)) - oct(in ...

  2. Python内置函数进制转换的用法

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  3. python 内置函数 进制转换

    4.内置函数 自定义函数 内置函数 len Open id() type() range() 输入输出 print() input() 强制转换 int() float() list() tuple( ...

  4. python 小兵内置函数进制转换

    Python内置函数进制转换的用法 使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Conve ...

  5. python内置的进制转换方法

    python进制转换方法总结表: ↓ 2进制 8进制 10进制 16进制 2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16)) 8进制 oct(in ...

  6. python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理

    python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...

  7. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  8. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  9. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

随机推荐

  1. asp中出现“错误 '80040e14' FROM 子句语法错误”原因

    当你的sql语句中出现 “错误 '80040e14' FROM 子句语法错误.”错误时,请注意了,那有可能是你的表名的命名不规范造成的,比如你的表名是“user”那么这杨的表名是不行的,那么在sql语 ...

  2. 006-guava 集合-集合工具类-集合扩展工具类

    一.概述 需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 二.使用 2.1.ForwardingList装 ...

  3. linux非root用户安装ncurses-devel依赖

    很明显,如果我们通过yum或rpm下载安装,始终无法绕开root用户,除非我们不用yum或rpm.嗯,我们直接用源码安装.下载源码包,到http://ftp.gnu.org/gnu/ncurses/我 ...

  4. SVL-VI SLAM

    3.4. Mappoints management and key frame process如果在步骤3.3中成功跟踪地图点,则缓存地图点以在下一帧中优先化.当完成当前帧的跟踪时,应该为下一帧更新帧 ...

  5. java获取全部子类或接口的全部实现

    在JAVA中,获取一个类的全部父类是比较简单的,只需要通过反射(Class的getSuperclass()方法)即可.然而,如果想获得一个类的所有子类,或者获得实现某一个接口的所有实现类,相对比较麻烦 ...

  6. Linux记录-Shell自动化部署批量建立用户和批量SSH配置(转载)

    if [ ! $# -eq 2 ] ; then echo "请输入用户名和密码以空格分开!" exit else name="$1" passwd=" ...

  7. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  8. Java Sound : audio inputstream from pcm amplitude array

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-making-audio-input-stream.html In ...

  9. LeetCode_344. Reverse String

    344. Reverse String Easy Write a function that reverses a string. The input string is given as an ar ...

  10. k8s调度器之亲和性和反亲和性/节点选择器

    容器在节点(物理机)上是如何部署的 是由调度器scheduler进行调度的 调度策略 随机 通过节点选择器选择某些节点 通过节点亲和性和pod的亲和性及反亲和性实现更细粒度的控制 参考 https:/ ...