Python学习(四)数据结构(概要)

Python 数据结构

  本章介绍 Python 主要的 built-type(内建数据类型),包括如下:

  • Numeric types          int float
  • Text Sequence Type     str
  • Boolean                bool
  • Sequence  Types        list tuple range
  • Set Types              set
  • Mapping Types          dict

  

  type() 函数

    type(object) 函数,返回括号中对象的 数据类型。

  数字类型

    python 提供了常用的数字类型:整数int,浮点数float,复数complex(暂不学习,需要时参考文档)

1 number = 1                  # int
2 # print(number)
3 print(type(number))
4 number = 1.0                # float 注意加了小数点 .0 就是浮点型了
5 # print(number)
6 print(type(number))

    数字可通过运算符进行简单的运算,也可通过导入math 等模块进行数学计算;详细章节参考:Python学习(三)数据结构 —— int float

   字符串类型

    字符串是 Python 中最常用的数据类型。我们可以使用引号来创建字符串。创建字符串很简单,只要为变量分配一个值即可。

1 string = "hello world"
2 print(string)
3 print(type(string))            # type() 返回类型函数

    Python 提供了很多字符串的操作方法以及内建的函数;详细章节参考:Python学习(四)数据结构 —— str

   布尔类型

    布尔类型使用两个常量表示 True 和 False,注意首字母大写,可进行 and or not 的运算;详细章节参考:Python学习(四)数据结构 —— bool

1 T = True                  # 布尔类型:True False;注意大小写
2 F = False
3 # t = true                # 小写报错
4 print(type(T))
5 print(type(F))

   序列类型

    list   tuple  及 range;list 类似 c 中的数组,有序;tuple 是不可变的 list;range 是 python 特有的,可方便产生等差的序列

    list是最常用的Python数据类型,需要熟练掌握其创建、访问、更新、删除等操作;range的使用方法也需熟练掌握,常用于 for 循环计数;

    详细章节参考:Python学习(四)数据结构 —— list tuple range

 1 list1 = [1,2,True,"c"]         # 列表类型;列表中的元素可为不同的类型;允许添加、修改、删除等操作
 2 list2 = []                     # 空列表
 3 print(type(list1))
 4 # print(type(list2))
 5 
 6 tuple1 = (1,2,True,"c")        # 元组类型:和列表类似,不同在于list可变,tuple不可变,无法添加、修改、删除等接口;其内存处理较优越
 7 tuple2 = (1)                   # 注意仅一个元素时候,要在后面加个 “,”,不然数据类型就不是元组了
 8 tuple3 = (1,)
 9 tuple4 = ()                    # 空元组的赋值
10 print(type(tuple1))
11 # print(type(tuple2))
12 # print(type(tuple3))
13 # print(type(tuple4))
14 
15 print(type(range(0)))    
16 # print(list(range(5)))        # range(stop) 产生0至stop(不包含stop)的等差为1的序列,可转换为list类型,便于查看理解
17 # print(list(range(1,4)))      # range(start, stop[, step])  产生start至stop(不包含stop)的等差为step的序列,step缺省为1
18 # print(list(range(8,1,-2)))   # step可为负值

   集合类型

    包括 set 和 frozenset(不可变的set);集合就是一组无序的元素组合,通常用作关系测试及去重;支持 交、并、差集运算

    详细章节参考:Python学习(四)数据结构 —— set frozenset

1 set1 = {1,2,3,"1",1,2}           # 集合类型:无序不重复元素的集。基本功能包括关系测试和消除重复元素。
2 set2 = {}                        # 注意:这不是空集合的表示法,这表示空字典,下节会介绍
3 set3 = set()                     # 空集合的赋值
4 print(type(set1))
5 # print(type(set2))
6 # print(type(set3))

    词典类型

    词典类型即 dict,由键和对应值成对组成;可对dict 的键值进行访问、修改;对dict 增加、删除键值等操作

    详细章节参考:Python学习(四)数据结构 —— dict

1 dict1 = {'Name': 'Zara', 'Age': 7};       # 字典类型:以关键字为索引,来存储和析取值
2 dict2 = {}                                # 空字典的赋值
3 print(type(dict1))
4 # print(type(dict2))

     数据类型转换

    Python 可对变量的数据类型进行强制转换,只需将数据类型作为函数名即可。不是所有的数据类型都能进行相互间的转换,仅列出常用的几个。

 1 x = float(1)                     # int 转换成 float
 2 print(x,type(x))
 3 x = int(1.9)                     # float 转换成 int,相当于取整
 4 print(x,type(x))
 5 
 6 x = str(x)                       # int 或 float 可转换成字符串
 7 print(x,type(x))
 8 # x = int("a")                   # 该字符串无法转换成int,执行该条会报错
 9 a = int("1")                     # 但是,可表示成 int 或 float 的字符串可转换
10 b = float("1.0")
11 print(a,type(a),b,type(b))
12 
13 print(int(True),int(False))      # 布尔型可转换成int类型 True 1; False 0                    
14 
15 l = [1,2,3,2,3]
16 t = tuple(l)
17 s = set(l)
18 print(t,type(t),s,type(s))       # 通常 list tuple set 可进行相互间的转换,注意set是去重的

     type() 的其他返回

    对于内建函数,type() 会返回为 built-in function ... ;

    对于用户自定义函数,type() 会返回为 function ;

    对于对象,type() 会返回为 type ;

# print(print)                    # Python2报错;Python3 返回 built-in function print

def my_fun():                     # 定义函数 my_fun
    pass                          # pass 为空语句
print(type(my_fun))               # 返回 function

print(type(str))                  # 内建数据类型名称 即 built-in type  返回type

class table(object):              # 定义对象 type()
    pass
print(type(table))                # 用户自定义对象也返回为 type

    


出处:http://www.cnblogs.com/feeland/

本博客内容大多为作者原创 如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客

如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

原文地址:https://www.cnblogs.com/feeland/p/4355237.html