一、数字类型:整型(int)and 浮点型(flaoat)
int用途:用来定义整数如:年龄、号码等
int定义方式:age = 10 # age = int(10)
整型是无序的不可变类型,只能存一个值
float定义方式:height = 1.77 # height = float(1.77)
浮点型是无序的不可变类型,只能存一个值
将10086进行进制转换
print(hex(10086)) #将10进制转成16进制0x2766print(oct(10086)) #将10进制转成8进制0o23546print(bin(10086)) #将10进制转成2进制0b10011101100110
所有进制转成10进制
print(int('0x2766',16)) #16进制转10print(int('0o23546',8)) #8进制转10print(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\t'print(name)
内置方法:
1.按照索引取值(正向取值,反向取值)
name = 'please call me jack'print(name[4]) #正向取第五位print(name[-4]) #反向取第四个sj
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\thello' # \t表示制表符(tab键) >>> name tony hello >>> name.expandtabs(1) # 修改\t制表符代表的空格数 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']