数据类型
按照如下几个点展开数据类型的学习python
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#======================================基本使用======================================
#一、用途
#二、定义方式
#三、经常使用操做+内置的方法
#======================================该类型总结====================================
#存一个值or存多个值
#有序or无序
#可变or不可变(一、可变:值变,id不变。可变==不可hash 二、不可变:值变,id就变。不可变==可hash)
|
1、数字(int,float)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
"""
整型int
做用:年纪,等级,身份证号,qq号等整型数字相关
定义:
age=10 #本质age=int(10)
浮点型float
做用:薪资,身高,体重,体质参数等浮点数相关
定义:
salary=9999.9 #本质salary=float(9999.9)
"""
age
=
'18'
print
(
type
(
int
(age)))
# <class 'int'>
salary
=
'123.8'
print
(
type
(
float
(salary)))
# <class 'float'>
print
(
bin
(
16
))
#0b10000 二进制
print
(
oct
(
17
))
#0o21 八进制
print
(
hex
(
18
))
#0x12 十六进制
|
2、字符串(str)
""" 字符串【有序不可变】 做用:名字,性别,国籍,地址等描述信息 定义: name='tom' #本质 name = str('tom') """ # strip name = '*tom**' print(name.strip('*')) # tom print(name.lstrip('*')) # tom** print(name.rstrip('*')) # *tom # lower,upper name = 'tom' print(name.lower()) # tom print(name.upper()) # TOM # startswith,endswith name = 'tom_en' print(name.endswith('en')) # True print(name.startswith('tom')) # True # format的三种玩法 res = '{} {} {}'.format('egon', 18, 'male') res = '{1} {0} {1}'.format('egon', 18, 'male') res = '{name} {age} {sex}'.format(sex='male', name='egon', age=18) # split name = 'root:x:0:0::/root:/bin/bash' print(name.split(':')) # 默认分隔符为空格 ['root', 'x', '0', '0', '', '/root', '/bin/bash'] name = 'C:/a/b/c/d.txt' print(name.split('/', 1)) # 只想拿到顶级目录 ['C:', 'a/b/c/d.txt'] name = 'a|b|c' print(name.rsplit('|', 1)) # 从右开始切分['a|b', 'c'] # join tag = ' ' print(tag.join(['tom', 'say', 'hello', 'world'])) # 可迭代对象必须都是字符串 tom say hello world print('_'.join('abcd')) # a_b_c_d # replace name = 'tom say hello world,tom say bye' print(name.replace('tom', 'rose', 1)) # rose say hello world,tom say bye # find name = 'tom say hello world,tom say bye' print(name.find('jack', 0, len(name))) # -1 (找不到返回-1)查找子串第一次在母串中出现的位置,能够本身指定位置范围来搜查 # count name = 'tom say hello world,tom say bye' print(name.count('tom', 0, len(name))) # 2 计算出子串 'tom'在母串中出现的次数,默认是在整个母串中查找, # isdigit:能够判断bytes和unicode类型,是最经常使用的用于于判断字符是否为"数字"的方法 age = input('>>: ') print(age.isdigit()) print('1'.isdecimal()) # True print('1'.isdigit()) # True print('1'.isnumeric()) # True print('②'.isdecimal()) # False print('②'.isdigit()) # True print('②'.isnumeric()) # True print('二'.isdecimal()) # False print('二'.isdigit()) # False print('二'.isnumeric()) # True print('Ⅳ'.isdecimal()) # False print('Ⅳ'.isdigit()) # False print('Ⅳ'.isnumeric()) # True #罗马数字 print(b'1'.isdigit()) # True # print(b'1'.isdecimal())#报错 # print(b'1'.isnumeric())#True #报错 print('4.3'.isdigit()) # False print('4.3'.isdecimal()) # False print('4.3'.isnumeric()) # False print('-4'.isdigit()) # False print('-4'.isdecimal()) # False print('-4'.isnumeric()) # False num = '-10' if (num.startswith('-') and num[1:] or num).isdigit(): print('num是整数') else: print('num不是整数') num = '-4.5' import re if re.match(r'^-?(\.\d+|\d+(\.\d+)?)', num): print('num是整数') else: print('num不是整数') num = '-10' if num.lstrip('-').isdigit(): print('num是整数') else: print('num不是整数') ''' 总结: 最经常使用的是isdigit,能够判断bytes类型,这也是最多见的数字应用场景 若是要判断中文数字或罗马数字,则须要用到isnumeric '''

