>>> #第一种
>>> a = '1 2 3'.split()
>>> a
['1', '2', '3']
>>> b = tuple(a)
>>> b
('1', '2', '3')
>>> #此时 b 是一个元组,而元组不能被化为 int 类型,所以就会报错
>>> int(b)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
int(b)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
>>> #第二种
>>> a = '1 2 3'.split()
>>> a
['1', '2', '3']
>>> #eval 函数的参数应该是一个字符串,比如像下面这样
>>> eval('1')
1
>>> eval('[1, 2, 3]')
[1, 2, 3]
>>> eval('print("hello")')
hello
>>> #eval 函数会执行字符串里面的代码,如果字符串里面是一个对象,就会返回这个对象
>>> #但是 eval 函数不能输入列表
>>> eval([1, 2, 3])
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
eval([1, 2, 3])
TypeError: eval() arg 1 must be a string, bytes or code object
>>> eval(a)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
eval(a)
TypeError: eval() arg 1 must be a string, bytes or code object
>>>
|