参考资料:

http://sebug.net/paper/books/dive-into-python3/native-datatypes.html

http://blog.csdn.net/hazir/article/details/10159709

1、Boolean【布尔型】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' # Python中的布尔值为True、False,首字母大写
def test_boolean(): # bool 值为False的情况
formater = "%r,%r" # %r —— 万能的格式符,它会将后面的参数原样打印输出,带有类型信息 n = 1
print formater % (n, bool(''))
n += 1 print formater % (n, bool(None))
n += 1 print formater % (n, bool(0))
n += 1 print formater % (n, bool(0L))
n += 1 print formater % (n, bool(0.0))
n += 1 print formater % (n, bool(0j))
n += 1 print formater % (n, bool({}))
n += 1 print repr(False) # bool值为True
print "++++++++++++++++++++++++++++++++++"
print formater %(n,bool("hello"))
n+=1 print formater %(n,bool(1)) test_boolean()

注:

1、上述代码指出了,为False的情况,0、None、’’{} 都为False,反之则为True

2、%r   为Python的原样输出符号,以上输出结果为:

2、Number【数值型】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' def test_integer():
formater = "%r,%r"
n = 1 print formater % (n, 123)
n += 1 print formater % (n, 123L)
n += 1 print formater % (n, 12.34)
n += 1 cplx = complex(1, 2)
print formater % (n, cplx)
print "%r,%r,%r" % (5, cplx.real, cplx.imag) test_integer()

注:

以上总结了数值型包括整数、浮点数、复数

输出结果:

