首页 电脑 电脑学堂 查看内容

Python数据类型

2016-9-9 17:55 1304 0

摘要: 目录目录前言软件环境Python数据结构树状图基本数据类型数值型整型Integral浮点型Float复数布尔型Bool变量的命名规则组合数据类型序列字符串String元组Tuple列表List字典dictionary最后前言Python作为一种弱类型编程 ...

目录

前言

Python作为一种弱类型编程语言,其数据类型较之于C/C++无论是在数据的存储还是调用都有着很大的区别。其特有的字典类型更是一个非常经典且功能强大的数据类型。下面一起来学习Python的数据类型,期间也会穿插一些Python的实用技巧。

软件环境

  • 系统 
    • Ubuntukylin 14.04
  • 软件 
    • Python 4.7.6
    • IPython 4.0.0

Python数据结构树状图

  • 基本数据类型 
    • 数值型 
      – 整型 
      – 浮点型 
      – 复数
    • 布尔型
    • 字符型
  • 组合数据类型 
    • 序列 
      – 列表 
      – 元组 
      – 字符串
    • 字典
    • 集合
    • 文件
  • 自定义类型 

基本数据类型

数值型

整型Integral

整型int是一种不可变类型

In [9]: num = 1

In [10]: num = 2

In [11]: num
Out[11]: 2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

需要注意的是,num = 1、 num = 2 并不是同一个数据对象。第一个num变量保存着 1 在内存中存储的索引地址,第二个变量num则表示 2 在内存中存储的索引地址。在上面的两次赋值中,1、2 两个数据对象在内存中并没有改变,改变的只是 num 变量所指向的内存地址。所以说int类型是一种不可变类型,int类型对象在内存中并不会发生改变,但可以改变int型对象所对应变量的值。其调用过程为:变量num只存储了索引地址,再通过索引找到在内存中存储的int型对象,并最终引用出来。

浮点型Float

In [51]: floatTest
Out[51]: 3.141592

In [52]: floatTest = 3.14159299999999999999999999999999999999999

In [53]: floatTest
Out[53]: 3.141593

In [54]: type(float)
float      floatTest  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

上面的例子可以看出Python中float类型的数据同样有着存储限制。

复数

Python还支持复数,为Math领域的研究提供了非常好的支持。

In [48]: pulralTest = 3+6j

In [49]: pulralTest
Out[49]: (3+6j)
  • 1
  • 2
  • 3
  • 4

布尔型Bool

In [12]: bool = False

In [13]: bool
Out[13]: False 

In [15]: bool = True

In [16]: bool
Out[16]: True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

布尔类型可以直接赋值,注意首字母要大写。

变量的命名规则

  1. 大小写敏感,可以含有下划线_ 。
  2. 命名惯例
    a. 尽量不要以’ _ ’ 为首字母 
    b. ‘_X_’ 为系统变量,有特殊含义 
    c. ‘__X’ 表示类的本地变量,在类中调用(注意:是两条下划线’__’) 
    d. ‘__X__’ Python Module的内置方法 
    e. ’ _ ’ 存储着当前最后一个表达式的结果
In [42]: 2*3
Out[42]: 6

In [43]: print _
6
  • 1
  • 2
  • 3
  • 4
  • 5

使用dir( )函数可以显示Python一般对象的内置方法,如下:

In [58]: import platform

In [59]: dir(platform)
Out[59]: 
['DEV_NULL',
 '__builtins__',
 '__copyright__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__version__',
 '_abspath',
 '_architecture_split',
 '_bcd2str',
 '_codename_file_re',
 '_default_architecture',
 '_dist_try_harder',
 '_distributor_id_file_re',
 '_follow_symlinks',
 '_ironpython26_sys_version_parser',
 '_ironpython_sys_version_parser',
 '_java_getprop',
 '_libc_search',
 '_lsb_release_version',
 '_mac_ver_gestalt',
 '_mac_ver_lookup',
 '_mac_ver_xml',
 '_node',
 '_norm_version',
 '_parse_release_file',
 '_platform',
 '_platform_cache',
 '_popen',
 '_pypy_sys_version_parser',
 '_release_file_re',
 '_release_filename',
 '_release_version',
 '_supported_dists',
 '_sys_version',
 '_sys_version_cache',
 '_sys_version_parser',
 '_syscmd_file',
 '_syscmd_uname',
 '_syscmd_ver',
 '_uname_cache',
 '_ver_output',
 '_win32_getvalue',
 'architecture',
 'dist',
 'java_ver',
 'libc_ver',
 'linux_distribution',
 'mac_ver',
 'machine',
 'node',
 'os',
 'platform',
 'popen',
 'processor',
 'python_branch',
 'python_build',
 'python_compiler',
 'python_implementation',
 'python_revision',
 'python_version',
 'python_version_tuple',
 're',
 'release',
 'string',
 'sys',
 'system',
 'system_alias',
 'uname',
 'version',
 'win32_ver']

