参考资料:

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使用:

最新文章

  1. ios 真机调试 could not find Developer Disk Image
  2. vi技巧合集
  3. Nodejs的模块实现
  4. 向量时钟算法简介——本质类似MVCC
  5. Socket Programming in C#--Server Side
  6. 20145207《Java程序设计》第三周学习总结
  7. MySQL数据库表中有usage字段名后的后果
  8. 锋利的jquery-validation
  9. list集合接口
  10. Office Web Add-in的技术原理和开发常见问题剖析
  11. 浅谈XAML控件
  12. using MR to compute PageRank
  13. 用Socket编写的聊天小程序
  14. (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
  15. MapReduce-深度剖析
  16. 2、SpringBoot接口Http协议开发实战8节课(7-8)
  17. android 注入so
  18. java-学习10
  19. .NetCore 中如何实现分页以及编写一个URL分页
  20. 根据源Excel文件,新建Excel文件

热门文章

  1. HTML中Meta属性http-equiv="X-UA-Compatible"详解
  2. [leetcode]_Container With Most Water
  3. vue中的重要特性
  4. Spark基础排序+二次排序(java+scala)
  5. php 解决json_encode中文问题
  6. php对mysql简单读取的实例
  7. 16)JAVA实现回调(Android,Swing中各类listener的实现)
  8. C语言警告:warning C4018: “<”: 有符号/无符号不匹配
  9. IOS开发之后台处理
  10. python 数据类型(sequence 序列、dictionary 词典、动态类型)