3、String【字符串型】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' # 基本类型——字符串 """可以使用单引号(')或者双引号(")表示字符串"""
def string_op():
str1 = "Hello World!"
str2 = "David's Book"
str3 = '"David" KO!'
str4 = "TOTAL"
str5 = '' print str1
print str2
print str3
print str4
print str5 if str1.endswith("!"):
print "endwith:%s" % "!" if str2.startswith("vid", 2, 5):
print "startswith:%s" % "vid" if str4.isupper():
print "Uppercase" if str5.isdigit():
print "Digit" if str5.isalnum():
print "alnum" print str5.find("") # 使用format函数格式化字符串
def string_format():
print "hello {}".format("world!")
print "hello {} {}".format("world", "!")
print "hello {}".format(123)
print "hello {}".format(12.43) string_op()
string_format()

注:

以上总结了字符串的一个操作函数,同时指出了格式化字符串的使用

输出结果:

4、Bytes【字节】

下次补充

pass

5、List【列表】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' def test_list():
# 构造列表——构造函数
mylist = list('a') # 只包含一个参数
print (mylist) value = (1, 2, 3, 4)
mylist = list(value)
print (mylist) # 构造列表——使用方括号
mylist_1 = ['a', 'b']
mylist_2 = [1, 2, 3]
mylist_1.append(mylist_2)
print "condition_1:",
print (mylist_1) mylist_1.extend(mylist_2)
mylist_1.extend(mylist_2) # 注意extend方法和append方法区别
print "condition_2:",
print (mylist_1) mylist_1.remove(mylist_2)
print "condition_3:",
print (mylist_1) mylist_3 = [xobj for xobj in mylist_1]
print "condition_4:",
print (mylist_3) # 以下是list的一些方法使用
mylist_1.reverse()
print (mylist_1) mylist_1.insert(1, 1233)
print (mylist_1) print mylist_1.index(1)
mylist_1.pop()
print (mylist_1) test_list()

注:

以上总结了list的基本用法,输出结果:

6、Tuples【元组】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' #元组类似于列表,用逗号(,)分隔存储数据,与列表不同的是元组是不可变类型,列表
#可以任你插入或改变,而元组不行,元组适合于数据固定且不需改变的情形,从内存角度
#来看,使用元组有一个好处是,可以明确的知道需要分配多少内存给元组
def test_tuples():
my_tuple=(1,2,3.1,2)
my_list=[1,2,3,3.3]
my_dict=dict(a=1,b=2,c=3) #测试数据类型
print type(my_tuple)
print type(my_list)
print type(my_dict) #构造tuple
my_tuple_2=tuple([1,2,3,2])
print type(my_tuple_2)
print my_tuple
print my_tuple_2 #tuple 特殊说明:
# 可以使用空小括号或者使用不传参数的构造函数来创建一个空的元组,
# 但创建仅有一个元素的元组时,还需要一个额外的逗号,因为没有这个逗号,
# python会忽略小括号,仅仅只看到里面的值
single_tuple=(1)
single_tuple_2=(1,)
print type(single_tuple)
print type(single_tuple_2) def test_pass():
pass #pass 是python中的空语句 test_tuples()

注:

tuple元组是不可变类型,注意单个元素时逗号的使用

7、Set【集合】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' def test_set():
a={12,1,2,3}
b={1,4,5,2} print "set(a):%r" %a
print "set(b):%r" %b print "set(a.union(b)):%r" %a.union(b)
print "set(a.difference(b)):%r" %a.difference(b)
print "set(a.intersection(b)):%r" %a.intersection(b) a=list(a)
print a test_set()

注:

集合的基本操作union、difference、intersection【并、差、集】

输出结果:

8、Dictionaries【字典】

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
''' def test_dictionaries(): # 字典构造
# 构造方法一
phonenumber = {
"":"luosongchao",
"":"haiktkong"
} # 构造方法二
diction = dict((['a', 1], ['b', 2], ['c', 3]))
for key, value in diction.iteritems():
print (key, value),
print # 构造方法三
diction = dict(a=1, b=2, c=3)
for key, value in diction.iteritems():
print (key, value),
print # 构造方法四,包含相同值,默认值为None
edic = {}.fromkeys(("x", "y"), -1)
for key, value in edic.iteritems():
print (key, value),
print epdic = {}.fromkeys(("a", "b"))
print epdic # 字典迭代方法:iteritems、iterkeys、itervalues
# iteritems使用
for key, value in phonenumber.iteritems():
print (key, value),
print # iterkeys使用
for key in phonenumber.iterkeys():
print key,
print # itervalues使用
for value in phonenumber.itervalues():
print value,
print # 查找字典
if diction.has_key('a'):
print "has_key" # 更新字典
print diction
diction['a'] += 1
print diction
diction['x'] = 12
print diction # 删除字典元素
diction.pop('a')
print diction
del diction['x']
print diction test_dictionaries()

注:

以上给出了dict的四种构造方法,字典的迭代方法,查找字典,删除字典元素,更新字典等

输出结果:

补充部分:

# coding:utf-8
'''
Created on 2014-4-29 @author: Administrator
'''
# 判断类型
def test_type():
int_type = 1;
long_type = 12L
float_type = 12.21
list_type = [1, 2, 3, 3, 1]
dict_type = {"a":1, "b":2, "c":3}
set_type = {1, 2, 3, 3}
tuple_type = (1, 2, 3, 1) print "int_type: %r" % type(int_type)
print "long_type: %r" % type(long_type)
print "float_type: %r" % type(float_type)
print "list_type: %r" % type(list_type)
print "dict_type: %r" % type(dict_type)
print "set_type: %r" % type(set_type)
print "tuple_type: %r" % type(tuple_type) print "++++++++++++++++++++++++++++++++++++++++" print "isinstance(int):%r" % (isinstance(int_type, int))
print "isinstance(long):%r" % (isinstance(long_type, long))
print "isinstance(float):%r" % (isinstance(float_type, float))
print "isinstance(list):%r" % (isinstance(list_type, list))
print "isinstance(dict):%r" % (isinstance(dict_type, dict))
print "isinstance(set):%r" % (isinstance(set_type, set))
print "isinstance(tuple):%r" % (isinstance(tuple_type, tuple)) test_type()

注:

以上给出了,判断类型的方法,type使用和isinstance使用:

【Python】内置数据类型的更多相关文章

  1. Python内置数据类型之Dictionary篇

    1.查看函数XXX的doc string. Python的函数是有属性的,doc string便是函数的属性.所以查看函数XXX的属性的方法是模块名.XXX.__doc__ 2.模块的属性 每个模块都 ...

  2. Python内置数据类型总结

    python的核心数据类型:(很多语言之提供了数字,字符串,文件数据类型,其他形式的数据类型都以标准库的形式表示 也就是用之前需要import ) ,但是python有很多都是内置的,不需要impor ...

  3. python 内置数据类型之数字

    目录: 1.2. 数字 1.2.1. 数字类型 1.2.2. 浮点数 1.2.3. 进制记数 1.2.4. 设置小数精度 1.2.5. 分数 1.2.6. 除法 1.2 数字   1.2.1 数字类型 ...

  4. python内置数据类型-字典和列表的排序 python BIT sort——dict and list

    python中字典按键或键值排序(我转!)   一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...

  5. Python内置数据类型之Tuple篇

    Tuple 是不可变的 list.一旦创建了一个 tuple,就不可以改变它.这个有点像C++中的const修饰的变量.下面这段话摘自Dive Into Python: Tuple 比 list 操作 ...

  6. python 内置数据类型之字符串

    1.3 字符串 字符串本身就是一个有序(从左至右)的字符的集合.是序列这种类型的一种,后面还要学习列表与元组. 在这一节中,需要了解字符串的定义,特殊字符,转义与抑制转义:字符串基本操作.格式化等. ...

  7. Python内置数据类型之List篇

    List的定义: li = ["one" , "two" , "three" , "four"] List是一个有序的集 ...

  8. Python中内置数据类型list,tuple,dict,set的区别和用法

    Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...

  9. python计算非内置数据类型占用内存

    getsizeof的局限 python非内置数据类型的对象无法用sys.getsizeof()获得真实的大小,例: import networkx as nx import sys G = nx.Gr ...

  10. Python的四个内置数据类型list, tuple, dict, set

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

随机推荐

  1. 使用DBCP时发生AbstractMethodError异常

    使用DBCP时发生AbstractMethodError异常,错误描述: Exception in thread "main" java.lang.AbstractMethodEr ...

  2. mysql基本数据类型(mysql学习笔记三)

    Mysql数据类型 小数: 浮点:小数位可以变化 Float单精度默认精度6位左右 Double 双精度默认精度16位左右 支持,控制数值范围 Type(M,D) M表示所有数值位数(不包括小数点和符 ...

  3. Sublime Text2 注册码 汉化 配置lua开发环境

    1.注册  help --> Enter License ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-8556 ...

  4. PHP上传图片如何防止图片木马?

    segmentfault回答: http://segmentfault.com/q/1010000000507750 一. 其实识别图片木马是很困难的,可以在一张正常的图片里加入一句话木马. 但是只要 ...

  5. String类的比较

    //strcmp只能运用于数组的比较 //string类可以用成员函数compare() //即a.compare(b) //例如 "; "; a.compare(b)返回值= & ...

  6. Android SDK中国在线更新镜像服务器 解决GOOGLE更新无法下载 更新失败的问题

    Android Tools Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/I ...

  7. 在linux下安装Mongodb

    1.到官网下载源码:mongodb-linux-x86_64-rhel55-3.2.4.gz 2.安装 创建用户组.用户.目录 [root@hadoop1 ~]# groupadd mongodb [ ...

  8. C语言如何 实现 下雪效果

    题外话  前言 1.本文主要围绕 如何 在 控制台上 下起 一场 只有自己能看见的雪 2.是个简易跨平台的,主要是C语言 3.动画 采用 1s 40帧, 雪花具有 x轴速度和y轴速度 4.比较简单,可 ...

  9. EMVTag系列3《持卡人基本信息数据》

    Ø  9F61    持卡人证件号 L:2–26 R(需求):数据应存在,在读应用数据过程中,终端不检查: (PBOC2.0第五部分中规定)芯片中持卡人姓名 5F20与持卡人姓名扩展9F0B只能使用一 ...

  10. c语言中通过指针将数值赋值到制定内存地址

    1.一种直观的方法 假设现在需要往内存0x12ff7c地址上存入一个整型数0x100.我们怎么才能做到呢? 我们知道可以通过一个指针向其指向的内存地址写入数据,那么这里的内存地址0x12ff7c其本质 ...