参考资料:

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. SQL多表查询:内连接、外连接(左连接、右连接)、全连接、交叉连接

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAADCCAIAAADrUpiXAAAGYklEQVR4nO3dQXqjuAJFYa1LC9J6tB

  2. SQL Server :DBLINK创建及使用

    Exec sp_droplinkedsrvlogin bvtwfld12,Null --若存在先刪除Exec sp_dropserver bvtwfld12EXEC sp_addlinkedserve ...

  3. SQL加、查、改、删、函数

    SQL加.查.改.删.函数   USE lianxiGOcreate table student1(code int not null ,name varchar(20),sex char(4),ci ...

  4. Win7下安装IEWebControls.msi

    编写人:CC阿爸 2014-2-22 IEWebControls.msi是发布在.net 1.1时代.微软为弥布.net控件的不足而发布一组控件.很多程序猿都喜欢用到他. 方法一: 首先保证IIS7安 ...

  5. set_exception_handler 和 set_error_handler 函数

    定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...

  6. CentOS7.0安装与配置Tomcat-7

    解决权限不够 #chmod a+x filename 安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:ht ...

  7. C# 平时碰见的问题【3】

    今天发现一个问题纳闷了半个小时, 需求是处理project文件里边的数据内容,其中需要判断任务名称不存在重复; 在测试的时候弄了两行一样的任务,如预想: 任务[xxx]重复 然后删掉重复的任务行,继续 ...

  8. C# 平时碰见的问题【2】

    问题1 修改命名空间后 .ashx 类型创建失败 [情景] 在调整前后台项目结构的时候,修改了默认命名空间(XXX.Admin 修改成XXX.Web),结果调试的时候发现XXX.Admin.Ajax. ...

  9. Maven学习随记

    慕课网视频教程:http://www.imooc.com/learn/443 ====Maven是什么 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建.报告和文档的软件 ...

  10. python 从private key pem文件中加载public key

    import rsa import logging from Crypto.PublicKey import RSA class RsaUtil: def __init__(self, pem_fil ...