python 基本数据类型占用内存空间大小的实例

yipeiwu_com6年前Python基础

python中基本数据类型和其他的语言占用的内存空间大小有很大差别

import sys
a = 100
b = True
c = 100L
d = 1.1
e =""
f = []
g =()
h = {}
i = set([])

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))

 <type 'int'> size is 12 
 <type 'bool'> size is 12 
 <type 'long'> size is 14 
 <type 'float'> size is 16 
 <type 'str'> size is 21 
 <type 'list'> size is 36 
 <type 'tuple'> size is 28 
 <type 'dict'> size is 140 
 <type 'set'> size is 116 

以上这篇python 基本数据类型占用内存空间大小的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3新特性函数注释Function Annotations用法分析

本文分析了python3新特性函数注释Function Annotations用法。分享给大家供大家参考,具体如下: Python 3.X新增加了一个特性(Feature),叫作函数注释...

python lxml中etree的简单应用

python lxml中etree的简单应用

我一般都是通过xpath解析DOM树的时候会使用lxml的etree,可以很方便的从html源码中得到自己想要的内容。 这里主要介绍一下我常用到的两个方法,分别是etree.HTML()...

python字符串中匹配数字的正则表达式

Python 正则表达式简介 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。 Python 自1.5版本起增加了re 模块,它提供 Perl 风格...

对Python3 序列解包详解

Python 中有很多很实用的语法糖,这些语法糖可以帮助我们简化代码、更易理解等优点,接下里再看一个 Python3 中特别实用的语法序列解包(序列解包是 Python 3.0 之后才有...

Python中defaultdict与lambda表达式用法实例小结

本文实例讲述了Python中defaultdict与lambda表达式用法。分享给大家供大家参考,具体如下: 从教程中看到defaultdict是一个类,在一台装有Python2.7.6...