Python数据转换与运算

整形 int

a = 1
str
  str(a) # '1' list
  list(a) # TypeError: 'int' object is not iterable   [a] # [1] dict
  dict(a) # TypeError: 'int' object is not iterable   res = {}   res.setdefault(a) # {1:None} tuple
  tuple(a) # TypeError: 'int' object is not iterable   (a,) # (1,) set
  set(a) # TypeError: 'int' object is not iterable   {a} # {1}
a = 1
b = 2.1
print(a+b)
print(a-b)
print(a*b)
print(a/b)  a/0  # ZeroDivisionError: division by zero


a = 2
b = [1,2,3]
print(a + b)  # TypeError: unsupported operand type(s) for +: 'int' and 'list'
print(a * b)  # [1, 2, 3, 1, 2, 3]
print(a / b)  # TypeError: unsupported operand type(s) for /: 'int' and 'list'
print(a - b)  # TypeError: unsupported operand type(s) for -: 'int' and 'list'


a = 2
b = {'c': 1, 'd': 2}
print(a + b)  # TypeError: unsupported operand type(s) for +: 'int' and 'dict'
print(a - b)  # TypeError: unsupported operand type(s) for -: 'int' and 'dict'
print(a * b)  # TypeError: unsupported operand type(s) for *: 'int' and 'dict'
print(a / b)  # TypeError: unsupported operand type(s) for /: 'int' and 'dict'


a = 2
b = (1,2,3)
print(a + b)  # TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
print(a - b)  # TypeError: unsupported operand type(s) for -: 'int' and 'tuple'
print(a * b)  # (1, 2, 3, 1, 2, 3)
print(a / b)  # TypeError: unsupported operand type(s) for /: 'int' and 'tuple'


a = 2
b = {1,2,3}
print(a + b)  # TypeError: unsupported operand type(s) for +: 'int' and 'set'
print(a - b)  # TypeError: unsupported operand type(s) for -: 'int' and 'set'
print(a * b)  # TypeError: unsupported operand type(s) for *: 'int' and 'set'
print(a / b)  # TypeError: unsupported operand type(s) for /: 'int' and 'set'

字符型 string

猜你喜欢

转载自www.cnblogs.com/yuyafeng/p/10958135.html