python数据转换

  • 数据类型转换
  • int()

将其他有效的数据转为整数
取整
从字符串中解析整数

>>> int(3.14) # 将小数进行取整操作
3
>>> int("123") # 将数字字符串进行解析(默认十进制),解析出一个整数
123
>>> int("123abc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '123abc'
>>> int("AD", 16) # 将数字字符串进行十六进制解析,结果都是十进制
173
# 10*16^1 + 13*16^0 = 173
>>> int("91A", 12)
1318
>>> 10 * 12 ** 0 + 1 * 12 ** 1 + 9 * 12 ** 2
1318


>>> int("A1F", 13)
Traceback (most recent call last):

  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 13: 'A1F'


>>> int("91a", 100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: int() base must be >= 2 and <= 36, or 0

 base 进制基数 = [2, 36]
>>> int("98*!",12) # 出现特殊符号
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 12: '98*!'
>>> int("10010101") # 注意坑 二进制串特不一定是二进制数字
10010101
>>> int("10010101", 2)
149

  • float():

将其他的数据转为小数 

>>> float(3)
3.0
>>> float(3.14)
3.14
>>> float("3.14")
3.14
>>> float("3.14", 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: float expected at most 1 argument, got 2

  • str():
  • str()函数将对象对象转换为可阅读的样式。

将其他数据转字符串
>>> str(123)
'123'
>>> str(3.14)
'3.14'
>>> str(print)
'<built-in function print>'

  • bool():

bool()函数将参数转换为bool类型,如果没有给参数则返回False 

将其他数据转布尔类型
# 对于数值类型的话 非0全是True 0就是False

>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool(3.14)
True
>>> bool(-3.14)
True 

>>> bool(0.0)
False
# 对字符串 空串为False 非空为True
>>> bool("abc")
True
>>> bool("") #空串
False

list([iterable])

list()函数根据输入的可迭代的参数,创建一个新的列表。

>>> list()
[]
>>> list('1234')
['1', '2', '3', '4']


tuple([iterable])

tuple()函数根据输入的可迭代的参数,创建一个新的元组。

>>> tuple()
[]
>>> tuple('1234')
['1', '2', '3', '4']

dict([iterable])

dict()函数根据传入的参数,创建一个新的字典。

>>> dict()
{}
>>> dict(a=1,b=2,c=3)
{'a': 1, 'c': 3, 'b': 2}
>>> dict(zip(['a','b','c'],['1','2','c']))
{'a': '1', 'c': 'c', 'b': '2'}

set([iterable])


set()函数根据参数创建一个新的集合

>>> set()
set([])
>>> set('1234')
set(['1', '3', '2', '4'])

frozenset([iterable])


frozenset()函数返回一个冻结的set集合,其不能添加和删除元素

>>> frozenset()
frozenset([])
>>> frozenset(range(10))
frozenset([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 

  • 进制转换 

>>> bin(123) # 转二进制字符串
'0b1111011'
>>> oct(123) # 转八进制字符串
'0o173'
>>> hex(123) # 转十六进制字符串
'0x7b'
>>> bin("123") # 参数必须是整数
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer


>>> bin(3.14)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

  • 字符与ASCII码转换 

a~z  A~Z 0~9 他们在ASCII中的编号都是连续的

ord():获取字符对应的ASCII码编号

>>> ord('a')
97
>>> ord('A')
65
>>> ord('0')
48

chr():根据给定的ASCII码编号获取对应的字符 

chr(i)
char()函数根据输入的数字返回一个字符,数字的范围为range(0,256)

>>> chr(65)
'A'

>>> chr(98)
'b'
>>> chr(57)
'9'

A = 10  B = 11 ...... F = 15
13 - D
chr(ord('A') + 13 - 10)

常见的数学计算

>>> abs(3.14) # 取绝对值
3.14
>>> abs(-3.14)
3.14


>>> pow(2, 4) # 求a 的 b 次幂
16
>>> pow(2.0, 4)
16.0
>>> pow(16, 0.5)
4.0


>>> max(1, 2, 3, 4) # 求最值问题
4
>>> min(1,2,3,4)
1

round(x[, n])
返回浮点数x的四舍五入的值,其中n值,表示小数点后的位数。 

>>> round(2)
2.0
>>> round(2.567)
3.0
>>> round(2.567, 2)
2.57


>>> round(3.14) #四舍五入
3
>>> round(3.51)
4

 

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值