数据类型的基础操作

字符串

--定义 :

​ 在单引号\双引号\三引号内,由一串字符组成,

​ 引号嵌套,双加单

--按索引取值(正向取+反向取) :只能取,

​ 从那个取:取到第几个:步长

a = 'qwertyp'
print(a[1])
print(a[-1])
print(a[:3])
print(a[1:])
print(a[1:4])
print(a[1:5:2])

===>
w
p
qwe
wertyp
wer
wr
--长度len
a = 'qwer/typ'
print(len(a))

====>
8

--移除空白

​ strip,移除两边的空格

a = ' qwer/typ '
print(a.strip( ))

====>
qwer/typ
--成员运算

in 和 not in

a = 'qwertyp'
print('a' in a)
print('q' in a)

====>
False
True
--循环

end = ' '这个可以让循环到的在一行显示

a = 'qwertyp'
for i in a:
    print(i)
====>
q
w
e
r
t
y
p

--------------------------------------------------
a = 'qwertyp'
for i in a:
    print(i,end='')
    
 ====>
 qwertyp

-- startswith/endswith

​ 以什么开头/以什么结尾

b='hello python15 welcome to shanghai'
print(b.startswith('hello'))
print(b.endswith('shang'))

====>
True
False
--strip,lstrip,resrip

​ strip:移除两边,默认移除空白字符(空格,/n,/t),可以指定字符

​ lstrip:移除左边,默认移除空白字符(空格,/n,/t),可以指定字符

​ rstrip:移除右边边,默认移除空白字符(空格,/n,/t),可以指定字符

b = '$$$qwe$$$'
print(b.strip('$'))
print(b.lstrip('$'))
print(b.rstrip('$'))
====>
qwe
qwe$$$
$$$qwe
--lower,upper

转小写,转大写

a = 'qwertyp'
print(a.upper())

====>
QWERTYP
--split

split切分,默认空格,可以指定字符切分,后面可以跟参数,即切分几次,得到的是列表

a = 'qwwe/qwe/qwe/qwe'
print(a.split('/'))
print(a.split('/',1))
===>
['qwwe', 'qwe', 'qwe', 'qwe']
['qwwe', 'qwe/qwe/qwe']
--join

​ 拼接

import os
print(os.path.join(a,'zxc'))

===>
qwertyp\zxc

---------------------------------------------------------
b='hello python15 welcome to shanghai'
print(b.join('xx'))

======>
xhello python15 welcome to shanghaix

---------------------------------------------------------
b='hello python15 welcome to shanghai'
print(b + '/' +'lyh')

====>
hello python15 welcome to shanghai/lyh
--replace替换
a = 'qwertyp'
print(a.replace('q','z'))

====>

zwertyp


--isdigit

判断是不是int类型

b = 'cvb'
c = '12'
print(b.isdigit())
print(c.isdigit())

====>
False
True

列表

--按索引取值

​ 顾头不顾尾

a = ['123','qwe','%66','a']
print(a[0])
print(a[-1])
print(a[1:2])
print(a[1:3:1])

=====>
123
a
['qwe']
['qwe', '%66']

数据类型的基础操作

--切片

​ 列表没有split切分,,列表切分就用索引去拿

a = ['123','qwe','%66','a']
print(a[1:2])
print(a[1:3:1])

=====>
['qwe']
['qwe', '%66']
--长度
a = ['123','qwe','%66','a']
print(len(a))

======>
4
--成员运算

​ in 和 not in

a = ['123','qwe','%66','a']
print('123' in a)
print('123' not in a)
print('lyh' in a)

====>
True
False
False
--追加

​ append:只能添加一个

​ extend:可以添加多个,不过要以列表形式传入

​ insert:只能添加一个,不过可以指定位置,insert(索引,‘字符串’)

a = ['123','qwe','%66','a']
a.append('lyh')
print(a)

====>
['123', 'qwe', '%66', 'a', 'lyh']
--删除

del

​ del用索引删

a = ['123','qwe','%66','a']
del a[1]
print(a)
====>
['123', '%66', 'a']

clear 列表点clear清除

a = ['123','qwe','%66','a']
b = a
print('clear前', b)
b.clear()
print('clear后', b)

=====>
clear前 ['123', 'qwe', '%66', 'a']
clear后 []

remove

​ remove移除,remove(’元素‘)

a = ['123','qwe','%66','a']
a.remove('123')
print(a)

===>
['qwe', '%66', 'a']
--循环
a = ['123','qwe','%66','a']
print(a)
for i in a:
    print(i)
    