In [60]: dir(type)
Out[60]: 
['__abstractmethods__',
 '__base__',
 '__bases__',
 '__basicsize__',
 '__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dictoffset__',
 '__doc__',
 '__eq__',
 '__flags__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__instancecheck__',
 '__itemsize__',
 '__le__',
 '__lt__',
 '__module__',
 '__mro__',
 '__name__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasscheck__',
 '__subclasses__',
 '__subclasshook__',
 '__weakrefoffset__',
 'mro']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

而Module或函数的__doc__方法可以显示Module或函数的Document,以此来认识一个Module或函数。而这些Document,写入在Python Module文件首部的三引号中”’ “’,同时”’ ”’作为批量输出的标识,用于同时输出多行内容或充当注释。

In [64]: platform.__doc__
Out[64]: ' This module tries to retrieve as much platform-identifying data as\n    possible. It makes this information available via function APIs.\n\n    If called from the command line, it prints the platform\n    information concatenated as single string to stdout. The output\n    format is useable as part of a filename.\n\n'

In [65]: id.__doc__
Out[65]: "id(object) -> integer\n\nReturn the identity of an object.  This is guaranteed to be unique among\nsimultaneously existing objects.  (Hint: it's the object's memory address.)"
  • 1
  • 2
  • 3
  • 4
  • 5

组合数据类型

序列

Python最基本的组合数据结构就是序列。序列,即由非负整数索引的对象的有序集合。有下列6种内建序列类型,其中又可以归为3类: 
1. 整数序列:xrange对象 
2. 字符序列:字符串、buffer(缓冲区)对象、Unicode字符串 
3. 对象序列:列表、元组 
其中列表、元组、字符串都是Python程序中非常常用的两种类型。

字符串String

字符串是一种序列,可以通过索引引用,以字符为单位,属于不可变类型。当定义一个str对象时,str对象会在内存中以ASCII码的格式从索引对象指定的入口地址依次以字符为单位存放起来,其中每一个Char都占用1Byte(8bit)的存储空间。 
:在Python中的字符串可以使用 ‘Str’ 或者 “Str” 括起来,而且两者没有任何的区别,只视乎于那一个使用起来更加方便和更适合使用环境。随便一提’” Document ‘”在Python中表示文档,用在代码行中,有类似注释的功能,而且’” ‘”中可以使用单引号和双引号。 
例子(1).

In [19]: name = 'jmilk'

In [20]: id(name)
Out[20]: 140682798476624

In [21]: name = 'chocolate'

In [22]: id(name)
Out[22]: 140682799006128

In [23]: type(name)
Out[23]: str
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

例子(2).

In [36]: name1 = 'jmilk'

In [37]: name2 = 'jmilk'

In [39]: id(name1) == id(name2)
Out[39]: True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

id(variable):查询变量存储的索引地址 
type(variable):查看变量的数据类型 
例子(1)中,显然同一个name变量在赋与不同的字符串类型值后name变量自身的索引地址发生了改变。 
例子(2)中,将同一个str对象’jmilk‘赋值给两个不同的变量,这两个变量的值(内存中的引用对象)却是一样的。 
字符串的切片: 
字符串最为序列的一种,可以使用切片操作符来实现字符串的切片显示。

In [62]: sentence = 'my name is jmilk'

In [63]: sentence
Out[63]: 'my name is jmilk'
  • 1
  • 2
  • 3
  • 4

(1)stringName[x:y] –>显示字符串中从第x个索引的字符–第y-1个字符,索引从0开始计算,其中的空格也算一个字符。

In [64]: sentence[0:7]
Out[64]: 'my name'
  • 1
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部