文章目录
标准数据类型
python中包含以下六个标准数据类型:
不可变数据:Number、String、Tuple
可变数据:List、Dictionary、Set
1、Number
Number中又包含<int、float、bool、complex>
1.1、int 和 float
在计算机内部存储中,int 和 float 的存储方式不同,int 运算永远是精确的,而 float 运算会存在微小的误差。
算术运算符及其描述 | 实例 |
---|---|
/(普通除法) | 10 / 3 结果是 :3.3333333333333335 |
//(整除,向下取整) | 10 // 3 结果是 :3;-10//3 结果是 :- 4 |
%(取余) | 10 % 3 的结果是 :1 |
**(幂运算) | 2 ** 2 结果是 :4;2 ** 5 结果是 :32 |
1.2、bool
一个布尔值只有 True 和 False 两种值,布尔值可以使用and、or、not 逻辑运算符,通常在代码中 1 代表 True,0 代表 False。
>>> True == 1
True
>>> False == 0
True
1.3、complex
复数由虚部(imag)和实部(real)构成,real可以为int、long、float或字符串类型;而image只能为int、long、或float类型。
real + imag
'定义一个复数的方式一'
>>> a = 2.3 + 5.0j #定义一个复数
>>> print(a) #输出复数a
(2.3+5j)
>>> print(a.real) #输出实部
2.3
>>> print(a.imag) #输出虚部
5.0
'定义一个复数方式二'
>>> a = complex(6, 0.7) #用complex()函数定义一个复数
>>> a
(6+0.7j)
>>> a = complex(6+0.7j)
>>> a
(6+0.7j)
>>> a = complex('1') #real可以用字符类型,但仅当没有imag部分时才行
>>> a
(1+0j)
2、String
python编程中存在着大量的字符串处理工作,python3对字符串作了明确的解释:字符串是由Unicode码点组成的不可变序列(Strings are immutable sequences of Unicode code points.)
由此可见,字符串是一中特殊的序列,这意味着字符串支持序列类型的相关操作。
>>> s = 'Life is priceless'
>>> s[8:] #字符串s可以使用list的切片操作
'priceless'
>>> s[0] = 'l' #注意字符类型是不可变数据类型
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
2.1、字符转义
转义字符 | 描述 |
---|---|
\(在尾行时) | 续行,表示该句没有结束,续写到下一行 |
\b | 退格 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\000 | 空 |
2.2、字符串运算符
操作符 | 描述 |
---|---|
+ | 字符串连接 |
* | 重复输出 |
[ ] \ [:] | 索引和切片 |
in \ not in | 判断是否包含指定字符 |
r \ R | 不转义 |
>>> a, b = 'hello', 'word'
>>> print(a + ' ' + b)
hello word
>>> print(a*2)
hellohello
>>> print(a[2] + b[1:])
lord
>>> 'ell' in a
True
>>> 'oor' not in b
True
>>> print(r'helo\nword!')
helo\nword!
2.3、常用的字符串内建函数
-
center()
语法:str.center(width[, fillchar])
参数:
width -指定字符串总宽度,包含要填充的字符宽度。
fillchar -指定要填充的字符,必须是字符类型,不指定则默认空格。
返回值:返回一个指定宽度居中的字符串,如果width小于str,则返回str。
>>> print('something just like this'.center(50,'*'))
*************something just like this*************
>>> print('something just like this'.center(5,'*')) #width > str
something just like this
-
count( )
语法:str.count(sub[, start = 0, end = len(sting)])
参数:
sub -要搜索的子字符串
返回值:返回sub在str中出现的次数
>>> str = 'I will become an excellent engineer!'
>>> print('e出现的次数:',str.count('e'))
e出现的次数: 8
>>> print('e出现的次数:',str.count('e',20)) #指定start
e出现的次数: 5
>>> print('e出现的次数:',str.count('e',20,25)) #指定start和end
e出现的次数: 2
>>> print('Iron Man出现的次数:',str.count('Iron Man'))
Iron Man出现的次数: 0
-
split( )
语法:str.split(str = ’ '[, num = string.counter(str)])
参数:
str -分隔符,默认空白符,如空格、换行(\n)、制表符(\t)等。
num -分割次数,默认分割整个字符串。
返回值:返回分割后的字符串列表
>>> str = 'mp.youkuaiyun.com'
>>> temp = str.split('.') #默认分割次数
>>> temp
['mp', 'csdn', 'net']
>>> print(str.split('.', 1)) #指定分割次数
['mp', 'youkuaiyun.com']
>>> print( 'i am Iron Man'.split()) #不指定分隔符,使用默认空白符
['i', 'am', 'Iron', 'Man']
-
join( )
语法:str.join(sequence)
参数:sequence -要连接的元素序列
返回值:返回通过指定字符’str‘连接序列中的元素后生成的新字符串
seq = []
for i in range(10):
seq.append(str(i)) #str(i)将i转换为字符型
print('seq =',seq)
join_result = '——>'.join(seq) #用'——>'连列seq中的字符元素
print('join_result =',join_result)
print('join_result类型是:',type(join_result))
输出结果为
seq = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
join_result = 0——>1——>2——>3——>4——>5——>6——>7——>8——>9
join_result类型是: <class 'str'>
-
lstrip()、rstrip()、strip( )
语法:str.lstrip([chars])
参数:[chars]是截取字符,可以指定;若不指定,则默认截取空白符
返回值:返回被截掉左边的空白符或者指定字符之后生成的新字符
rstrip()是从右边截取,strip()是lstrip()和rstrip()的共同作用效果。
-
find()、index()、rfind()、rindex()
find()语法:str.find(str[s, statr = 0, end = len(string)])
参数:strs -指定检索的字符串
返回值:若子字符串存在,返回子字符串的开始位置,不存在则返回-1
index()语法:
str.index(strs[, start = o, end = len(string)])
参数:strs- 指定检索字符串
返回值:若子字符串存在,返回子字符串的开始位置,不存在则返回异常
>>> fruits = 'orange lemon mango strawberry'
>>> fruits.find('lemon')
7
>>> fruits.find('apple')
-1 # apple不存在返回-1
>>> fruits.index('strawberry')
19
>>> fruits.index('banana') # banana不存在,报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- str.endswith(suffix[, start[, end]])
判断是否以指定的字符串suffix结尾。 - str.startswith(suffix[, start[, end]])
判断是否以指定的字符串suffix开头。
- str.partition(sep)
在 sep 首次出现的位置拆分字符串,返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身,以及分隔符之后的部分。 如果分隔符未找到,则返回的 3 元组中包含字符本身以及两个空字符串。 - str.rpartition(sep)
在 sep 最后一次出现的位置拆分字符串.
- str.replace(old, new[, count])
返回字符串的副本,其中出现的所有子字符串 old 都将被替换为 new。 如果给出了可选参数 count,则只替换前 count 次出现。
3、List
3.1、list操作符
基本操作:< [ ] , [:] , + , * , in , for…in…>
3.2、list常用方法
len(list)、max(list)、min(list)、list(sequence)
3.3、list其他方法
class list(object)
| list(iterable=(), /)
|
| Built-in mutable sequence.
|
| If no argument is given, the constructor creates a new empty list.
| The argument must be an iterable if specified.
|
| Methods defined here:
...省略部分Methods...
| append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| insert(self, index, object, /)
| Insert object before index.
|
| pop(self, index=-1, /)
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None, reverse=False)
| Stable sort *IN PLACE*.
注意:在list.sort()方法中,只能对同一数据类型进行排序,不支持对如包含 int 和 str 混合列表进行排序。修改默认参数reverse=True,产生的效果是逆序排序。
3.4、list生成式
有时候我们需要生成一个有规律列表,可以这么做:
>>> [x*x for x in range(0,101,10)]
[0, 100, 400, 900, 1600, 2500, 3600, 4900, 6400, 8100, 10000]
>>> [x for x in range(20) if x%2 == 0]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> [[x,x**3] for x in range(6)]
[[0, 0], [1, 1], [2, 8], [3, 27], [4, 64], [5, 125]]
>>> [i + j for i in 'ABC' for j in 'XYZ']
['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
4、Tuple
tuple(元组)是一种一旦初始化就无法修改的序列,它能够使用的的操作符和内置函数与list完全相同,但是却没有append、insert、pop、remove等这些能够修改序列的函数方法,删除一个tuple要用del方法。
注意:定义一个只有一个元素的tuple,如下:
>>> a = (1) #定义的 a 是 int 类型
>>> isinstance(a,tuple) #判断变量类型的函数
False
>>> b = (1,) #定义的 b 是 tuple 类型
>>> isinstance(b,tuple)
True
>>> isinstance(a,int)
True
5、Dict
dict(dictionary),使用“key:value”的存储方式,在存储数据时,根据key去计算value的存储位置,取的时候直接根据key拿到value,因此具有极快的查找速度。
与list相比,dict是用空间来换取时间的一种方法。
由于dict是根据key来计算value的位置,为了确保每次通过同一个key查找到的value是相同的,key必须是不可变对象。
5.1、创建一个dict
方式一:
>>> dict_one = {'Harry Potter':'Gryffindor', 'Voldemort':'Slytherin'}
>>> dict_one
{'Harry Potter': 'Gryffindor', 'Voldemort': 'Slytherin'}
方式二:
>>> dict_two = {}
>>> dict_two['Harry Potter'] = 'Gryffindor'
>>> dict_two['Voldemort'] = 'Slytherin'
>>> dict_two
{'Harry Potter': 'Gryffindor', 'Voldemort': 'Slytherin'}
注意:初始化一个dict时,key可以重复,但是使用 d[key] 索引时得到的value是最后一个key所对应的value。
5.2、字典的增、删、修改
>>> d = {'name':'Ed Sheeran', 'age':29, 'Nationalitty':'England'}
>>> d['Profession'] = 'singer' #增加键值对
>>> d['age'] = 30 #修改‘age’对应的值
>>> d
{'name': 'Ed Sheeran', 'age': 30, 'Nationalitty': 'England', 'Profession': 'singer'}
>>> d.pop('Nationalitty') #用内置方法pop删除键值对
'England' #返回已经被删除的key所对应的value
>>> d
{'name': 'Ed Sheeran', 'age': 30, 'Profession': 'singer'}
>>> d.pop('Geender','不存在') #pop一个不存在的key,并指定返回的错误信息
'不存在'
>>> del d['age'] #用del方式删除键值对,没有返回结果
>>> d
{'name': 'Ed Sheeran', 'Profession': 'singer'}
>>> d.clear() #clear清空字典
>>> d
{}
>>> del d #用del + '字典对象' 删除字典
>>> d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined #字典d被删除,不存在
5.3、迭代在字典中的key和value
d = {'小明':40, '小胖':30, '小白':35}
for k in d.keys():
print(k)
for v in d.values():
print(v)
for k,v in d.items():
print('%s:%s' % (k,v))
终端输出:
小明
小胖
小白
40
30
35
小明:40
小胖:30
小白:35
5.4、dict的函数方法
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
...部分省略...
| clear(...)
| D.clear() -> None. Remove all items from D.
|
| copy(...)
| D.copy() -> a shallow copy of D
|
| get(self, key, default=None, /)
| Return the value for key if key is in the dictionary, else default.
|
| items(...)
| D.items() -> a set-like object providing a view on D's items
|
| keys(...)
| D.keys() -> a set-like object providing a view on D's keys
|
| pop(...)
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised
|
| popitem(...)
| D.popitem() -> (k, v), remove and return some (key, value) pair as a
| 2-tuple; but raise KeyError if D is empty.
|
| update(...)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
| If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
6、Set
set(集合)是一个无序的没有重复元素的序列
6.1、创建一个set
>>> set_one = {'a', 'b', 'c'} #方式一
>>> lis = [x for x in range(1,10,2)]
>>> set_two = set(lis) #方式二
>>> print('set_one:{0}\nlis:{1}\nset_two:{2}'.format(set_one,lis,set_two))
set_one:{'a', 'b', 'c'}
lis:[1, 3, 5, 7, 9]
set_two:{1, 3, 5, 7, 9}
>>> set_three = set() #创建一个空集合必须用set()方法,因为set_three={}创建的是一个空字典,而不是空集合
>>> type(set_three)
<class 'set'>
关于字典和集合的无序问题
请看这篇回答 —> 关于python中set与dict的无序问题
6.2、集合间的运算
>>> set_one = {x for x in 'Python'} #set也支持类似于列表推导式的操作
>>> set_two = {y for y in 'python123'}
>>> set_one
{'o', 'n', 'y', 'P', 't', 'h'}
>>> set_two
{'o', '1', 'n', 'y', 'p', '3', '2', 't', 'h'}
>>> set_two & set_one #交集,两集合中共同的元素
{'o', 'n', 'y', 't', 'h'}
>>> set_two | set_one #并集,两集合中所有的元素
{'o', '1', 'n', 'y', 'p', '3', 'P', '2', 't', 'h'}
>>> set_two - set_one #属于set_two却不属于set_one
{'2', 'p', '1', '3'}
>>> set_two ^ set_one #不同时存在于两集合中的元素
{'1', '3', 'p', 'P', '2'}
6.3、set的增、删、修改等基本操作
>>> s1 = {'张三', '法外狂徒'}
>>> s2 = {'李四', '隔壁老王'}
>>> s1.add('姗姗来迟') #增加元素
>>> s1
{'张三', '姗姗来迟', '法外狂徒'}
>>> s1.remove('姗姗来迟') #删除元素,若不存在,会报错
>>> s1
{'张三', '法外狂徒'}
>>> s1.update(s2) #一次性添加多个元素,添加对象还可以是元组、字典、列表等
>>> s1
{'李四', '法外狂徒', '张三', '隔壁老王'}
>>> s1.pop() #随机删除一个元素
'李四'
>>> s1.clear() #清空集合
>>> s1
set()
>>> del s1
>>> s1 #删除该集合
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's1' is not defined
7、 可变类型和不可变类型
我们把数据赋给某个变量时(如‘x=1’),内存中会给这个数据对象开辟一块空间,这块空间有自己的 id 号(门牌号),并且让变量 x 指向这个 id 。
不可变类型()有: [ int、 float ] 数值 、str 字符串 、tuple 元组。
>>> x = 1
>>> id(x)
140721977791296
>>> y = 1
>>> id(y)
140721977791296
>>> x = 2
>>> id(x)
140721977791328
观察上面id地址的变化,赋给变量x和变量y的数值1是不可变的,所以x和y指向的是同一个地址,即指向了同一个数据对象。当 赋值 x=2 时,内存中开辟了另一块空间用于存储新的数据对象,变量x指向新的门牌号,但数据1并没有被修改,变量y还是指向的1。
也就是说,对于不可变数据类型,它的值是不可被修改的,“x=1, x=2” 这种操作只是让变量指向了新的数据,没有起到修改数值的作用。
可变类型()有: list 列表 、dict 字典。
>>> m = [1,2,3]
>>> id(m)
2817931317640
>>> n = [1,2,3]
>>> id(n)
2817931337864
>>> m[0]=666
>>> m
[666, 2, 3]
>>> id(m)
2817931317640
观察上面id地址,看似把“同一个list——[1, 2, 3]”赋值给了变量m和变量n,但其实是将两个值相同的list分别赋予了m和n。可变数据类型一旦被赋值给变量之后,就相当于把id地址跟变量绑定了,通过变量可以修改这个可变数据对象内部的值。就像上例中的m,修改了数据的值之后,依然指向原先的id地址。
8、 Boolean 布尔值与布尔操作符
布尔值: True 或 False。
逻辑运算也称为bool运算,逻辑运算符有三个,分别是and、or和not。
布尔运算符 and 和 or 也被称为 短路运算符:它们的参数从左至右解析,一旦可以确定结果解析就会停止。
- 如果
and
左边的布尔值是False,不管右边的布尔值是什么,最终的结果都是False,所以在做运算的时候右边的值会被跳过(短路处理),这也就意味着在and运算符左边为False的情况下,右边的表达式根本不会执行。 or
运算符也是有短路功能的,在它左边的布尔值为True的情况下,右边的表达式根本不会执行。not
的优先级比非布尔运算符低,因此not a == b
会被解读为not (a == b)
而a == not b
会引发语法错误。
9、 字符串格式化输出
除了 print( )
函数,python还提供了其它非常有特色的格式化输出方式。
f
或 F
方式
在字符串前面加上f
或 F
,并将表达式包含在花括号里 {expression}
:
>>> a=1;b=2;
>>> f'a + b = {a + b}'
'a + b = 3'
str.format( )
方式
Format strings 包含有以花括号{}
括起来的“替换字段”,不在{}
之内的内容被视为字面文本,会不加修改地复制到输出中。
{ }
中的替换字段有如下选项:
replacement_field ::= "{" [field_name] [":" format_spec] "}"
- field_name 位置参数的序号,或者是“命名关键字参数”的名字。
- [":" format_spec] 输出的格式控制。
format_spec ::= [[fill] [align] [sign] [width] [grouping_option] [type] ]
fill ::= <any character> # 填充
align ::= "<" | ">" | "=" | "^" # 向左向右居中
sign ::= "+" | "-" | " " # 在数值前面添加正负号
width ::= digit+ # 指定输出宽度
grouping_option ::= "," # 数值类型的千分符号
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
如果指定了一个有效的 align
值 (对齐),则可以在该值前面加 一个 fill
字符,它可以为任意字符,如果省略则默认为空格符。
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1 本版以上
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> "姓名:{f_name},{s_name}".format(f_name="张", s_name="三")
'姓名:张,三'
>>> '{:<30}'.format('left aligned') # 左对齐
'left aligned '
>>> '{:>30}'.format('right aligned') # 右对齐
' right aligned'
>>> '{:*^30}'.format('centered') # 居中
'***********centered***********'
>>> '{:,}'.format(1234567890) # 千位分隔符
'1,234,567,890