一、基本数据类型
1.字符串
类:str
方法:选中str,按住command(ctrl)+左键跳转至对应的方法
- 创建
a = "hexin"
a = str('hexin')
- 转换
age = 19
new_age = str(age)
- 字符串的拼接
name = 'hexin'
gender = '女'
new_str = name + gender
print(new_str)
- 字符串格式化 占位符
# name = '我叫李杰,性别:%s,我今年%s岁,我在说谎!'
# new_str = name %('男',19,)
# print(new_str)
name = '我叫李杰,性别:%s,我今年%s岁,我在说谎!' %('男',19,)
print(name)
判断子序列是否在其中
content = "123 前几天去泰国玩姑娘,一不小心染上了病,他的内心活动是,真该多来几个"
if "前几天去" in content:
print('包含敏感字符')
else:
print(content)
- 移除空白 strip
a = ' he xin '
print(a)
new_a=a.strip() #左右
new_a1=a.lstrip() #左
new_a2=a.rstrip() #右
print(new_a)
print(new_a1)
print(new_a2)
输出
he xin
he xin
he xin
he xin
- 分割
user_info = "ex|in s123 9"
v1 = user_info.split('|')
v2 = user_info.split('|',1)
v3 = user_info.rsplit(' ',1)
print(v1)
print(v2)
print(v3)
输出
['ex', 'in s123 9']
['ex', 'in s123 9']
['ex|in s123', '9']
- 长度 len()
user_info = "ex|in s123 9"
v1 = len(user_info)
print(v1)
输出
12
- 索引
val = "exin"
v = val[0]
print(v)
val = input('>>>')
i = 0
while i < len(val):
print(val[i])
i += 1
输出
e
>>>iii
i
i
i
- 切片
name = '我叫李杰,性别我今年岁,我在说谎!'
print(name[0])
print(name[0:2])
print(name[5:9])
print(name[5:])
print(name[5:-2])
print(name[-2:])
输出
我
我叫
性别我今
性别我今年岁,我在说谎!
性别我今年岁,我在说
谎!
字符串常用方法归纳如下:
- 1)capitalize
功能:实现字符串首字母大写,自身不变,会生成一个新的值
例子:
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 name = 'hexin'
4 v = name.capitalize() #调用str类,执行其中capitalize的方法
5 print(v)
输出:
hexin
Hexin
- 2)casefold
功能:将所有大写变成小写,另外支持多门语言变化
例子:
1 name = 'HexIn'
2 v = name.casefold()
3 print(name)
4 print(v)
输出:
HexIn
hexin
- 3)lower,upper
功能:
lower:将所有的大写变小写,局限英文
upper:将所有小写变大写
lower
例子:
1 name = 'HeXin'
2 v = name.lower()
3 print(name)
4 print(v)
结果:
HeXin
hexin
- 4)center
功能:文本居中,空白处填充字符
参数1:表示总长度;参数2:空白处填充的字符(长度为1)
center例子:
1 name = 'HeXin'
2 v = name.center(20,'*')
3 print(name)
4 print(v)
输出:
HeXin
*******HeXin********
- 5)count
功能:表示要查找的子序列在字符串中出现的次数
参数1:要查找的值(子序列);参数2:起始位置(索引);参数3:结束位置(索引)
count例子:
1 name = 'HeXinddaklfjsl;dfjcnljdajsflajdf'
2 v = name.count('a')
3 i = name.count('a',0,15)
4 print(name)
5 print(v)
6 print(i)
输出:
3
1
- 6)endswith
功能:判断是否以xx结尾
参数1:判断值;参数2,3:起始和结束的位置(个数)
endswith例子:
1 name = 'HeXinddaklfjsl;dfjcnljdajsflajdf'
2 v = name.endswith('df')
3 i = name.endswith('n',0,5)
4 print(name)
5 print(v)
6 print(i)
输出:
HeXinddaklfjsl;dfjcnljdajsflajdf
True
True
- 7)expandtabs
功能:找到制表符\t,进行替换(包含前面的值)
expandtabs例子:
1 name = "al\te\tx\nalex\tuu\tkkk"
2 v = name.expandtabs(5) #包含前面的值,5个长度
3 print(v)
输出:
al e x
alex uu kkk
- 8)find
功能:找到指定子序列的索引位置,不存在返回-1
find例子:
1 name = 'hexin'
2 v = name.find('0')
3 i = name.find('x')
4 print(v)
5 print(i)
输出:
-1
2
- 9)format,%s,format_map
功能:字符串格式化
formatformat_map
例子:
1 tpl1 = "我是:%s;年龄:%s;性别:%s" %( 'hexin',18,'man')
2 print(tpl1)
3
4 tpl2 = "我是:{0};年龄:{1};性别:{2}"
5 v2 = tpl2.format("李杰",19,'都行')
6 print(v2)
7
8 tpl3 = "我是:{name};年龄:{age};性别:{gender}"
9 v3 = tpl3.format(name='李杰',age=19,gender='随意')
10 print(v3)
11
12 tpl4 = "我是:{name};年龄:{age};性别:{gender}"
13 v4 = tpl4.format_map({'name':"李杰",'age':19,'gender':'中'})
14 print(v4)
输出:
我是:hexin;年龄:18;性别:man
我是:李杰;年龄:19;性别:都行
我是:李杰;年龄:19;性别:随意
我是:李杰;年龄:19;性别:中
- 10)isalnum
功能:是否是数字或汉字
isalnum例子:
1 name = 'hexin0好'
2 v = name.isalnum()
3 print(v)
输出:
True
- 11)isdecimal,isdigit,isnumeric
功能:是否是数字
isalnumisdecimalisdigit
例子:
1 num = '二'
2 v1 = num.isdecimal() # '123'
3 v2 = num.isdigit() # '123','②'
4 v3 = num.isnumeric() # '123','二','②'
5 print(v1,v2,v3)
输出:
False False True
- 12)isidentifer
功能:是否是有效的标识符
isidentifier例子:
1 n = '1name'
2 u = 'name'
3 v = n.isidentifier()
4 i = u.isidentifier()
5 print(v)
6 print(i)
输出:
False
True
- 13)islower(isupper)
功能:是否全部是小写(大写)
islower例子:
1 name = 'hexin'
2 name1 = 'Hexin'
3 v = name.islower()
4 i = name1.islower()
5 print(v)
6 print(i)
输出:
True
False
- 14)isprintable
功能:是否包含隐含的XX(包含\n,\t等不可见字符为False)
isprintable
例子:
1 name = 'hexindas\talj,hexin'
2 v = name.isprintable()
3 print(v)
输出:
False
- 15)join
功能:元素拼接
join例子:
1 name = 'hexin'
2
3 v = "_".join(name) # 内部循环每个元素
4 print(v)
5
6 name_list = ['1','2','3','4']
7 v = "+".join(name_list)
8 print(v)
输出:
h_e_x_i_n
1+2+3+4
- 16)rjust,ljust
功能:左右填充,类似center
ljust例子:
1 name = 'hexin'
2 v = name.ljust(14,'*')
3 i = name.rjust(6,'*')
4 print(v)
5 print(i)
输出
hexin*********
*hexin
- 17)maketrans,translate
功能:创建对应关系,翻译转换
maketrans例子:
1 m = str.maketrans('aeiou','12345') # 对应关系
2 name = "akpsojfasdufasdlkfj8ausdfakjsdfl;kjer09asdf"
3 v = name.translate(m)
4 print(v)
输出:
1kps4jf1sd5f1sdlkfj815sdf1kjsdfl;kj2r091sdf
- 18)partition
功能:分割,保留分割的元素
partition例子:
1 content = "9SB6SB6"
2 v = content.partition('SB') # partition
3 print(v)
输出:
('9', 'SB', '6SB6')
- 19)replace
功能:替换
replace例子:
1 content = "1SB2SB3SB4"
2 v = content.replace('SB','Love')
3 print(v)
4 v = content.replace('SB','Love',1)
5 print(v)
输出:
1Love2Love3Love4
1Love2SB3SB4
- 20)strip
功能:移除空白,\n,\t, 自定义
strip
例子:
1 name = 'hexin \t'
2 v = name.strip() # 空白,\n,\t
3 print(v)
输出:
hexin
- 21)zfill
功能:填充0
zfill例子:
1 name = 'hexin'
2 v = name.zfill(20)
3 print(v)
输出:
000000000000000hexin
2.整数
类int
- 1)bit_length
功能:当前整数的二进制表示的最少位数
bit_length例子:
age = 4 # 100
print(age.bit_length())
输出:
3
- 2)to_bytes
功能:获取当前数据的字节表示
to_bytes例子:
age = 15
v = age.to_bytes(10,byteorder='big')
v = age.to_bytes(10,byteorder='little')
print(v)
输出:
b'\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00'
3.list列表
类list
可变类型
- 创建
a = ['狗','eric',123]
a = list(['狗','eric',123])
print(a)
输出
['狗', 'eric', 123]
- in判断
a = ['狗','eric',123]
if 'eric' in a:
print(True)
print(a)
输出
True
['狗', 'eric', 123]
- 索引
val = a[0]
- 长度
val = len(a)
- 切片
a = ['狗','eric',123]
v = a[0::2]
print(v)
输出
['狗', 123]
补充:
- 1)append
功能:追加
append例子:
user_list = ['tom','刘','jack','n'] # 可变类型
user_list.append('hex')
print(user_list)
输出
['tom', '刘', 'jack', 'n', 'hex']
- 2)clear
功能:清空
例子:
user_list = ['tom','刘','jack','n'] # 可变类型
user_list.clear()
print(user_list)
输出:
[]
- 3)copy
功能:浅拷贝
例子:
user_list = ['tom','刘','jack','n']
t = user_list.copy()
print(user_list)
print(t)
输出:
['tom', '刘', 'jack', 'n']
['tom', '刘', 'jack', 'n']
- 4)count
功能:计数
例子:
user_list = ['tom','n','刘','jack','n','n']
t = user_list.count('n')
print(user_list)
print(t)
输出:
['tom', 'n', '刘', 'jack', 'n', 'n']
3
- 5)extend
功能:扩展原列表
例子:
user_list = ['tom','n','刘','jack','n','n']
user_list.extend('9')
print(user_list)
输出:
['tom', 'n', '刘', 'jack', 'n', 'n', '9']
- 6)index
功能:查找元素索引,没有报错
例子:
user_list = ['tom','n','刘','jack','n','n']
v = user_list.index('n')
print(v)
输出:
1
- 7)pop
功能:删除并获取元素,索引
例子:
user_list = ['tom','n','刘','jack','n','n']
v = user_list.pop(1)
print(v)
print(user_list)
输出:
n
['tom', '刘', 'jack', 'n', 'n']
- 8)remove
功能:删除,值
例子:
user_list = ['tom','n','刘','jack','n','n']
user_list.remove('n')
print(user_list)
输出:
['tom', '刘', 'jack', 'n', 'n']
- 9)reverse
功能:翻转
例子:
user_list = ['tom','n','刘','jack','n','n']
user_list.reverse()
print(user_list)
输出:
['n', 'n', 'jack', '刘', 'n', 'tom']
- 10)sort
功能:排序
例子:
num = [11,2,3,6,111]
num.sort()
print(num)
num.sort(reverse=True)
print(num)
输出:
[2, 3, 6, 11, 111]
[111, 11, 6, 3, 2]
4.range
- 1)创建
py2.7:立即生成数字
range(1,11) # 生成 1,23,,4,56.10
py3:不会立即生成,只有循环迭代,才一个个生成;
for i in range(1,11):
print(i)
for j in range(1,11,2):
print(j)
for k in range(10,0,-1):
print(k)
1
2
3
4
5
6
7
8
9
10
1
3
5
7
9
10
9
8
7
6
5
4
3
2
1
例子
# li = ['eric','alex','tony']
# for i in range(0,len(li)):
# ele = li[i]
# print(ele)
li = ['eric','alex','tony']
for i in li:
print(i)
# for i in range(0,len(li)):
# print(i+1,li[i])
输出
eric
alex
tony
5.enumerate
功能:额外生成一列有序的数字
例子
li = ['eric','alex','tony']
for i,ele in enumerate(li,1):
print(i,ele)
#1 eric
#2 alex
#3 tony
li = ['eric','alex','tony']
for i,ele in enumerate(li,1):
print(i,ele)
v = input('请输入商品序号:')
v = int(v)
item = li[v-1]
print(item)
#1 eric
#2 alex
#3 tony
#请输入商品序号:1
#eric
6.tuple元组
不可被修改类型,儿子不可被修改,孙子可以
- 1)创建
user_tuple = ('hex','eric','seven','hex')
- 2)count
功能:获取个数
user_tuple = ('hex','eric','seven','hex')
v = user_tuple.count('hex')
print(v)
#2
- 3)index
功能:获取值得第一个索引位置
user_tuple = ('hex','eric','seven','hex')
v = user_tuple.index('hex')
print(v)
#0
- 4)注意:元组最后加逗号
例子
li = ('hx',)
print(li)
- 5)本身不可修改,但是孙子可以
user_tuple = ('alex','eric','seven',['1','2','3'],'a4')
# user_tuple[0] = 123 执行错误
# user_tuple[3] = [11,22,33] 执行错误
user_tuple[3][1] = '0'
print(user_tuple)
7.dict
可变类型
- 创建
v = { 'name': 'al','password': '123123'}
- 1)clear
功能:清空
dic = {'k1':'v1','k2':'v2'}
dic.clear()
print(dic)
- 2)copy
功能:浅拷贝
dic = {'k1':'v1','k2':'v2'}
v = dic.copy()
print(v)
- 3)get
功能:根据key获取指定value,不存在不报错
dic = {'k1':'v1','k2':'v2'}
v = dic.get('k1111',1111)
print(v)
- 4)pop
功能:删除并获取对应的value值
# dic = {'k1':'v1','k2':'v2'}
# v = dic.pop('k1')
# print(dic)
# print(v)
输出:
{'k2': 'v2'}
v1
- 5)popitem
功能:随机删除键值对,并获取到删除的键值
dic = {'k1':'v1','k2':'v2'}
v = dic.popitem()
print(dic)
print(v)
输出:
{'k1': 'v1'}
('k2', 'v2')
dic = {'k1':'v1','k2':'v2'}
k,v = dic.popitem() # ('k2', 'v2')
print(dic)
print(k,v)
输出:
{'k2': 'v2'}
k1 v1
dic = {'k1':'v1','k2':'v2'}
v = dic.popitem() # ('k2', 'v2')
print(dic)
print(v[0],v[1])
输出:
{'k1': 'v1'}
k2 v2
- 6)setdefault
功能:增加,如果不存在即删除
dic = {'k1':'v1','k2':'v2'}
dic.setdefault('k3','v3')
print(dic)
dic.setdefault('k1','1111111')
print(dic)
输出:
{'k2': 'v2', 'k1': 'v1', 'k3': 'v3'}
{'k2': 'v2', 'k1': 'v1', 'k3': 'v3'}
- 7)update
功能:批量增加或修改
dic = {'k1':'v1','k2':'v2'}
dic.update({'k3':'v3','k1':'v24'})
print(dic)
输出:
{'k1': 'v24', 'k2': 'v2', 'k3': 'v3'}
- 8)fromkeys
功能:从序列键和值设置为value来创建一个新的字典。
例子:
dic = dict.fromkeys(['k1','k2','k3'],123)
dic['k1'] = 'asdfjasldkf'
print(dic)
输出:
{'k2': 123, 'k1': 'asdfjasldkf', 'k3': 123}
8.set
集合,不可重复列表,可变类型。
- 1)创建
s1 = {"alex",'eric','tony'}
print(type(s1))
print(s1)
输出:
<class 'set'>
{'alex', 'eric', 'tony'}
- 2)difference
功能:输出s1中存在,s2中不存在的值
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
v = s1.difference(s2)
print(v)
输出:
{'ii'}
- 3)difference_update
功能:s1中存在,s2中不存在,然后对s1清空,然后在重新赋值
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
s1.difference_update(s2)
print(s1)
输出:
{'ii'}
- 4)symmetric_difference
功能:s1中存在,s2中不存在的值及s2中存在,s1中不存在的值
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
v = s1.symmetric_difference(s2)
print(v)
{'ii', 'hexin'}
- 5)intersection
功能:交集
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
v = s1.intersection(s2)
print(v)
输出:
{'eric', 'alex', 'tony'}
- 6)union
功能:并集
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
v = s1.union(s2)
print(v)
输出:
{'alex', 'hexin', 'eric', 'ii', 'tony'}
- 7)discard
功能:移除
s1 = {"alex",'eric','tony','ii'}
s2 = {"alex",'eric','tony','hexin'}
s1.discard('alex')
print(s1)
输出:
{'eric', 'tony', 'ii'}
- 8)update
功能:添加
s1 = {"alex",'eric','tony','李泉','李泉11'}
s1.update({'alex','123123','fff'})
print(s1)
输出:
{'fff', '李泉', '123123', 'tony', 'alex', 'eric', '李泉11'}
9.布尔值
- 创建
- 转换
二、基本运算
1.算数运算:
注:运算符的执行顺序,从前往后
2.比较运算:
3.赋值运算:
4.逻辑运算:
5.成员运算: