一周学会python5数据类型

5 数据类型

Python程序员使用各种数据类型来构建跨平台应用程序。因此必须了解数据类型在软件开发中的重要性。

5.1 数据类型简介

数据类型是一组预定义值,程序员在创建变量时使用它们。Python不是静态类型语言,所以不需要明确定义变量数据类型。

虽然Python程序员在创建程序时不需要定义变量数据类型,但要开发能与用户高效交互的复杂程序,了解各种可用的数据类型仍然是必要的。

years = 12

years的类型没有定义,Python解释器认为提供的值是整数。

5.2 不同的数据类型

在了解Python支持的各种数据类型之前,我们先来谈谈开发人员在编程时用来创建逻辑语句的基本编程片段。

让我们来看一个简单的表达式和语句。要在编程语言中创建逻辑语句,需要使用三个主要组件。

  • 数据标识符

为了存储数据,需要创建变量、列表和元组等编程组件。

a = 24
  • 字面量

比如上面的24。

  • 运算符

前面的代码中使用了赋值运算符 =。其他算术运算符,如 +、-、* 和 /,是众所周知的用于生成逻辑 Python 代码的运算符。

我们将介绍Python程序员在应用程序中最常用的一些数据类型。

5.3 字符串

字符串是常用于表示大量文本的数据类型。例如,字符串数据类型可以在程序中用单引号连接来表示文本。创建字符串数据类型时,会创建一个包含字符序列的”str”对象。

文本信息是人类相互交流的最常见方式。因此,为了创建有意义的软件,字符串是开发人员必须了解的最重要的数据类型。用字符串表示数据也很重要,因为计算机只能理解二进制数据。因此,使用ASCII和Unicode编码机制至关重要。

Python 3引入了一种高级编码机制,用于表示中文、日文和韩文等外语,这使得字符串成为软件开发中不可或缺的一部分。

字符串可以包含在单引号或双引号中。

z = 'This is my sentence'

单引号之间的所有内容都是字符串数据类型。变量”x”用于定义这种字符串数据。当变量为字符串数据类型时,其占用的位数通常决定了它的内存位置和大小。字符串数据类型的字符数与其位数成正比。”This is an example”有18个字符,包括空格。

还有其他几种定义字符串的方法。在实际项目中,为了保持一致性,尽可能使用单一类型。

# 用双引号定义字符串
a = "This is my sentence"
print(a)

# 用三个单引号定义字符串
b = '''This is my sentence'''
print(b)

# 用三个双引号定义字符串
c = """This is my sentence
but with more than one line """
print(c)

解释器按照字符串被输入的方式显示字符串,通常包含在单引号中,如果内容包含包含单引号,则包含在双引号中。

print会以更可视的格式显示:

>>> '"Isn't," she said.'
'"Isn't," she said.'
>>> print('"Isn't," she said.')
"Isn't," she said.
>>> s = 'First line.nSecond line.' # n means newline
>>> s # without print(), n is included in the output
'First line.nSecond line.'
>>> print(s) # with print(), n produces a new line
First line.
Second line.

在前面的示例中,我们定义了三种定义字符串的方法。特殊字符、符号和新制表符行也可以用在引号之间。Python还支持所有编程语言都使用的转义序列。例如n是程序员常用的转义序列,用来创建新行。

更多实例:

>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn't' # use ' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> ""Yes," he said."
'"Yes," he said.'
>>> '"Isn't," she said.'
'"Isn't," she said.'

字符串前面添加’r’表示原始字符串,里面的反斜杠不会转义:

>>>  r'C:Program Filesfoobar''\'
File "<stdin>", line 1
r'C:Program Filesfoobar''\'
IndentationError: unexpected indent
>>> r'C:Program Filesfoobar''\'
'C:\Program Files\foo\bar\'

原始字符串不能以单个反斜杠结尾。换而言之,原始字符串的最后一个字符不能是反斜杠,除非你对其进行转义(但进行转义时,用于转义的反斜杠也将是字符串的一部分)如果最后一个字符(位于结束引号前面的那个字符)为反斜杠,且未对其进行转义,Python将无法判断字符串是否到此结束。

以下第一个三引号后面有反斜杠,就不会输出第一个换行符。末尾的反斜杠表示续行。

>>> print("""
... Usage: thingy [OPTIONS]
... -h Display this usage message
... -H hostname Hostname to connect to
... """)
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to

5.3.1 访问字符串中的字符

因为字符串是 Python 中最常用的数据类型,所以核心库包含了几个用于与字符串数据交互的内置函数。要访问字符串中的字符,首先必须知道索引号。负索引和切分操作也可用于访问字符串的一部分。

字符串下标又称索引和C类似 ,第一个字符索引为 0 。没有独立的字符类型,字符就是长度为 1 的字符串,也可以使用负数,-1表示倒数第一个,-2表示倒数第二个,以此类推。不存在的下标会报IndexError。


>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[-6]
'P'
>>> word[-16]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[16]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
s = 'PYTHON'

# 我们打印整个字符串
print ('Whole string =', s)

# 我们打印第一个字符
print ('1st character =', s[0])

# 使用负索引打印最后一个字符
print ('Last character =', s[-1])

# 使用正索引打印最后一个字符
print ('Again, Last character =', s[5])

输出:

Whole string = PYTHON
1st character = P
Last character = N
Again, Last character = N

字符串支持切片:由两个索引,中间是冒号。第一个索引表示起点,包含该元素,默认为0;第2个索引表示终点,不包含该元素,默认为字符串末尾。s[:i] + s[i:]等同于s。

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'

记住切片的工作方式:切片索引是在字符之间。左边第一个字符的索引为0,右界索引为字符串长度n 。例如:

 +---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
图片

由于所有字符串数据类型都是不可变的,因此无法替换字面字符串中的字符。因此,尝试替换字符串字符将导致类型错误。

程序代码:

>>> s = 'PYTHON'
>>> s[1] = 'c'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

第一行数字给出字符串正索引点值0…5 。第二行给出相应的负索引。切片是从 i 到 j 两个数值标示的边界之间的所有字符。

对于非负索引,如果两个索引都在边界内,切片长度就是两个索引之差。例如, word[1:3] 是 2 。

切片时,下标溢出不会报错。

>>> word[4:42]
'on'
>>> word[43:42]
''

5.3.2 字符串格式化

  • formatted string literals

使用格式化字符串(formatted string literals)字面值,要在字符串开头的引号/三引号前添加f或F 。在这种字符串中,可以在{和} 字符之间输入引用的变量,或字面值的Python表达式。

>>> year = 2016
>>> event = 'Referendum'
>>> f'Results of the {year} {event}'
'Results of the 2016 Referend
图片
  • str.format()

字符串的str.format()方法需要更多手动操作。该方法也用{和}标记替换变量的位置,虽然这种方法支持详细的格式化指令,但需要提供格式化信息。

>>> yes_votes = 42_572_654
>>> no_votes = 43_132_495
>>> percentage = yes_votes / (yes_votes + no_votes)
>>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes 49.67%'
  • 切片和合并

最后,还可以用字符串切片和合并操作完成字符串处理操作,创建任何排版布局。字符串类型还支持将字符串按给定列宽进行填充,这些方法也很有用。

Python 有办法将任意值转为字符串: repr() 或 str() 函数。

标准模块 string 的Template 类可以替换字符串的值。(python标准模块介绍-string:文本常量和模板)

函数 str() 用于将值转化为适于人阅读的形式,而 repr() 转化为供解释器读取的形式(如果没有相关语法,则会发生 SyntaxError 异常,没有str() 会返回与 repr() 等同的值。很多类型,诸如数值或列表、字典这样的结构,两者解读方式相同。字符串和浮点数则不同。

下面有些例子:

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, worldn'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, worldn'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"

有两种方式可以写平方和立方表:

>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... # Note use of 'end' on previous line
... print(repr(x*x*x).rjust(4))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

(注意第一个例子,print() 在每列之间加了一个空格,默认在参数间加入空格。)

以上是一个 str.rjust() 方法的演示,它把字符串输出到一列,并通过向左侧填充空格来使其右对齐。类似的方法还有 str.ljust() 和 str.center()。如果输出的字符串太长,它们也不会截断它,而是原样输出,这会使你的输出格式变得混乱(如果你确实需要截断它,可以使用切割操作,例如:x.ljust(n)[:n] )。

str.zfill() 用于向数值的字符串表达左侧填充 0。该函数可以正确识别正负号:

>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'

方法 str.format() 的基本用法如下:

>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"

大括号和其中的字符会被替换成传入 str.format() 的参数。大括号中的数值指明使用传入 str.format() 方法的对象中的哪一个:

>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam

如果在 str.format() 使用关键字参数,可以通过参数名来引用值:

>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.

位置参数和关键字参数可以随意组合:

>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
other='Georg'))
The story of Bill, Manfred, and Georg.

