Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)
使用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 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)的更多相关文章
- 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 ...
- Python内置函数进制转换的用法
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- python 内置函数 进制转换
4.内置函数 自定义函数 内置函数 len Open id() type() range() 输入输出 print() input() 强制转换 int() float() list() tuple( ...
- python 小兵内置函数进制转换
Python内置函数进制转换的用法 使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Conve ...
- python内置的进制转换方法
python进制转换方法总结表: ↓ 2进制 8进制 10进制 16进制 2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16)) 8进制 oct(in ...
- python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
随机推荐
- ISO/IEC 9899:2011 条款6.5.10——按位与操作符
6.5.10 按位与操作符 语法 1.AND-expression: equality-expression AND-expression equality-expression 约束 2.这些 ...
- 1. Tomcat之startup.sh
#判断操作系统os400=falsecase "`uname`" inOS400*) os400=true;;esac # 取脚本名称PRG="$0" # 判断 ...
- eclipse默认指向WebContent目录修改为webRoot 设置说明【也适用于Eclipse启动MyEclipse项目】
转: eclipse默认指向WebContent目录修改为webRoot 设置说明 2014-07-02 17:42:58 落叶上的秋 阅读数 8618更多 分类专栏: Eclipse 问题 l ...
- pls_integer类型
转: pls_integer类型 2011-08-22 16:49:06 heqiyu34 阅读数 13413更多 分类专栏: Oracle 今天在看一个触发器代码的时候碰到了一个pls_inte ...
- 【Dart学习】--Dart之数组(List)的相关方法总结
一,初始化List 非固定长度list var testList = List(); print(testList.length);//输出0 固定长度List var testList2 = Lis ...
- nginx通过robots.txt禁止所有蜘蛛访问(禁止搜索引擎收录)
在server {} 块中添加下面的配置 location =/robots.txt { default_type text/html; add_header Content-Type "t ...
- OpenLDAP配置坎坷路
https://segmentfault.com/a/1190000014683418 轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDA ...
- GNU C之__attribute__
__attribute__可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型属性(Type Attribute) __attribute_ ...
- axios发了两次请求
一.问题描述 用axios发post请求,却出现了options请求和post请求,options请求哪里来的? 二.问题分析 1.先温习一下跨域的知识 2.axios默认类型是Content-Typ ...
- 【编程基础】CppLint工具使用过程
前言 coding最好要形成一定的编程风格,一般常用的开源风格有google code style,可以使用cpplint工具检查是否符合该编程风格. 目录 1. linux系统使用过程: 2. wi ...