本文介绍: 前面主要了解了 Python语言基础,现在我们来了解 Python我们提供了哪些数据类型,以及如何使用这些数据类型。Python语言中,一切皆为对象,而每个对象都属于某个数据类型;Python数据类型包括内置的数据类型模块定义的数据类型用户自定义类型数值数据类型intboolfloatcomplex序列数据类型:不可变str、tuple、bytes)和可变listbytearray);集合数据类型set、frozenset字典数据类型:dict。

简述 / 前言

前面主要了解了 Python语言基础,现在我们来了解 Python 给我们提供了哪些数据类型,以及如何使用这些数据类型。

上一篇文章我们介绍了数值数据类型,这篇我们介绍序列数据类型(str、tuple、list、bytes和bytearray

1. str 数据类型(字符串

  1. 引号(’ ‘)。包含在单引号中的字符串,其中可以包含双引号
  2. 引号(” “)。包含在双引号中的字符串,其中可以包含单引号。
  3. 三单引号(“” “”)。包含在三单引号中的字符串可以跨行。
  4. 三双引号(“”” “””)。包含在三双引号中的字符串,可以跨行。

1.1 str对象

str() 可以将任何数据类型转为str类型。

>>> str(2023)
'2023'
>>> str(False)
'False'
>>> str(3.141592)
'3.141592'
>>> str('小邓在森林')
'小邓在森林'
>>> str([1, 2, 3])
'[1, 2, 3]'
>>> str(None)
'None'

1.2 str对象属性方法

  • 转成大写

    >>> s = 'abc'
    >>> s.upper()		# 字符串对象s的方法输出:'ABC'
    'ABC'
    >>> str.upper(s)	# str类方法,字符串s作为参数输出:'ABC'
    'ABC'
    
  • 转成小写

    >>> s = 'AbC'
    >>> s.lower()
    'abc'
    >>> str.lower(s)
    'abc'
    >>> s = '小邓在森林'
    >>> s.lower()
    '小邓在森林'
    >>> str.lower(s)
    '小邓在森林'
    

    大小转换只对英文字符有用~

    Python还提供了另一个内置函数repr()函数 repr() 返回一个对象的更精确的字符串表示形式,但是经过实操发现它和 str() 有什么区别

    >>> c = 1 / 3
    >>> str(c)
    '0.3333333333333333'
    >>> repr(c)
    '0.3333333333333333'
    

1.3 字符串编码

>>> ord('a')
97
>>> ord('A')
65
>>> chr(97)
'a'
>>> chr(65)
'A'
>>> ord('森')
26862
>>> chr(26862)
'森'

1.4 转义字符

转义字符 功能/作用 转义字符 功能/作用
单引号 n 换行(LF)
双引号 r 回车(CR)
\ 斜杠 t 水平制表符(HT)
a 响铃(BEL) v 垂直制表符(VT)
b 退格(BS) ooo 八进制Unicode码对应的字符
f 换页(FF) xhhh 十六进制Unicode码对应的字符

:标粗的 5 个转义字符是本人觉得最常用的~

1.5 字符串的格式化

  1. 字符串.format(value1, value2, …)
    >>> "我是{0}, 加入CSDN已经{1}年了".format("小邓在森林", 2)
    '我是小邓在森林, 加入CSDN已经2年了'
    
  2. str.format(格式字符串, value1, value2, …)
    >>> str.format("我是{0}, 加入CSDN已经{1:.1f}年了", "小邓 在森林", 2)
    '我是小邓在森林, 加入CSDN已经2.0年了'
    
  3. format(value, 格式字符串)
    >>> format(99.99, "0.4f")
    '99.9900'
    
  4. 格式字符串 % (value1, value2, …) 兼容Python2的格式,但是这里不建议使用
    >>> "我是%s, 加入CSDN已经%2d年了" % ("小邓在森林", 2)
    '我是小邓在森林, 加入CSDN已经 2年了'
    

更多示例(画三角形):

  1. 居中对齐
    >>> print("1".center(10))
        1
    >>> print(format("121", "^10"))
       121
    >>> print(format("12321", "^10"))
      12321
    
  2. 对齐
    >>> print("1".rjust(10, "*"))
    *********1
    >>> print(format("121", "*>10"))
    *******121
    >>> print(format("12321", "*>10"))
    *****12321
    
  3. 左对齐
    >>> print("1".ljust(10, "*"))
    1*********
    >>> print(format("121", "*<10"))
    121*******
    >>> print(format("12321", "*<10"))
    12321*****
    

2. tuple 数据类型(元组

一组有序系列,包含0个或多个对象引用

元组可以通过创建 tuple 对象来创建,在 Python 中用小括号表示。

2.1 创建元组对象

>>> t1 = ()
>>> t2 = 1, 2
>>> t3 = 1,
>>> t4 = (1, 2, 3)
>>> t5 = 'a', 'b', 'c'
>>> t6 = 3.14			# 后面没加逗号,所以不是元组!
>>> print(t1, t2, t3, t4, t5, t6)
() (1, 2) (1,) (1, 2, 3) ('a', 'b', 'c') 3.14

3. list 数据类型(列表

类似下面这种形式的数据就是 list :[x1, x2, [x3, …, xn]],可以通过创建 list 对象来创建。

3.1 创建列表对象

>>> l1 = []
>>> l2 = [2023]
>>> l3 = ['a', 'b', 'c']
>>> print(l1, l2, l3)
[] [2023] ['a', 'b', 'c']

3.2 list 对象的方法

>>> s = [1, 2, 3]
方法 功能
s.append(x) 把对象x追加到列表s尾部
s.clear() 删除所有元素。相当于del s[:]
s.copy() 拷贝列表
s.extend(f) 序列t附加到s尾部
s.insert(index, x) 下标 index 位置插入对象x
s.pop([index]) 返回移除下标 index 位置对象,省略 index 时为最后对象。若超出下标,将导致IndexError !
s.remove(x) 移除列表中第一个出现的x。若对象不存在,将导致ValueError !
s.reverse() 逆置列表
s.sort() 列表排序
>>> s = [1, 2, 3]
>>> s.append('abc')
>>> s
[1, 2, 3, 'abc']
>>> s.clear()
>>> s
[]
>>> s = [1, 2, 3]
>>> s1 = s.copy()
>>> s1
[1, 2, 3]
>>> s.extend('abc')
>>> s
[1, 2, 3, 'a', 'b', 'c']
>>> s.pop(3)
'a'
>>> s
[1, 2, 3, 'b', 'c']
>>> s.remove('b')
>>> s
[1, 2, 3, 'c']
>>> s.reverse()
>>> s
['c', 3, 2, 1]
>>> s.sort()		# 注意:排序时不能有字符串,只支持数值类型的元素排序!!!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> s.remove('c')
>>> s
[3, 2, 1]
>>> s.sort()
>>> s
[1, 2, 3]

3.3 列表解析表达式

  1. value for i_1 in 序列1 ... for i_N in 序列N迭代序列里面的所有内容,并计算生成列表
  2. value for i_1 in 序列1 ... for i_N in 序列N if ...:按照条件迭代,并计算生成列表
>>> [val**2 for val in range(5)]        		# 平方
[0, 1, 4, 9, 16]
>>> [(val, val**2) for val in range(5)] 		# 原值,平方
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
>>> [val for val in range(10) if val%2==1]      # 取奇数
[1, 3, 5, 7, 9]

4. 系列数据的基本操作

函数 功能
len() 获取系列长度
max() 获取系列元素最大值
min() 获取系列元素最小值
sum() 获取列表或元组中各元素之和
x in s 元素x在s中吗?是的话为True,否则为False
x not in s 元素x不在s中吗?是的话为True,否则为False
s.count(x) 返回x在s中出现的次数
s.index(x) 返回x在s中第一次出现的下标
sorted(iterable, key=None, reverse=False) 返回系列的排序列
all(iterable) 如果序列的所有值都为True,返回True;否则,返回False
any(iterable) 如果序列的任意值为True,返回True;否则,返回False
  1. 字符串

    >>> s = '小邓在森林-2023'
    >>> len(s)
    10
    >>> max(s)
    '邓'
    >>> min(s)
    '-'
    >>> s1 = 'abcd'
    >>> len(s1)
    4
    >>> max(s1)
    'd'
    >>> min(s1)
    'a'
    >>> s2 = ''
    >>> len(s2)
    0
    >>> max(s2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: max() arg is an empty sequence
    >>> min(s2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: min() arg is an empty sequence
    >>> '小' in s
    True
    >>> '1' in s
    False
    >>> '1' not in s
    True
    >>> s.count('2')
    2
    >>> s.count('1566')
    0
    >>> s.index('森林')
    3
    >>> s.index('6')		# '6'不在s中
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: substring not found
    >>> sorted(s)
    ['-', '0', '2', '2', '3', '在', '小', '林', '森', '邓']
    >>> sorted(s, reverse=True)
    ['邓', '森', '林', '小', '在', '3', '2', '2', '0', '-']
    >>> s1 = 'abAD'
    >>> sorted(s1, key=str.upper)
    ['a', 'A', 'b', 'D']
    >>> sorted(s1, key=str.lower)
    ['a', 'A', 'b', 'D']
    

    :汉字求 max, min 是先将汉字转为汉字编码再进行大小比较,而英文则是转为 ASCII 码。空字符串不能求 maxmin

  2. 元组

    >>> t = (1, 2, 3)
    >>> len(t)
    3
    >>> max(t)
    3
    >>> min(t)
    1
    >>> t1 = ()
    >>> len(t1)
    0
    >>> max(t1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: max() arg is an empty sequence
    >>> min(t1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: min() arg is an empty sequence
    >>> 1 in t
    True
    >>> -1 in t
    False
    >>> -1 not in t
    True
    >>> t.count(1)
    1
    >>> t.index(2)
    1
    >>> t.index(1)
    0
    >>> sorted(t)
    [1, 2, 3]
    >>> sorted(t, reverse=True)
    [3, 2, 1]
    

    :空元组不能求 maxmin

  3. 列表

    >>> l = [1, 2, 3]
    >>> len(l)
    3
    >>> max(l)
    3
    >>> min(l)
    1
    >>> l1 = []
    >>> len(l1)
    0
    >>> max(l1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: max() arg is an empty sequence
    >>> min(l1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: min() arg is an empty sequence
    >>> 3 in l
    True
    >>> 3 not in l
    False
    >>> l.count(3)
    1
    >>> l.index(2)
    1
    >>> sorted(l)
    [1, 2, 3]
    >>> sorted(l, reverse=True)
    [3, 2, 1]
    

    :空列表不能求 maxmin

如何访问系列元素?
s[index] 即可访问不同数据类型的 index 位置的数据,注意它是从 0 开始计算的访问时不能越界!!!

python 中如果 index 为负数,表示从尾部向前取元素,最后一个元素索引-1

其中:

s

[

0

]

=

s

[

l

e

n

(

s

)

]

s[0] = s[-len(s)]

s[0]=s[len(s)]

  1. 字符串

    >>> s = 'abcdefg'
    >>> s[0]
    'a'
    >>> s[-1]
    'g'
    >>> s[10]	# 越界
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: string index out of range
    
  2. 元组

    >>> t = (1, 2, 3)
    >>> t[0]
    1
    >>> t[-1]
    3
    >>> t[-3]
    1
    >>> t[100]		# 越界
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: tuple index out of range
    
  3. 列表

    >>> l = [1, 2, 3]
    >>> l[0]
    1
    >>> l[-1]
    3
    >>> l[2023]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    

5. 系列的切片操作

前面只说了取一个元素,那么能不能一次性取多个元素呢?答案是可以的

s[start:stop] 或者 s[start:stop:step] 就可以一次性取多个元素。

含意
start 取值的开始索引
stop 取值终止索引
step 取值步长

切片操作区间是左闭右开的,即只能取到 s[start], ..., s[stop-1]s[stop] 无法取到!

如果 stop > len(s)-1 , 则只会取到索引len(s)-1 的元素。

  1. 字符串

    >>> s = 'abcdefg'
    >>> s[0:2]
    'ab'
    >>> s[1:3]
    'bc'
    >>> s[1:6:2]	# 会间隔一个元素再取
    'bdf'
    >>> s[::2]		# 从第一个元素开始取,然后间隔一个元素再取
    'aceg'
    >>> s[::-1]		# 从后往前取
    'gfedcba'
    >>> s[::-2]		# 从后往前取,每次间隔一个元素
    'geca'
    
  2. 元组

    >>> t = (1, 2, 3, 4, 5, 6)
    >>> t[2:6]
    (3, 4, 5, 6)
    >>> t[::-3]
    (6, 3)
    >>> t[::-1]
    (6, 5, 4, 3, 2, 1)
    >>> t[2:6:2]
    (3, 5)
    >>> t[2:10]		# 等价于t[2:6]
    (3, 4, 5, 6)
    >>> t[2:6]
    (3, 4, 5, 6)
    
  3. 列表

    >>> l = [1, 2, 3, 4, 5, 6]
    >>> l[-5:-1]
    [2, 3, 4, 5]
    >>> l[-5:-1:2]
    [2, 4]
    >>> l[-5:-1:-1]
    []
    >>> l[-1:-5:-1]
    [6, 5, 4, 3]
    

6. 字节系列

字节系列(bytes 和 bytearray)是由 8 位字节数据组成的系列数据类型,即 0<=x<256 的整数系列。

6.1 bytes常量

使用字母 b 加单引号或双引号括起来的内容。

bytes常量与字符串定义方式类似:

  1. 单引号(b' ')。包含在单引号中的字符串,其中可以包含双引号;
  2. 双引号(b" ")。包含在双引号中的字符串,其中可以包含单引号;
  3. 三单引号(b''' ''')。包含在三单引号中的字符串,可以跨行;
  4. 三双引号(b""" """)。包含在三双引号中的字符串,可以跨行。

6.2 创建 bytes 和 bytearrary 对象

函数 功能
bytes() 创建空的bytes对象
bytes(n) 创建长度为n的bytes对象,各字节为0
bytearrary() 创建空的bytearrary对象
bytearrary(n) 创建长度为n的bytearrary对象,各字节为0

还有一些创建 bytes 和 bytearrary 对象的方法,因为这个不常用,所以不具体介绍,下面主要讲讲编码解码问题就结束了。

6.3 字节编码解码

字符串可以通过 s.encode() 方法编码为字节码;通过 s.decode() 方法解码为字符串。

>>> s = '小邓在森林'
>>> b = s.encode()
>>> b
b'xe5xb0x8fxe9x82x93xe5x9cxa8xe6xa3xaexe6x9ex97'
>>> b.decode()
'小邓在森林'

这里展示了字符串和字节码之间的转换,字符串还可以和 UTF-8GBK 等编码进行转换

文章传送门

上一篇文章【人生苦短,我学 Python】(3)Python 常用内置数据类型 I —— 数值数据类型(int、float、complex、bool)
下一篇文章

原文地址:https://blog.csdn.net/senlin_6688/article/details/134665042

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_9799.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注