'!a' (应用 ascii()),'!s' (应用 str() )和 '!r' (应用 repr() )可在格式化之前转换值:

>>> contents = 'eels'
>>> print('My hovercraft is full of {}.'.format(contents))
My hovercraft is full of eels.
>>> print('My hovercraft is full of {!r}.'.format(contents))
My hovercraft is full of 'eels'.

=说明符可被用于将一个表达式扩展为表达式文本、等号再加表达式求值结果的形式。

>>> bugs = 'roaches'
>>> count = 13
>>> area = 'living room'
>>> print(f'Debugging {bugs=} {count=} {area=}')
Debugging bugs='roaches' count=13 area='living room'

字段名后允许可选的 ':' 和格式指令。这允许对值的格式化更多的控制。下例将 Pi 转为三位精度。

>>> import math
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.

在字段后的 ':' 后面的整数会限定该字段的最小宽度,这在美化表格时很有用:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print('{0:10} ==> {1:10d}'.format(name, phone))
...
Jack ==> 4098
Dcab ==> 7678
Sjoerd ==> 4127

很长的格式化字符串又不想分割,可以传入一个字典,用中括号( '[]' )访问它的键:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
... 'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

也可以用 ‘**’ 标志将这个字典以关键字参数的方式传入:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

这种方式与新的内置函数 vars() 组合使用非常有效。该函数返回包含所有局部变量的字典。

要进一步了解字符串格式化方法 str.format(),参见 格式字符串语法。

  • 旧式的字符串格式化

操作符 % 也可以用于字符串格式化。它以类似 sprintf()-style 的方式解析左参数,将右参数应用于此,得到格式化操作生成的字符串,例如:

>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.

更多的信息可以参见 printf-style String Formatting 。

图片

参考资料

  • 软件测试精品书籍文档下载持续更新 https://github.com/china-testing/python-testing-examples 请点赞,谢谢!
  • 本文涉及的python测试开发库 谢谢点赞! https://github.com/china-testing/python_cn_resouce
  • python精品书籍下载 https://github.com/china-testing/python_cn_resouce/blob/main/python_good_books.md
  • Linux精品书籍下载 https://www.cnblogs.com/testing-/p/17438558.html

5.3.3 字符串处理技巧

  • 连接

连接是将两个不同的实体连接起来。使用算术运算符”+”,可以将两个字符串连接在一起。如果想提高字符串的可读性,只需在两个字符串之间使用空格即可。

>>> 'Today is' + ' a wonderful day'
'Today is a wonderful day'

相邻字符串文本会自动连接,它只用于字符串文本,不能用于字符串表达式和变量(需要使用加号)等:

