一、数字类型:整型(int)and 浮点型(flaoat)
int用途:用来定义整数如:年龄、号码等
int定义方式:
age = 10 # age = int(10)
整型是无序的不可变类型,只能存一个值
float用途:用来定义浮点数如:身高、体重等
float定义方式:
height = 1.77 # height = float(1.77)
将10086进行进制转换
print(hex(10086)) #将10进制转成16进制
0x2766
print(oct(10086)) #将10进制转成8进制
0o23546
print(bin(10086)) #将10进制转成2进制
0b10011101100110
所有进制转成10进制
print(int('0x2766',16)) #16进制转10
print(int('0o23546',8)) #8进制转10
print(int('0b10011101100110',2)) #2进制转10
将字符串转成浮点数类型
s = float('1.23')
print(type(s))
将浮点数转成整数
m = float(1.23)
s = int(m)
print(type(s))
二、字符串
用途:用于存储一些描述性信息
定义方式:
name = 'jack'#可用' ' ," ", ''' '''表示
r 表示转义取消描述里的特殊符号
name = r'1,2,3,4,5 '
print(name)
内置方法:
1.按照索引取值(正向取值,反向取值)
name = 'please call me jack'
print(name[4]) #正向取第五位
print(name[-4]) #反向取第四个
s
j
2.切片顾头不顾尾:取出0-8的字符
name = 'please call me jack' print(name[0:9])# 也可使用步长进行切片[0:9:2]取从0-8的字符每隔2取一个字符 please ca #pes a 反向切片 name = 'please call me jack' print(name[::-1]) #print(name[::-2]) kcaj em llac esaelp #ka mla sep
3.长度len:
获取字符串的长度,即字符的个数,但凡存在引号内的都算作字符
name = 'please call me jack' print(len(name)) 19
4.成员运算 in 和 not in
name = 'please call me jack' print('hello' in name) print('call' in name) False True name = 'please call me jack' print('hello' not in name) print('call'not in name) True False
5.移除(strip):移除字符串首尾指定的字符(默认移除空格)()内指定字符移除首尾字符
name = 'please call me jack!' print(name.strip('!')) please call me jack name = ' please call me jack! ' print(name.strip()) please call me jack!
6.移除符:lstrip移除左边的指定字符,rstrip移除右边的指定字符
name = '!!!!!!!please call me jack!!!!!!!!' print(name.lstrip('!')) please call me jack!!!!!!!! name = '!!!!!!!please call me jack!!!!!!!!' print(name.rstrip('!')) !!!!!!!please call me jack
7.切分(split):括号里不指定字符默认以空格作为切分符号
name = 'please call me jack!' print(name.split()) ['please', 'call', 'me', 'jack!'] name = 'plea|se |call |me j|ac|k!' print(name.split('|')) ['plea', 'se ', 'call ', 'me j', 'ac', 'k!']#split切割得到的结果是列表数据类型
split会按照从左到右的顺序对字符串进行切分,可指定切分次数,rsplit从右向左切分,可指定切分次数
name = '!!!!!!! please call me jack!!!!!!!!' print(name.split(' ',2)) ['!!!!!!!', 'please', 'call me jack!!!!!!!!'] name = '!!!!!!! please call mejack!!!!!!!!' print(name.rsplit(' ',2)) ['!!!!!!! please call', 'me', 'jack!!!!!!!!']
8.startswith()判断字符串是否以括号内的指定字符开头,结果为true 或 false endswith()判断字符串是否以括号内的指定字符结尾,结果为true 或 flas
name = '!!!!!!!please call me jack!!!!!!!!' print(name.startswith('p')) False print(name.startswith('!')) True name = '!!!!!!!please call me jack!!!!!!!!' # print(name.rstrip('!')) print(name.endswith('p')) False print(name.endswith('!')) True
9..format格式化输出
1.formart括号内的参数可以被打乱顺序,指名道姓地为指定参数传值如:name = 'jack'就是传给{} s1 = 'my name is {name},my age is {age}'.format(age = 18,name = 'jack' ) print(s1) my name is jack,my age is 18 2.如果不指定参数参数值就会根据顺序传值 s1 = 'my name is {},my age is {}'.format(18, 'jack' ) print(s1) my name is 18,my age is jack 3.可以将format传入的多个值当作一个列表,然后{索引}取值 s1 = 'my name is {1},my age is {0}'.format(18, 'jack' ) print(s1) my name is jack,my age is 18
10.join从可迭代对象中取出多个字符串,按照指定分隔符进行拼接,拼接结果为字符串
name = '!!!!!!! please call me jack!!!!!!!!' #在name中取出多个字符段,用%作为分隔符进行拼接 print("%".join(name)) !%!%!%!%!%!%!% %p%l%e%a%s%e% %c%a%l%l% %m%e% %j%a%c%k%!%!%!%!%!%!%!%!
11.replace字符替换
1.用新的字符替换掉旧的字符 name = name.replace('jack','tom') print(name) please call me tom 2.指定修改个数 name = 'please call me jack,i am not jack' name = name.replace('jack','tom',1) print(name) please call me tom,i am not jack
12.isdigit
判断字符串是否纯数字构成,返回结果True或False name = 'please call me jack,i am not jack' name = name.replace('jack','tom',1) print(name.isdigit()) False name name = '123456789' print(name.isdigit())= '123456789' print(name.isdigit()) True
13lower(),upper()
s1 = ' hello world '#将英文字符串全部转换成大写 print(s1.upper()) HELLO WORLD s1 = ' Hello World ' #将英文字符串全部转换成小写 print(s1.lower()) hello world
3.3.3 了解操作
# 1.find,rfind,index,rindex,count # 1.1 find:从指定范围内查找子字符串的起始索引,找得到则返回数字1,找不到则返回-1 >>> msg='tony say hello' >>> msg.find('o',1,3) # 在索引为1和2(顾头不顾尾)的字符中查找字符o的索引 1 # 1.2 index:同find,但在找不到时会报错 >>> msg.index('e',2,4) # 报错ValueError # 1.3 rfind与rindex:略 # 1.4 count:统计字符串在大字符串中出现的次数 >>> msg = "hello everyone" >>> msg.count('e') # 统计字符串e出现的次数 4 >>> msg.count('e',1,6) # 字符串e在索引1~5范围内出现的次数 1 # 2.center,ljust,rjust,zfill >>> name='tony' >>> name.center(30,'-') # 总宽度为30,字符串居中显示,不够用-填充 -------------tony------------- >>> name.ljust(30,'*') # 总宽度为30,字符串左对齐显示,不够用*填充 tony************************** >>> name.rjust(30,'*') # 总宽度为30,字符串右对齐显示,不够用*填充 **************************tony >>> name.zfill(50) # 总宽度为50,字符串右对齐显示,不够用0填充 0000000000000000000000000000000000000000000000tony # 3.expandtabs >>> name = 'tony hello' # 表示制表符(tab键) >>> name tony hello >>> name.expandtabs(1) # 修改 制表符代表的空格数 tony hello # 4.captalize,swapcase,title # 4.1 captalize:首字母大写 >>> message = 'hello everyone nice to meet you!' >>> message.capitalize() Hello everyone nice to meet you! # 4.2 swapcase:大小写翻转 >>> message1 = 'Hi girl, I want make friends with you!' >>> message1.swapcase() hI GIRL, i WANT MAKE FRIENDS WITH YOU! #4.3 title:每个单词的首字母大写 >>> msg = 'dear my friend i miss you very much' >>> msg.title() Dear My Friend I Miss You Very Much # 5.is数字系列 #在python3中 num1 = b'4' #bytes num2 = u'4' #unicode,python3中无需加u就是unicode num3 = '四' #中文数字 num4 = 'Ⅳ' #罗马数字 #isdigt:bytes,unicode >>> num1.isdigit() True >>> num2.isdigit() True >>> num3.isdigit() False >>> num4.isdigit() False #isdecimal:uncicode(bytes类型无isdecimal方法) >>> num2.isdecimal() True >>> num3.isdecimal() False >>> num4.isdecimal() False #isnumberic:unicode,中文数字,罗马数字(bytes类型无isnumberic方法) >>> num2.isnumeric() True >>> num3.isnumeric() True >>> num4.isnumeric() True # 三者不能判断浮点数 >>> num5 = '4.3' >>> num5.isdigit() False >>> num5.isdecimal() False >>> num5.isnumeric() False ''' 总结: 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 如果要判断中文数字或罗马数字,则需要用到isnumeric。 ''' # 6.is其他 >>> name = 'tony123' >>> name.isalnum() #字符串中既可以包含数字也可以包含字母 True >>> name.isalpha() #字符串中只包含字母 False >>> name.isidentifier() True >>> name.islower() # 字符串是否是纯小写 True >>> name.isupper() # 字符串是否是纯大写 False >>> name.isspace() # 字符串是否全是空格 False >>> name.istitle() # 字符串中的单词首字母是否都是大写 False
三、列表
列表就是用于存储一个或者多个不同类型的值
定义方式:l1 = [1,'a',[1,2]] #本质:l1 = list([1,'a',[1,2]])
1.1按索引取值(正向存取,反向存取)
s1 = ['jack','tom','jan','mira',789,456] print(s1[3]) #正向取第四个 mira s1 = ['jack','tom','jan','mira',789,456] print(s1[-3]) #反向取第三个 mira
1.2替换指定值
s1 = ['jack','tom','jan','mira',789,456] (s1[-3]) = 'jon' print(s1) ['jack', 'tom', 'jan', 'jon', 789, 456]
1.3切片(顾头不顾尾步长)
s1 = ['jack','tom','jan','mira',789,456] print(s1[0:3]) #取出索引为0-2的元素 ['jack', 'tom', 'jan'] s1 = ['jack','tom','jan','mira',789,456] print(s1[0:4:2])#取出索引为0-3的元素步长为2
1.4长度
s1 = ['jack','tom','jan','mira',789,456] print(len(s1)) 6
1.5成员运算in 和 not in
s1 = ['jack','tom','jan','mira',789,456] print('tom' in s1 True s1 = ['jack','tom','jan','mira',789,456] print('tom' not in s1) False
1.6添加
1.向s1列表尾部追加元素‘duck’ s1 = ['jack','tom','jan','mira',789,456] s1.append('duck') print(s1) ['jack', 'tom', 'jan', 'mira', 789, 456, 'duck'] 2.extend()一次性在列表尾部添加多个元素'dack',1,2,3 s1 = ['jack','tom','jan','mira',789,456] s1.extend(['dack',1,2,3]) print(s1) ['jack', 'tom', 'jan', 'mira', 789, 456, 'dack', 1, 2, 3] 3. insert()在指定位置插入元素 s1 = ['jack','tom','jan','mira',789,456] s1.insert(3,'kiki') #在第四个字符串位置插入‘kiki print(s1) ['jack', 'tom', 'jan', 'kiki', 'mira', 789, 456]
1.7删除
1.del s1 = ['jack','tom','jan','mira',789,456] del s1[4] #删除索引为4的字符串 print(s1) ['jack', 'tom', 'jan', 'mira', 456] 2.pop()默认删除列表最后一个元素,并将删除的值返回,括号内可以通过加索引值来指定删除元素 s1= ['jack','tom','jan','mira',789,456] print(s1.pop()) print(s1) 456 ['jack', 'tom', 'jan', 'mira', 789] s1 = ['jack','tom','jan','mira',789,456] print(s1.pop(4)) print(s1) 789 ['jack', 'tom', 'jan', 'mira', 789] 3.remove()括号内指名道姓表示要删除哪个元素,没有返回值 s1 = ['jack','tom','jan','mira',789,456] print(s1.remove(789)) print(s1) None ['jack', 'tom', 'jan', 'mira', 456] 4.reverse()颠倒列表内元素顺序 s1 = ['jack','tom','jan','mira',789,456] print(s1.reverse()) print(s1) [456, 789, 'mira', 'jan', 'tom', 'jack'] 5.。sort()当列表中是同一种数据类型时可进行从小到大排序 s1 = [1,2,5,3,14,9,8,7,6] l2 = s1.sort() print(s1) [1, 2, 3, 5, 6, 7, 8, 9, 14]
元祖
元祖可以存放多个不同类型的元素,不同之处在于元素不可更改,即元祖是一个不可变的列表,用于记录多个固定不允许修改的值,单独用于取。
定义方式:通过小括号存储数据,数据与数据之间通过逗号分隔,定义容器类型的时候,如果只有一个值,在值的后边加一个逗号,如果不加输出的就是一个字符串
t1 = ('a','b') #本质:t1 = tuple('a','b') 常用方法: 1.按照索引取值:只能取不能改 tuple1 = (1,2,3,4,'a','b','c') print(tuple1[3]) #正取第四个 4 tuple1 = (1,2,3,4,'a','b','c') print(tuple1[-3]) #反取第三个 a 2.切片:顾头不顾尾 可选步长 tuple1 = (1,2,3,4,'a','b','c') print(tuple1[0:7:3])#从0-7每隔3个字符截取一个字符 (1, 4, 'c') 3.长度 tuple1 = (1,2,3,4,'a','b','c') print(len(tuple1)) 7 4.成员运算: in not in tuple1 = (1,2,3,4,'a','b','c') print(5 in tuple1) False tuple1 = (1,2,3,4,'a','b','c') print(5 not in tuple1) True 5.循环 tuple1 = (1,2,3,4,'a','b','c') for i in tuple1: print(i) 1 2 3 4 a b c
字典
定义方式:通过大括号来存储数据,通过key:value来定义键值对数据,每个键值对通过逗号隔开
1.通过key存取值,可存可取可索引取值 l1 = {'name':"join",'age':18} print(l1['name']) join l1 = {'name':"join",'age':[18,19]} l3 = l1['age'][0] print(l3) 18 对于赋值操作,如果key原先不存在字典里,则会增加新的keyi:value如果存在就会修改对应的value值 l1 = {'name':"join",'age':[18,19]} l1['身高'] = 1.77 print(l1) {'name': 'join', 'age': [18, 19], '身高': 1.77} l1 = {'name':"join",'age':[18,19]} l1['身高'] = 1.79 print(l1) {'name': 'join', 'age': [18, 19], '身高': 1.79} 2.长度 l1 = {'name':"join",'age':[18,19]} print(len(l1)) 2 3.成员运算 l1 = {'name':"join",'age':[18,19]} print('name' in l1) True l1 = {'name':"join",'age':[18,19]} print('name' not in l1) False 4.获取字典中的key l1 = {'name':"join",'age':[18,19]} l2 = l1.keys() print(l2) dict_keys(['name', 'age']) 5.获取字典中的value l1 = {'name':"join",'age':[18,19]} l2 = l1.values() print(l2) dict_values(['join', [18, 19]]) 6.获取字典中的所有键值对 l1 = {'name':"join",'age':[18,19]} l2 = l1.items() print(l2) dict_items([('name', 'join'), ('age', [18, 19])])
1.get()key值存在获取相应的value值,不存在默认返回None可定义返回值 l1 = {'name':"join",'age':[18,19]} l2 = l1.get('name',‘没有) print(l2) join l1 = {'name':"join",'age':[18,19]} l2 = l1.get('naame','没有') print(l2) 没有 2.pop()删除指定的key对应的键值对,并返回值 l1 = {'name':"join",'age':[18,19],'a':'b'} l2 = l1.pop('a') print(l1,l2) {'name': 'join', 'age': [18, 19]} b 3.popitem()随机删除一组键值对,并将删除的键值放到元祖返回 l1 = {'name':"join",'age':[18,19],'a':'b'} l2 = l1.popitem() print(l1,l2) {'name': 'join', 'age': [18, 19]} ('a', 'b') 3.fromkeys()生成一个新字典,以第一个参数中的元素为key,第三个参数为值,组成一个新字典。 l2 = dict.fromkeys(['a','b','c'],['有','鬼','啊']) print(l2) {'a': ['有', '鬼', '啊'], 'b': ['有', '鬼', '啊'], 'c': ['有', '鬼', '啊']} 4.setdefault()key存在返回对应的value不存在则新增键值对返回value l2 = dict.fromkeys(['a','b','c'],['有','鬼','啊']) l3 = l2.setdefault('d',333) print(l3) print(l2) 333 {'a': ['有', '鬼', '啊'], 'b': ['有', '鬼', '啊'], 'c': ['有', '鬼', '啊'], 'd': 333} l2 = dict.fromkeys(['a','b','c'],['有','鬼','啊']) l3 = l2.setdefault('b',333) print(l3) print(l2) ['有', '鬼', '啊'] {'a': ['有', '鬼', '啊'], 'b': ['有', '鬼', '啊'], 'c': ['有', '鬼', '啊']}
集合
集合和列表、元祖、字典一样可以存放多个值,单集合主要用于去重,关系运算
通过{}存储每个元素,用逗号隔开集合内没有重复的元素,元素排列无序
定义一个空集合 s = {}是定义一个空字典 s = set()才是定义一个空集合 l1 = {'a','b','c','d'} l2 = {'e','f','b'} 和集| l1 = {'a','b','c','d'} l2 = {'e','f','b'} l3 = l1 | l2 print(l3) 交集& l1 = {'a','b','c','d'} l2 = {'e','f','b'} l3 = l1 & l2 print(l3) {'b'} 差集- l1 = {'a','b','c','d'} #l1独有的 l2 = {'e','f','b'} l3 = l1 - l2 print(l3) {'c', 'd', 'a'} 对称差集^ l1 = {'a','b','c','d'} l2 = {'e','f','b'} l3 = l1 ^ l2 print(l3) {'e', 'd', 'c', 'a', 'f'} 父集> 字集< 去重: 只能针对不可变类型,集合是无序的,去重后,无法保留原来的顺序 针对不可变类型并保证顺序,需写代码进行实现 l1 =[{'name':'tom','age':16,'sex':'male'},{'name':'jack','age':17,'sex':'male'},{'name':'jan','age':17,'sex':'fmale'},{'name':'jack','age':17,'sex':'male'}] new_l1 = [] for i in l1: if i not in new_l1: new_l1.append(i) print(new_l1) [{'name': 'tom', 'age': 16, 'sex': 'male'}, {'name': 'jack', 'age': 17, 'sex': 'male'}, {'name': 'jan', 'age': 17, 'sex': 'fmale'}]