====>
123
qwe
%66
a
---------------------------------------
a = ['123','qwe','%66','a']
for i in range(len(a)):
    print(i)
    
===>  取索引
0
1
2
3

--枚举(enumerate)
a = ['123','qwe','%66','a']
for index, i in enumerate(a):
    print('编号: %s   值: %s' %(index,i))
    
====>
编号: 0   值: 123
编号: 1   值: qwe
编号: 2   值: %66
编号: 3   值: a
--count

​ count 用来统计一个元素在列表中出现的次数

a = ['a','123','qwe','%66','a','a']
print(a.count('a'))
===>
3

元组

​ 元组可以存任意任意类型的多个值,元组不可变,主要用来读,元组的操作和列表类似,除了元组不能删除单一值,其他操作都一样,元组只能整个全部删掉,不可重复。

--按索引取值
a=('qwe',['1','2','asd'],{'qwe':123})
print(a[1])
print(a[-1])
====>
['1', '2', 'asd']
{'qwe': 123}
--切片
a=('qwe',['1','2','asd'],{'qwe':123},'asd','123')
print(a[1:2])
print(a[1:4])
print(a[1:4:2])
=====>
(['1', '2', 'asd'],)
(['1', '2', 'asd'], {'qwe': 123}, 'asd')
(['1', '2', 'asd'], 'asd')
--长度
a=('qwe',['1','2','asd'],{'qwe':123},'asd','123')
print(len(a))
===>
5

--成员运算

​ in | not in

a=('qwe',['1','2','asd'],{'qwe':123},'asd','123')
print('qwe' in a)
print('qwe' not in a)
--循环
a=('qwe',['1','2','asd'],{'qwe':123},'asd','123')
for i in a:
    print(i)
    
====>
qwe
['1', '2', 'asd']
{'qwe': 123}
asd
123

字典

--按key存取值
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}

print(a['qwe'])
===>
123
---------------------------------------------------------
改:
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
a['zxc'] = 789
print(a)
=====>
{'qwe': 123, 'asd': 345, 'zxc': 789, 'ert': 688}
-------------------------------------------------------------
增:
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
a['123'] = 'qwe'
print(a)
============>
{'qwe': 123, 'asd': 345, 'zxc': 456, 'ert': 688, '123': 'qwe'}
--长度
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
print(len(a))

====>
4

--删除
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
print('删除前',a)
del a['asd']
print('删除后',a)

====>
删除前 {'qwe': 123, 'asd': 345, 'zxc': 456, 'ert': 688}
删除后 {'qwe': 123, 'zxc': 456, 'ert': 688}

--循环(key,values,items)
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
for key in a.keys():
    print(key)
    
 =====>
qwe
asd
zxc
ert
-----------------------------------------------------
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
for value in a.values():
    print(value)
====>
123
345
456
688
----------------------------------------------------------
a = {
    'qwe':123,
    'asd':345,
    "zxc":456,
    'ert':688
}
for k,v in a.items():
    print(k,v)
====>
qwe 123
asd 345
zxc 456
ert 688

集合

​ 可变类型不可hash

​ 不可变类型可hash

​ 可以包含多个元素,每个元素是不可变类型,

集合不可重复

​ 集合用{} 定义:a = set()

--长度(len)
--成员运算(in | not in)
--集合可以用数学里面的集合的运算符
并集(|)
a = set()
b = set()
a={1,23,34,2352,15,35}
b={1,23,34,2352,15,35,12,345,56}
print(a | b)
=====>
{1, 34, 35, 12, 15, 2352, 23, 56, 345}

交集(&)
a = set()
b = set()
a={1,23,34,2352,15,35}
b={1,23,34,2352,15,35,12,345,56}
print(a & b)
=====>
{1, 34, 35, 15, 2352, 23}

差集(-)
a = set()
b = set()
a={1,23,34,2352,15,35}
b={1,23,34,2352,15,35,12,345,56}
print(b - a)
====>
{56, 345, 12}

对称差集(^)
a = set()
b = set()
a={1,23,34,2352,15,35}
b={1,23,34,2352,15,35,12,345,56}
print(b ^ a)
=====>
{56, 345, 12}


整数(int)

​ 整型和浮点数可以混着玩,可以加减乘除。(int,float)

布尔类型(bool)

​ true | False

补充:

​ type取类型,id取存在内存里的值id

上一篇:1.搭建Redis运行环境


下一篇:P1559 运动员最佳匹配问题 by hyl 天梦