>>> 'Py' 'thon'
'Python'
>>> prefix = 'P'
>>> prefix 'thon'
File "<stdin>", line 1
prefix 'thon'
^^^^^^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
File "<stdin>", line 1
('un' * 3) 'ium'
^^^^^
SyntaxError: invalid syntax
>>> prefix + 'thon'
'Pthon'
>>> text = ('Put several strings within parentheses '
... 'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

join方法是一种更灵活的串联字符串方式。

>>> s = "China Testing"
>>> print("-".join(s))
C-h-i-n-a- -T-e-s-t-i-n-g
>>> l = ['1','2','3']
>>> print("".join(l)) # 一种列表转换为字符串的方法
123
  • 乘法

使用字符串乘法技术时,字符串值会不断重复。*运算符可用于对字符串内容进行乘法运算。

>>> 'Yes '* 4
'Yes Yes Yes Yes '
>>> 3 * 'un' + 'ium'
'unununium'
  • 追加

您可以使用算术运算符+=将任意字符串添加到另一个字符串的末尾。

>>> example = "Today is a beautiful day "
>>> example += "to start learning Python!"
>>> print (example)
Today is a beautiful day to start learning Python!
  • 长度(len)

除了字符串操作,您还可以使用内置函数在代码中执行其他任务。例如”len()”函数返回字符串中的字符数。

>>> x = 'Tomorrow it will be sunny'
>>> len(x)
25
  • 查找(find)

使用字符串作为主要数据类型时,有时需要查找字符串的特定部分。要解决这个问题,可以使用内置的find()方法。在第一次找到输入时,输出将提供位置索引。

>>> 'Tomorrow it will be sunny'.find('hi')
-1
>>> 'Tomorrow it will be sunny'.find('it')
9

如果未找到子串,解释器将返回值-1。找到只会返回非负索引。

  • 大小写

lower()和higher()方法可用于将字符串中的字符转换为完全小写或大写。要将字符串格式转换为驼峰格式,请使用title()。

>>> "Asia is the biggest continent".lower()
'asia is the biggest continent'
>>> "Asia is the biggest continent".upper()
'ASIA IS THE BIGGEST CONTINENT'
>>> "Asia is the biggest continent".title()
'Asia Is The Biggest Continent'
  • 反转(reversed)
>>> s = "https://china-testing.github.io/"
>>> print(''.join(reversed(s)))
/oi.buhtig.gnitset-anihc//:sptth
>>> s[::-1]
'/oi.buhtig.gnitset-anihc//:sptth'
  • 切割(split)
>>> s = "https://china-testing.github.io/"
>>> s.split('.')
['https://china-testing', 'github', 'io/']
  • 替换

方法replace()返回将旧字符串替换为新字符串的副本。

>>> old = "China Testing"
>>> new = old.replace("Testing", "Python")
>>> print(new)
China Python

5.4 整数

要执行算术运算或提供有关统计值的信息,就需要数值。当Python解释器遇到整数类型的数据值时,它会根据所提供的值创建一个int 对象。因为int对象的值是可变的,所以开发者可以随时替换它们。

开发人员使用”Int”数据类型在软件中创建各种复杂的功能。整数通常用于表示图像或视频文件的像素密度值等。

开发人员必须了解一元运算符 (+,-),它们可分别用于表示正整数和负整数。正整数不需要指定一元运算符(+),但负整数必须包含一元运算符。

>>> x = 13
>>> y = -92
>>> print(x)
13
>>> print(y)
-92

Python可以处理多达十位的数字。虽然大多数实际应用不会因为数值较大而导致瓶颈,但最好确保不涉及大整数。

5.5 浮点数

使用浮点数,您可以处理长达十位小数的十进制数值。

>>> x = 3.121212
>>> y = 58.4545
>>> print(x)
3.121212
>>> print(y)
58.4545

浮点数也可用十六进制表示数据。

>>> x = float.hex(15.2698)
>>> print(x)
0x1.e8a2339c0ebeep+3

5.6 布尔数据类型

布尔是一种特殊的数据类型,在比较两个不同值时,通常用来表示”True”或”False”。

>>> A = 21
>>> B = 55
>>> print (A > B)
False

由于上例中A的值小于B的值,所以输出结果为False。在处理逻辑运算时,布尔数据类型非常有用。

5.7 综合练习1:框住输出内容

请输出如下内容


+---------+
| |
| Hello |
| |
+---------+
  • 参考答案
sentence = input("Sentence: ")

screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) // 2

print()
print(' ' * left_margin + '+' + '-' * (box_width-2) + '+')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '| ' + sentence + ' |')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '+' + '-' * (box_width-2) + '+')
print()

5.8 综合练习2:以千格式化数值

输入1234567890,要求格式化为:’1,234,567,890′

  • 参考答案:
>>> number = 1234567890
>>> f"{number:,}"
'1,234,567,890'

Python 3.6以前

>>> number = 1234567890
>>> format(number, ",")
'1,234,567,890'

更多格式化

>>> number = 1234567890
>>> f"{number:020,.2f}"
'0,001,234,567,890.00'

5.9 字符串试题

1,下面哪个字符串定义有错误?

A,r’C:Program Filesfoobar’
B,r’C:Program Filesfoobar’
C, r’C:Program Filesfoobar’
D,r’C:Program Filesfoobar\’

2,min(‘abcd’)的结果是?

A,a B,b |C,c D,d

2,max(‘abcd3A’)的结果是?
A,a B,3 |C,A D,d

参考答案: B A D

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/360274.html

(0)
联系我们
联系我们
分享本页
返回顶部