def capitalize(self) 首字母大写 def casefold(self) 全部变小写,casefold更强,不少未知的也会相应变小写 def center(self, width, fillchar=None)设置宽度,并将内容居中 def count(self, sub, start=None, end=None)去字符串中寻找子序列的出现次数 def encode(self, encoding='utf-8', errors='strict') def endswith(self, suffix, start=None, end=None)以什么什么结尾 def expandtabs(self, tabsize=8) 断句tabsize的长度 def find(self, sub, start=None, end=None)从开始日后找,找到第一个以后,获取其索引 def format(self, *args, **kwargs)格式化,将一个字符串中的占位符替换为指定的值 def format_map(self, mapping)格式化,传入的值 {"name": 'alex', "a": 19} def index(self, sub, start=None, end=None)找不到,报错 def isalnum(self)字符串中是否只包含 字母和数字 def isalpha(self)是不是字母,汉字 def isdecimal(self)当前输入是不是数字 2 def isdigit(self)②,2 def isidentifier(self) def islower(self) def isnumeric(self)②,2,二 def isprintable(self) def isspace(self) def istitle(self) def isupper(self) def join(self, iterable) def ljust(self, width, fillchar=None) def lower(self) def lstrip(self, chars=None) def maketrans(self, *args, **kwargs) def partition(self, sep)分割为三部分 def replace(self, old, new, count=None)将指定字符串替换为指定字符串 def rfind(self, sub, start=None, end=None) def rindex(self, sub, start=None, end=None) def rjust(self, width, fillchar=None) def rpartition(self, sep) def rsplit(self, sep=None, maxsplit=-1) def rstrip(self, chars=None) def split(self, sep=None, maxsplit=-1) def splitlines(self, keepends=None) def startswith(self, prefix, start=None, end=None) def strip(self, chars=None) def swapcase(self) def title(self) def translate(self, table) def upper(self) def zfill(self, width) # m = str.maketrans("aeiou", "12345") # new_v = v.translate(m)
3、列表(list)
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
|
"""
列表【有序可变】
做用:多个装备,多个爱好,多门课程,多本书籍等
定义:
[]内能够有多个任意类型的值,逗号分隔
"""
# 建立
li
=
[
'a'
,
'b'
,
'cc'
,
4
]
# 定义一个列表通常用这种就能够
# li = list(['a','b','cc',4]) # 定义一个列表
# #增长
li.append(
5
)
# #在列表末尾 添加一个元素,li=['a', 'b', 'cc', 4, 5]
li.insert(
0
,
'g'
)
# #在列表末尾 添加一个元素,li=['g', 'a', 'b', 'cc', 4]
li.extend([
'gg'
,
'kk'
])
# 添加一个列表['gg','kk'], li=['a', 'b', 'cc', 4, 'gg', 'kk']
# # 删除
aa
=
li.pop()
#******pop 删除的时候要保障元素存在,否则会报错******,
print
(li,aa)
# 从尾部删除一个元素,并返回删除的元素 ['a', 'b', 'cc'] 4
aa
=
li.pop(
2
)
# 删除索引为2的元素,并返回删除的元素['a', 'b', 4] cc
aa
=
li.remove(
'a'
)
# 从列表中移除,无返回值 'a',['b', 'cc', 4] None
li.clear()
# 清空列表[]
del
li[-1]
# 删除最后一个
# # 修改
li[
0
]
=
'A'
# ['A', 'b', 'cc', 4]
# # 查找
print
(li.index(
'a'
))
# 运行结果0
# 获取该元素,在列表中的索引,(若是列表中有多个相同的元素,只会取找到的第一个元素的索引。
# 固然也可获取某段范围的索引print(liist1.index('d',2,5)))
# 找不到会报错
print
(li.count(
'a'
))
# 运行结果1 统计列表中有几个a(元素)
# # 其余
li.reverse()
# 反转一个列表,li=[4, 'cc', 'b', 'a']
for
i
in
li:
# 循环输出列表元素
print
(i)
list1
=
[
"a"
,
"c"
,
"b"
,
"e"
,
"d"
]
list1.sort()
# 排序
print
(list1)
# ['a', 'b', 'c', 'd', 'e']
# # python3.x系列的数据类型排序,字符串类型和数字类型不能一块儿进行排序
|
4、元组(tuple)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
"""
元组【有序不可变】
做用:存多个值,对比列表来讲,元组不可变(是能够当作字典的key的),主要是用来读
定义:
与列表类型比,只不过[]换成()
"""
t
=
(
'a'
,)
# 元祖只有一个元素时,须要加逗号, 和字符串区分开
t
=
(
'a'
,
'b'
,
'b'
,
'c'
)
# 定义一个元组
t
=
tuple
((
'a'
,
'b'
,
'b'
,
'c'
))
print
(t.index(
'b'
))
# 索引出元素第一次出现的位置,还能够指定在某一范围里查找,这里默认在整个元组里查找输出1
print
(t.count(
'b'
))
# 计算元素出现的次数,这里输出2
print
(
len
(t))
# 输出远组的长度,这里输出4
print
(t[
1
:
3
])
# 切片 输出('b','b')
for
i
in
t:
print
(i)
# 循环打印出元组数据
|
5、字典(dict)
""" 字典【无序可变】 做用:存多个值,key-value存取,取值速度快 定义: key必须惟一,必须是不可变类型,value能够是任意类型 """ # 建立: info = {"name": "tom", "age": 18, "gender": "male"} # 本质info=dict({"name":"tom","age":18}) # info=dict(name='tom',age=18,gender='male') # info=dict([['name','tom'],('age',18)]) # info={}.fromkeys(('name','age','gender'),None) #{'name': None, 'gender': None, 'age': None} # 增长 info['salary'] = 50000 # {'name': 'tom', 'age': 18, 'salary': 50000, 'gender': 'male'} # 删除 info.pop('age') # 根据键删除某一元素 d={'Michael': 95, 'Tracy': 85} info.popitem() # 随机删除 info.clear() # {} # 修改 info['age'] = '25' # 若是没有该key,则在字典建立新的的的key-value # 查询 info.get('age') # 根据key获取values,若是不存在返回None,这里输出75 ''' setdefault的功能 1:key存在,则不赋值,key不存在则设置默认值 2:key存在,返回的是key对应的已有的值,key不存在,返回的则是要设置的默认值 ''' print(info.setdefault('age', 50000)) # 18 print(info.setdefault('salary', 50000)) # 50000 print(info) # {'age': 18, 'name': 'tom', 'salary': 50000, 'gender': 'male'} # 其余 print(len(info)) # 输出字典长度 print('age' in info) # python3 中移除了 has_key,要判断键是否存在用in for i in info: print(i) # 循环默认按键输出 for i in info.values(): # 循环按值输出 print(i) for k, v in info.items(): # 循环按键值输出 print(k, v) seq = ('Google', 'Runoob', 'Taobao') seq2 = ('1', '2', '3') d1 = dict.fromkeys(seq) d2 = dict.fromkeys(seq,seq2) print(d1) # {'Google': None, 'Taobao': None, 'Runoob': None} print(d2) # {'Taobao': ('1', '2', '3'), 'Google': ('1', '2', '3'), 'Runoob': ('1', '2', '3')}

