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

yipeiwu_com5年前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设计】。

相关文章

基于python代码实现简易滤除数字的方法

基于python代码实现简易滤除数字的方法

如果想从一个含有数字,汉字,字母的列表中滤除仅含有数字的字符,当然可以采取正则表达式来完成,但是有点太麻烦了,因此可以采用一个比较巧妙的方式: 1、正则表达式解决 import re...

pytorch自定义初始化权重的方法

在常见的pytorch代码中,我们见到的初始化方式都是调用init类对每层所有参数进行初始化。但是,有时我们有些特殊需求,比如用某一层的权重取优化其它层,或者手动指定某些权重的初始值。...

Python操作MySQL数据库的三种方法总结

Python操作MySQL数据库的三种方法总结

1. MySQLdb 的使用 (1) 什么是MySQLdb? MySQLdb 是用于 Python 连接 MySQL 数据库的接口,它实现了 Python 数据库 API 规范 V2.0...

python 控制语句

1比如python提倡简单实用的思想,它就没有switch语句,如果要实现switch语句的效果 的话可以通过2个方法来写把 (1)通过if elif 语句来实现 if 条件: … el...

一张图带我们入门Python基础教程

一张图带我们入门Python基础教程

啄木鸟社区上原始翻译后绘制的,最早这个图是出现在(链接已失效) “这个图太棒了,有编程基础的人一下子就了解 Python 的用法了。真正的 30 分钟上手。”Buzz by http:/...