美文网首页
《零基础上手Python爬虫系列》 - 03 Python数据类

《零基础上手Python爬虫系列》 - 03 Python数据类

作者: 聂云⻜ | 来源:发表于2020-02-12 22:57 被阅读0次

    本篇主要为Python中的基本数据类型学习。

    计算机,顾名思义就是可以做数学计算的机器,因此计算机程序理所当可以处理各种数值。但计算机能处理的远不止数值,还可以处理文本、图形、音频、视频、网页等各种各样的数据,而不同的数据,需要定义不同的数据类型。

    在Python中,能够直接处理的数据类型有以下几种:

    一,数字类型 Number

    数字类型包含整型int,浮点数float, 布尔型bool和复数complex

    1. 整型int

    Python3不同于Python2和其他语言,只有int,不存在short 、long等细节区分

    >>> type(1)
    <class 'int'>
    
    >>> type(1000000000000)
    <class 'int'>
    

    进制标识

    # 0b标志二进制
    >>> 0b10
    2
    
    # 0o标志八进制
    >>> 0o10
    8
    
    # 0x标志16进制
    >>> 0x10
    16
    

    进制转换

    # 转二进制
    >>> bin(1)
    '0b1'
    
    # 转八进制
    >>> oct(1)
    '0o1'
    
    # 转十进制
    >>> int(0b01)
    1
    
    # 转16进制
    >>> hex(1)
    '0x1'
    

    2. 浮点型 float

    Python3不同于其他语言,float也不存在单精度(float)和双精度(double)

    >>> type(1.0)
    <class 'float'>
    
    >>> type(1.000000000001)
    <class 'float'>
    >>>
    

    整数的四则运算,类型以具体结果为准

    >>> type(1+1)
    <class 'int'>
    
    # // 标示整除
    >>> type(1//1)
    <class 'int'>
    
    >>> type(1//2)
    <class 'int'>
    
    >>> type(1+1.0)
    <class 'float'>
    
    # 注意:Python中的结果为1.0
    >>> type(1/1)
    <class 'float'>
    
    >>> type(1/2)
    <class 'float'>
    

    3. 布尔型 bool

    布尔型表示真或假,首字母必须大写

    >>> True
    True
    >>> False
    False
    
    >>> true
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'true' is not defined
    

    结果为False的一些情况

    >>> bool('abc')
    True
    
    >>> bool(0)
    False
    >>> bool(0.0)
    False
    >>> bool({})
    False
    >>> bool([])
    False
    >>> bool('')
    False
    >>> bool("")
    False
    >>> bool(None)
    False
    
    >>> bool(none)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'none' is not defined
    

    任何为空的都为False,None也必须首字母大写

    4. 复数complex

    >>> 36j
    36j
    

    j标识复数

    我们把形如z=a+bi(a,b均为实数)的数称为复数,其中a称为实部,b称为虚部,i称为虚数单位。当z的虚部等于零时,常称z为实数;当z的虚部不等于零时,实部等于零时,常称z为纯虚数。

    这个不常用到,快速学一门新语言要抓重点,不要死抠一些细节

    二,组

    1. 序列 - 字符串 Str

    单引号、双引号和多行字符串

    >>> "Let's go"
    "Let's go"
    
    # 对于字符串非常长或需要换行的可以用三引号
    >>> '''this string
    ...
    ...
    ...
    ... is to long
    ... '''
    'this string\n\n\n\nis to long\n'
    
    # 单引号也可以换行,用\
    >>> 'hello \
    ... world'
    'hello world'
    

    转义字符

    # 换行符
    >>> print('\n')
    
    # 制表符
    >>> print('\t')
    
    # 单引号
    >>> print('\'')
    '
    # 换行
    >>> print('\r')
    
    

    原始字符串

    # 正常会被转义
    >>> print('C:\Windows\normal')
    C:\Windows
    ormal
    
    # 采用转义符处理
    >>> print('C:\Windows\\normal')
    C:\Windows\normal
    
    # 加r(不区分带小写)之后的原始字符,所见即所得,不会再有转义的情况
    >>> print(r'C:\Windows\normal')
    C:\Windows\normal
    >>> print(R'C:\Windows\normal')
    C:\Windows\normal
    

    字符串的操作

    # 字符串运算,拼接
    >>> 'hello' + ' world'
    'hello world'
    
    # 可以用字符串 * N
    >>> 'hello ' * 3
    'hello hello hello '
    

    获取指定字符

    # 字符串序号从0开始
    >>> 'hello world'[0]
    'h'
    
    # 空格也算字符
    >>> 'hello world'[5]
    ' '
    
    # 负号表示从右边起,第N个数
    >>> 'hello world'[-1]
    'd'
    

    截取字符串

    # N >=1 && N < 6
    'hello world'[1:6]
    'ello '
    
    # 从0到最后,右边为空,则表示到最后;左边为空,则表示从首开始
    >>> 'hello world'[0:]
    'hello world'
    
    # 数量超出不影响
    >>> 'hello world'[0:100]
    'hello world'
    
    # 从0到倒数第一个数,不包含最后一个
    >>> 'hello world'[0:-1]
    'hello worl'
    
    # 对于很长的字符串,负数截取更方便
    >>> 'hello php,java,python,go,c,c++,vb,.net,react,vue,node,'[0:-1]
    'hello php,java,python,go,c,c++,vb,.net,react,vue,node'
    
    # 负号在前面表示从右边开始数,直到最后
    >>> 'hello php,java,python,go,c,c++,vb,.net,react,vue,node'[-4:]
    'node'
    

    2,序列 - 列表 list

    列表的定义

    # 列表用list标识
    >>> type([1,2,3])
    <class 'list'>
    
    # 列表定义
    >>> [1,2,3,4,5]
    [1, 2, 3, 4, 5]
    
    # 列表元素可以为任意值
    >>> [1,"hi",3,True]
    [1, 'hi', 3, True]
    
    # 嵌套列表
    >>> [1,[1,2,3]]
    [1, [1, 2, 3]]
    

    列表的访问(跟字符串的访问方式一样)

    >>> ['吃饭','睡觉','打豆豆'][0]
    '吃饭'
    
    >>> ['吃饭','睡觉','打豆豆'][0:1]
    ['吃饭']
    
    >>> ['吃饭','睡觉','打豆豆'][-1:]
    ['打豆豆']
    

    列表的操作

    # +
    >>> ['暴击'] + ['防御']
    ['暴击', '防御']
    
    # *
    >>> ['暴击'] * 3
    ['暴击', '暴击', '暴击']
    

    定义只有一个元素的列表

    >>> [1]
    [1]
    
    >>> type([1])
    <class 'list'>
    

    3,序列 - 元祖 tuple

    元祖的定义

    # 元祖用tuple标识
    >>> type((1,2,3))
    <class 'tuple'>
    
    # 元祖的定义
    >>> type((1,2,3))
    
    <class 'tuple'>
    >>> (1, '2', True)
    (1, '2', True)
    
    # 元祖的访问
    >>> (1, '2', True)[1]
    '2'
    >>> (1, '2', True)[0:]
    (1, '2', True)
    
    # 元祖的操作
    >>> (1,2,3) + (4,5)
    (1, 2, 3, 4, 5)
    
    >>> (1,2,3) * 2
    (1, 2, 3, 1, 2, 3)
    
    

    特例:当括号内只有一个元素的时候,会认为()是计算中的括号,而非元祖的定界符

    >>> type((1))
    <class 'int'>
    >>> type(('hello'))
    <class 'str'>
    

    如何定义一个只有一个元素的元祖

    >>> (1,)
    (1,)
    
    >>> type((1,))
    <class 'tuple'>
    
    >>> len((1,))
    1
    

    如何定义一个空元祖

    >>> ()
    ()
    
    >>> type(())
    <class 'tuple'>
    

    4,序列总结

    str, list, tuple三种都是序列的类型,具有如下特点:

    • 都可以通过序号来访问,进行切片操作[-1:]
    • 都可以进行 + 和 * 数字的计算

    判断元素是否在序列中

    >>> 1 in [1,2,3]
    True
    
    >>> 1 in (1,2,3)
    True
    
    >>> 1 in '1,2,3'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'in <string>' requires string as left operand, not int
    
    >>> '1' in '1,2,3'
    True
    

    NOT IN

    >>> 'python' not in ['java', 'php']
    True
    

    函数操作

    >>> len([1,2,3])
    3
    >>> len((1,2,3))
    3
    >>> len('1,2,3')
    5
    
    # 字符串判断大小用ASCII码
    >>> ord('w')
    119
    
    >>> max('hello world')
    'w'
    
    >>> max([1,2,3])
    3
    >>> max((1,2,3))
    3
    >>> max('123')
    '3'
    >>> min([1,2,3])
    1
    

    5,集合

    集合的标志符是set,特点是无序、唯一

    >>> type({1,2,3})
    <class 'set'>
    
    >>> {1,2,3,3,3,4}
    {1, 2, 3, 4}
    

    集合跟序列不同,不支持相加

    >>> {1,2,3} + {4}
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'set' and 'set'
    

    集合的操作类似PHP的数组,支持差集、并集和交集

    # 差集
    >>> {1,2,3,4} - {1,2}
    {3, 4}
    
    # 交集
    >>> {1,2,3} & {2,5}
    {2}
    
    # 并集
    >>> {1,2,3} | {4,5}
    {1, 2, 3, 4, 5}
    

    如何创建一个空集合

    >>> set()
    set()
    
    >>> type(set())
    <class 'set'>
    
    >>> len(set())
    0
    

    6,字典 dict

    字典的标志符是dict,格式{key:value, key:value ... },也是无序的

    >>> {type({1:1,2:2})}
    {<class 'dict'>}
    

    字段的访问,下标必须跟key一致

    >>> {'Q':'急速冷却', 'R':'天火', 'Z':'吹风'}[Q]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'Q' is not defined
    
    >>> {'Q':'急速冷却', 'R':'天火', 'Z':'吹风'}['Q']
    '急速冷却'
    

    字典的key不允许重复,否则会过滤掉

    >>> {'A':'我艹','A':'我艹', 'B':'我屮艸芔茻'}
    {'A': '我艹', 'B': '我屮艸芔茻'}
    

    字典的key必须是不可变的类型(数字,字符串,元祖),value可以是任意类型

    >>> {1:1,'2':'二',(3):[3]}
    {1: 1, '2': '二', 3: [3]}
    
    >>> type({1:1,'2':'二',(3):[3]})
    <class 'dict'>
    

    如何创建一个空字典

    >>> {}
    {}
    
    >>> type({})
    <class 'dict'>
    

    相关文章

      网友评论

          本文标题:《零基础上手Python爬虫系列》 - 03 Python数据类

          本文链接:https://www.haomeiwen.com/subject/cjhhfhtx.html