Python数据类型

数据类型

按照以下几个点展开数据类型的学习

1
2
3
4
5
6
7
8
9
10
11
12
13
#======================================基本使用======================================
#1、用途
 
#2、定义方式
 
#3、常用操作+内置的方法
 
#======================================该类型总结====================================
#存一个值or存多个值
     
#有序or无序
 
#可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

一、数字(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 十六进制

二、字符串(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)
字符串函数

三、列表(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系列的数据类型排序,字符串类型和数字类型不能一起进行排序

四、元组(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)  # 循环打印出元组数据

五、字典(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)
字典函数

六、集合(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
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

二、切片

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开始

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,下面从代码里面来加深一下理解,从下面的代码明显可以看出三目运算符可以使代码更加简洁。

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         #三目运算符的写法

五、浅copy和深copy

对于字典、列表等数据结构,深拷贝和浅拷贝有区别,从字面上来说,可以看出深拷贝可以完全拷贝,浅拷贝则没有完全拷贝。

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都是对源对象的复制,占用不同的内存空间。
如果源对象只有一级目录的话,源做任何改动,不影响深浅拷贝对象
如果对象不止一级目录,源做任何改动,都要影响浅拷贝,但不影响深 拷贝
'''
"""
小数据池:为了快
在python中,默认会把数字,字符串,布尔值进行缓存


控制台的时候,代码块与代码块之间的规则:
    对于数字:-5~256是会被加到小数据池中的,每次使用都是同一个对象
    对于字符串:
    1.如果字符串的长度是0或者1都会默认进行缓存
    2.字符串长度大于1,但是字符串中只包含字母,数字,下划线时才会缓存
    3.用乘法得到的字符串
        3.1乘数为1,仅包含数字,字母,下划线时会被缓存,如果包含其他字符,而长度<=1也会被驻存
        3.2乘数大于1,仅包含数字,字母,下划线这个时候会被缓存,但乘完后字符串长度不能大于20
    4.指定驻留,可以通过sys模块中的intern()函数来指定要驻留的内容    
    对于布尔值

pycharm中:py文件里,一个代码块内部,也有自己的小数据池,缓存的范围会比外面的代码块的数据池要大
    数字:基本都会缓存,运算结果不缓存 (-5~256会缓存)
    字符串:默认都缓存,如果有乘法依然遵循上方的结论
    布尔值:默认缓存
"""
from sys import intern
a=intern('bubu@qq.com')
b=intern('bubu@qq.com')
print(a is b)


a=1000
b=1000
print(a is b)
# 在py文件中,得到的结果时True,但是在command中就不是了。

"""
在代码块内的缓存机制是不一样的,在执行同一个代码块的初始化对象的命令时,会检查是否其值是否已经存在,
如果存在会将其重用。换句话说:执行同一个代码块时,遇到初始化对象的命令时,他会将初始化的这个变量与值存储在一个字典中,
在遇到新的变量时,会先在字典中查询记录,如果有同样的记录那么它会重复使用这个字典中的之前的这个值。
所以文件执行时(同一个代码块)会把a,b两个变量指向同一个对象。
如果是不同的代码块,他就会看这两个变量是否是满足小数据池的数据,如果是满足小数据池的数据则会指向同一个地址。
所以a,b的赋值语句分别被当作两个代码块执行,但是他们不满足小数据池的数据所以得到两个不同的对象,因而is判断返回False
"""
小数据池

 

posted @ 2018-12-23 23:03  silencio。  阅读(757)  评论(0编辑  收藏  举报