数据类型

0. 概念


  • 理解:对程序处理的数据,进行的 分类
  • 举例
    # 处理的数据是数值类型
    print(1)
    
    # 处理得数据是字符串类型
    print("abc")
    
  • Python 是属于 强类型动态类型 的语言

1. 作用


  • 区分存储空间:不同的数据类型,存储的 空间大小 可能不同
    a = 100
    b = True
    c = 10000000000000000000000000000000
    d = 1.1
    e = ""
    f = []
    g = ()
    h = {}
    i = set([])
    j = complex(1, 2)
    j2 = 1 + 2j
    print(" %s size is %d " % (type(a), sys.getsizeof(a)))
    print(" %s size is %d " % (type(b), sys.getsizeof(b)))
    print(" %s size is %d " % (type(c), sys.getsizeof(c)))
    print(" %s size is %d " % (type(d), sys.getsizeof(d)))
    print(" %s size is %d " % (type(e), sys.getsizeof(e)))
    print(" %s size is %d " % (type(f), sys.getsizeof(f)))
    print(" %s size is %d " % (type(g), sys.getsizeof(g)))
    print(" %s size is %d " % (type(h), sys.getsizeof(h)))
    print(" %s size is %d " % (type(i), sys.getsizeof(i)))
    print(" %s size is %d " % (type(j), sys.getsizeof(j)))
    print(" %s size is %d " % (type(j2), sys.getsizeof(j2)))
    
  • 根据不同数据类型的 特性,做不同的 数据处理
    num = 6 + 6
    print(num)
    
    appendStr = "6" + "6"
    print(appendStr)
    

2. 分类


  • Numbers(数值类型)
    # int
    num = 100
    print(type(num), num)
    
    # Python2.x: 有 long 类型, Python3.x: 无 long 类型
    long_num = 100L
    print type(long_num), long_num
    
    # float
    float_num = 11.11
    print(type(float_num), float_num)
    
    # complex
    complex_num = complex(1, 2)
    print(type(complex_num), complex_num)
    
  • Bool(布尔类型)
    is_open = True
    print(type(is_open), is_open)
    
  • String(字符串)
    name = "秦子阳"
    print(type(name), name)
    
  • List(列表)
    score_list = [100, 99, 88, 66, 59]
    print(type(score_list), score_list)
    
  • Tuple(元组)
    name_tuple = ("wxx", "qtt", "xdy", "xfq", "lm", "lb")
    print(type(name_tuple), name_tuple)
    
  • Dictory(字典)
    person_dict = {"name": "wxx", "age": 18, "address": "杭州市"}
    print(type(person_dict), person_dict)
    
  • Set(集合)
    num_set = {1, 3, 5, 7, 9}
    print(type(num_set), num_set)
    
  • NoneType(空类型)
    score_list = [100, 99, 88, 66, 59]
    result = score_list.clear()
    print(type(result), result)
    

3. 查看


  • 方法:可以使用 type() 查看数据类型
    num = 100
    print(type(num))
    

4. 转换


  • 强制类型转换:将一个数据 转换指定 的类型,方便处理
    # 格式:类型(需要转换的值)
    numStr = "66"
    a = int(numStr) + 6
    print(a)
    
  • 隐式类型转换:较低精度 操作数被 自动转换较高精度 的数据类型
    a = 20
    b = 32.0
    c = a + b
    print(type(c), c)
    
  • 转换图
    类型转换.png

你可能感兴趣的:(数据类型)