def clear(self) def copy(self) @staticmethod # known case def fromkeys(*args, **kwargs) def get(self, k, d=None) def items(self) def keys(self) def pop(self, k, d=None) def popitem(self) def setdefault(self, k, d=None) def update(self, E=None, **F) def values(self)
6、集合(set)
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
|
"""
集合【无序可变】
做用:去重,关系运算
定义:
1:每一个元素必须是不可变类型(可hash,可做为字典的key)
2:没有重复的元素
3:无序
4:能够包含多个元素,用逗号分割,
"""
a
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
}
b
=
{
1
,
3
,
9
,
10
,
11
}
a.add(
10
)
# 添加一个元素
a.discard(
1
)
# 删除元素1,不存在的话不报错
a.remove(
1
)
# 删除元素1,不存在的话报错
a.pop()
# 随机删除
a.update([
1
,
8
,
34
])
# 更新,没有就添加,有就不添加
# 并集
a.union(b)
a | b
# 返回一个新的集合包含a和b的全部元素
# 交集
a.intersection(b)
a & b
# 返回一个新的集合包含a和b的公共元素
# 差集
a.difference(b)
a
-
b
# 返回一个新的集合,包含a中的元素,可是没有b中的元素
# 对称差集
a.symmetric_difference(b)
print
(a ^ b)
# 返回一个新的集合包含 a和b中不重复的元素
|
数据类型总结
- 【有序】: 列表,元组
- 【无序】: 字典,集合
- 【可变】:列表,字典,集合
- 【不可变】:数字,字符串,元组
- 【存单值】:数字,字符串
- 【存多值】:列表,元组,字典
其余
1、格式化
1
2
3
4
5
6
7
8
9
10
11
12
13
|
res1
=
'{} {} {}'
.
format
(
'tom'
,
18
,
'male'
)
res2
=
'{1} {0} {1}'
.
format
(
'tom'
,
18
,
'male'
)
res3
=
'{name} {age} {gender}'
.
format
(gender
=
'male'
,name
=
'tom'
,age
=
18
)
res4
=
'{name} {age} {gender}'
.
format
(
*
*
{
"name"
:
"tom"
,
"age"
:
18
,
"gender"
:
"male"
})
print
(res1)
#tom 18 male
print
(res2)
#18 egon 18
print
(res3)
#tom 18 male
print
(res4)
#tom 18 male
msg
=
'i am %s my hobby is %s'
%
(
'seven'
,
'paly'
)
tpl
=
"%(name)s age %(age)d"
%
{
"name"
:
"tom"
,
"age"
:
18
}
print
(msg)
#i am seven my hobby is paly
print
(tpl)
#tom age 18
|
2、切片
1
2
3
4
5
6
7
8
9
10
11
12
|
s
=
'abcdefghigk'
print
(s[
0
:
3
])
#截取第一位到第三位的字符 #abc
print
(s[:] )
#截取字符串的所有字符 #abcdefghigk
print
(s[
6
:])
#截取第七个字符到结尾 #ghigk
print
(s[:
-
3
] )
#截取从头开始到倒数第三个字符以前 #abcdefgh
print
(s[
2
])
#截取第三个字符 #c
print
(s[
-
1
] )
#截取倒数第一个字符 #k
print
(s[::
-
1
])
#创造一个与原字符串顺序相反的字符串 #kgihgfedcba
print
(s[
-
3
:
-
1
] )
#截取倒数第三位与倒数第一位以前的字符 #ig
print
(s[
-
3
:])
#截取倒数第三位到结尾 #igk
print
(s[
0
:
10
:
2
])
#每隔一个,取一个值 #acegi
print
(s[
0
:
10
:
3
])
#每隔2个,取一个值 #adgg
|
三 、enumerate
为一个可迭代的对象添加序号,可迭代的对象你能够理解成能用for循环的就是可迭代的。默认是编号是从0开始,能够设置从1开始git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
user
=
[
'tom'
,
'rose'
,
'jack'
]
for
k,v
in
enumerate
(user):
print
(k,v)
"""
tom
rose
jack
"""
user
=
[
'tom'
,
'rose'
,
'jack'
]
for
k,v
in
enumerate
(user,
25
):
print
(k,v)
"""
tom
rose
jack
"""
|
四 、三目运算
三目运算符能够简化条件语句的缩写,可使代码看起来更加简洁,三目能够简单的理解为有三个变量,它的形式是这样的 name= k1 if 条件 else k2 ,若是条件成立,则 name=k1,不然name=k2,下面从代码里面来加深一下理解,从下面的代码明显能够看出三目运算符可使代码更加简洁。python3.x
1
2
3
4
5
6
7
8
|
a
=
1
b
=
2
if
a<b:
#通常条件语句的写法
k
=
a
else
:
k
=
b
c
=
a
if
a<b
else
b
#三目运算符的写法
|
5、浅copy和深copy
对于字典、列表等数据结构,深拷贝和浅拷贝有区别,从字面上来讲,能够看出深拷贝能够彻底拷贝,浅拷贝则没有彻底拷贝。api
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
|
#字典只有顶级对象(源变了,深浅copy没变)
import
copy
#导入copy模块
info
=
{
'name'
:
'tom'
,
'age'
:
18
}
#原始字典
info_copy
=
copy.copy(info)
#浅拷贝
info_deep
=
copy.deepcopy(info)
#深拷贝
print
(info)
print
(info_copy)
print
(info_deep)
id
(info);
id
(info_copy);
id
(info_deep)
#3个不一样的对象,id不同
info[
'age'
]
=
19
#源变了,深浅copy没变
#字典嵌套可变对象 (源和浅copy变了,深copy没变)
import
copy
#导入copy模块
info
=
{
'name'
:
'tom'
,
'age'
:
18
,
'job'
:[
'it'
,
'design'
]}
#原始字典
info_copy
=
copy.copy(info)
#浅拷贝
info_deep
=
copy.deepcopy(info)
#深拷贝
id
(info);
id
(info_copy);
id
(info_deep)
#3个不一样的对象,id不同
info[
'job'
][
0
]
=
'boss'
#源和浅copy变了,深copy没变
print
(info)
#{'age': 18, 'job': ['boss', 'design'], 'name': 'tom'}
print
(info_copy)
#{'age': 18, 'job': ['boss', 'design'], 'name': 'tom'}
print
(info_deep)
#{'age': 18, 'job': ['it', 'design'], 'name': 'tom'}
'''
深浅copy都是对源对象的复制,占用不一样的内存空间。
若是源对象只有一级目录的话,源作任何改动,不影响深浅拷贝对象
若是对象不止一级目录,源作任何改动,都要影响浅拷贝,但不影响深 拷贝
'''
|