Python数据转换与运算

整形 int

增删改

增
  a = 1
改
  a = 2
删
  del a

转换

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}

运算

float
  a = 1   b = 2.1   print(a+b)   print(a-b)   print(a*b)   print(a/b) a/0 # ZeroDivisionError: division by zero str
  a = 1
  b = '2'
  print(a + b) # TypeError: unsupported operand type(s) for +: 'int' and 'str'
  b = 'addsad'
  print(a + b) # TypeError: unsupported operand type(s) for +: 'int' and 'str'
  print(a - b) # TypeError: unsupported operand type(s) for -: 'int' and 'str'
  print(a * b) # addsad
  print(a / b) # TypeError: unsupported operand type(s) for /: 'int' and 'str'
list   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' dict   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' tuple   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' set   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

增删改

增
    a = 'dasd'
改
    a = 'dasdasd'
删
    del a

转换

a = '123'
int
  print(
int(a)) # 123
list
  print(list(a)) # [
'1','2','3']
  [a] # ['123'] dict
  print(dict(a)) # ValueError: dictionary update sequence element #
0 has length 1; 2 is required   res = {}  
  res.setdefault(a) # {'123':None}
tuple
  print(tuple(a)) # (
'1', '2', '3')
  (a,) # ('123',) set
  print(
set(a)) # {'1', '2', '3'}
  {a} # {'123'}
a = 'afasfa'
int
  print(int(a)) # ValueError: invalid literal for int() with base 10: 'afasfa'
list
  print(list(a)) # ['a', 'f', 'a', 's', 'f', 'a']
  [a] # ['afasfa']
dict
  print(dict(a)) # ValueError: dictionary update sequence element #0 has length 1; 2 is required
  res = {}
  res.setdefault(a) # {'afasfa':None}
tuple
  print(tuple(a)) # ('a', 'f', 'a', 's', 'f', 'a')
  (a) # ('afasfa')
set
  print(set(a)) # {'a', 's', 'f'} 集合去重
  {a} # {'afasfa'}

运算

int
   a = 'afasfa'
  b = 2
  print(a + b)  # TypeError: can only concatenate str (not "int") to str
  print(a - b)  # TypeError: unsupported operand type(s) for -: 'str' and 'int'
  print(a * b)  # afasfaafasfa
  print(a / b)  # TypeError: unsupported operand type(s) for /: 'str' and 'int'
str
  a = '2'
  b = 'dada'
  print(a + b)  # 2dada
  print(a - b)  # TypeError: unsupported operand type(s) for -: 'str' and 'str'
  print(a * b)  # TypeError: can't multiply sequence by non-int of type 'str'
  print(a / b)  # TypeError: unsupported operand type(s) for /: 'str' and 'str'
list
  a
= 'asdad'   b = ['das','das','dasd']   print(a + b) # TypeError: can only concatenate str (not "list") to str   print(a - b) # TypeError: unsupported operand type(s) for -: 'str' and 'list'   print(a * b) # TypeError: can't multiply sequence by non-int of type 'list'   print(a / b) # TypeError: unsupported operand type(s) for /: 'str' and 'list' dict
  a
= 'dasda'   b = {'sd':'dasd','dsad':'dad'}   print(a + b) # TypeError: can only concatenate str (not "dict") to str   print(a - b) # TypeError: unsupported operand type(s) for -: 'str' and 'dict'   print(a * b) # TypeError: can't multiply sequence by non-int of type 'dict'   print(a / b) # TypeError: unsupported operand type(s) for /: 'str' and 'dict' tuple
  a
= 'dasda'   b = (1,'dasd','dasda')   print(a + b) # TypeError: can only concatenate str (not "tuple") to str   print(a - b) # TypeError: unsupported operand type(s) for -: 'str' and 'tuple'   print(a * b) # TypeError: can't multiply sequence by non-int of type 'tuple'   print(a / b) # TypeError: unsupported operand type(s) for /: 'str' and 'tuple' set
  a
= 'dasd'   b = {'asda','dad','dada'}   print(a + b) # TypeError: can only concatenate str (not "set") to str   print(a - b) # TypeError: unsupported operand type(s) for -: 'str' and 'set'   print(a * b) # TypeError: can't multiply sequence by non-int of type 'set'   print(a / b) # TypeError: unsupported operand type(s) for /: 'str' and 'set'

 

列表list

 增删改

增
    a = ['1','2','3']
a = a.append('4') # ['1','2','3','4']
a = a.insert(3,'4')
# ['1','2','3','4']
a = a.extend(['4']) # ['1','2','3','4']
 
   del
a
   a.remove('1') # ['2','3']
   a.pop(1) # ['1','3']
   a.clear() # []

  a[1] = '3' # ['1','3','3']

转换

a = ['1','2','3']
int   
int(a) # TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' str
  str(a) # ['1', '2', '3'] dict
  dict(a) # ValueError: dictionary update sequence element #0 has length 1; 2 is required tuple
  tuple(a) # ('1', '2', '3') set
  set(a) # {'3', '2', '1'}

运算

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

str
    a = ['1','2','3']
    b = 'dasd'
    print(a + b)  # TypeError: can only concatenate list (not "str") to list
    print(a - b)  # TypeError: unsupported operand type(s) for -: 'list' and 'str'
    print(a * b)  # TypeError: can't multiply sequence by non-int of type 'str'
    print(a / b)  # TypeError: unsupported operand type(s) for /: 'list' and 'str'

list
    a = ['1','2','3']
    b = ['4','5','6']
    print(a + b)  # ['1', '2', '3', '4', '5', '6']
    print(a - b)  # TypeError: unsupported operand type(s) for -: 'list' and 'list'
    print(a * b)  # TypeError: can't multiply sequence by non-int of type 'list'
    print(a / b)  # TypeError: unsupported operand type(s) for /: 'list' and 'list'

dict
    a = ['1','2','3']
    b = {'a':4,'c':5}
    print(a + b)  # TypeError: can only concatenate list (not "dict") to list
    print(a - b)  # TypeError: unsupported operand type(s) for -: 'list' and 'dict'
    print(a * b)  # TypeError: can't multiply sequence by non-int of type 'dict'
    print(a / b)  # TypeError: unsupported operand type(s) for /: 'list' and 'dict'

tuple
    a = ['1','2','3']
    b = ('4','5','6')
    print(a + b)  # TypeError: can only concatenate list (not "tuple") to list
    print(a - b)  # TypeError: unsupported operand type(s) for -: 'list' and 'tuple'
    print(a * b)  # TypeError: can't multiply sequence by non-int of type 'tuple'
    print(a / b)  # TypeError: unsupported operand type(s) for /: 'list' and 'tuple'

set
    a = ['1','2','3']
    b = {'4','5','6'}
    print(a + b)  # TypeError: can only concatenate list (not "set") to list
    print(a - b)  # TypeError: unsupported operand type(s) for -: 'list' and 'set'
    print(a * b)  # TypeError: can't multiply sequence by non-int of type 'set'
    print(a / b)  # TypeError: unsupported operand type(s) for /: 'list' and 'set'

 

字典dict

 

posted @ 2019-06-01 00:09  yyfgrd  阅读(768)  评论(0)    收藏  举报