python基础篇2~数据类型
数据类型
数据类型:数字、字符串、列表、元组、字典、集合
数字int
i = 3 print(i.bit_length()) i = 6 print(i.bit_length())
字符串str
索引、切片
# 1.索引和切片[起始位置:结束为止:步长] # s1 = 'python全栈8期' # print(len(s1)) # 索引从0开始【索引(下标,index)】 # print(s1[0]) # print(s1[5]) # print(s1[-1]) # 切片 顾头不顾尾(索引部分超出部分不会报错) # print(s1[0:6]) # print(s1[6:10]) # print(s1[6:1000]) # print(s1[6:]) # 切片 # print(s1[1:5:2]) # print(s1[3:0]) #不会报错 # 倒取值:加步长 # print(s1[3::-1]) # print(s1[5::-2]) # print(s1) #s1不变 # s2 = s1[0:6] # print(s2) # s = 'python全栈8期' # print(s[5:0:-1]) # print(s[-1:-4]) # print(s[-1:-4:-1]) # print(s[-4:-1])
字符串方法
# capitalize首字母大写,其他的全部小写 ret = 'alex' # print(ret.capitalize()) ret1 = 'alexS' # print(ret1.capitalize()) # upper,lower # ret2 = ret.upper() # ret3 = ret.lower() # print(ret2,ret3) # ret4 = ret.swapcase() # print(ret4) # # # 以特殊符号或者空格隔开,每一个字符串首字母大写 # ret11 = 'alex jin wu' # # print(ret11.title()) # 居中 # ret = 'alexS' # print(ret.center(20)) # print(ret.center(20,'*')) #*******alexS******** ret = 'alexS' # ret5 = ret.startswith('a') # ret5 = ret.startswith('ale') # ret5 = ret.startswith('alexS') # ret5 = ret.startswith('l',1,3) #切片 # print(ret5) # ret6 = ret.endswith('xS') # print(ret6) #count ret22 = 'alex,aeaeQ' # ret7 = ret22.count('Q') # ret7 = ret22.count('ale') #整体与个体‘ale'相当于是一个整体 # ret7 = ret22.count('W') #当不存在时就会返回0 # print(ret7) #\t前面的补全 #默认将一个tab键变成8个空格,如果tab前面的字符长度不足8个,则会补全8个,如果tab键前面的字符长度超过8个,则会补全16个,以此类推 # a2 = "hqw\t" # ret4 = a2.expandtabs() # print(ret4,len(ret4)) # b = "lsajdlajlkja\t" # ret8 = b.expandtabs() # print(ret8,len(ret8)) # index 和 find 用法一模一样,找不到元素,就会报错 # ret = 'alexS' # ret10 = ret.index('w') # print(ret10) # #strip去除字符串前后的空格 # ret23 = ' alex '.strip() # print(ret23) # ret = 'dww zjj' # ret2 = ret.strip() # print(ret2) # name = 'alex' # username = input('请输入你的姓名:') # if username.strip() == name: # print('登录成功') # ret = '***alex***'.strip('*') # print(ret) # ret1 = '***alex***'.lstrip('*') # ret2 = '***alex***'.rstrip('*') # print(ret1) # print(ret2) # # ret13 = '***asldfkjafa**'.strip('*') # ret14 = 'asldfkjafa'.strip('salf') # ret15 = 'asldfkajafa'.strip('salf') # ret16 = 'asldfkajafa'.strip('saWlf') # print(ret13) # print(ret14) # print(ret15) # print(ret16) # ret16 = 'jinxin alex musir' # # ret17 = ret16.split() # print(ret17) # ret18 = 'title tle tlie' # ret19 = ret18.split('t') # ret20 = ret18.split('t',2) # print(ret20) # print(ret19) #--->['', 'i', 'le ', 'le ', 'lie'] #replace # ret21 = 'dwwisverygoodwsw' # ret22 = ret21.replace('dww','zjj') # ret23 = ret21.replace('w','m',3) # print(ret22) # print(ret23) # 格式化输出 format # 通过索引的方法 # ret20 = 'name:{0},sex:{1},high:{2}'.format('alex','male',180) # 通过键值对的方法 # ret20 = 'name:{name},sex:{sex},high:{high}'.format(name='alex',sex='male',high=180) # print(ret20) # name = '123' # print(name.isalnum()) #字符串由字母或数字组成 # print(name.isalpha()) #字符串只由字母组成 # print(name.isdigit()) #字符串只由数字组成 # s = 'aseldki' # print(len(s)) # print(s[0]) # index = 0 # while index < len(s): # print(s[index]) # index += 1 # # if index == len(s):break # for 变量in可迭代对象 # for i in s: # print(i) # s = 'kasjdlfjal12jfal3jla4' # count = 0 # for i in s: # if i.isdigit(): # count +=1 # else: # print(666) # print(count)
布尔类型
# 字符串转化成bool值,bool(str) # 非空字符串转化成bool true # ''空字符串转化成bool false # s1 = 'afs' # s2 = 'a' # s3 = '1' # s4 = '' # print(bool(s1)) # print(bool(s2)) # print(bool(s3)) # print(bool(s4)) # bool -》str 值 # a = str(True) # b = str(False) # print(a,type(a)) # print(b,type(b)) a = str(2<1) print(a,type(a))
列表
索引及切片
# #索引和切片 # 索引 返回的是列表中的一个元素 # l = [1,2,3] # print(l[0]) # print(l[3]) # print(l[-1]) # 切片 返回的是一个列表 # l = [1,2,3,'af','re','re',4,'24'] # print(l[0:3]) # print(l[0:5:2]) # print(l[-2::-2]) # print(l[5:0:-2])
列表方法
# insert 插入 # li = ['taibai','alex','wusir'] # # li.insert(1,'dww') # li.insert(4,'dww') # print(li) # extend 迭代的添加 # li = ['taibai','alex','wusir'] # li.extend('dww') # li.extend(['dww',1,2,3]) # li.append(['dww',1,2,3]) # print(li) # 删除pop按照索引去删除,有返回值 # li = ['taibai','alex','wusir'] # name = li.pop(1) # print(name) # print(li) # remove 按照元素去删除,没有返回值 # li = ['taibai','alex','wusir'] # name = li.remove('alex') # print(name) # print(li) # clear清空列表 # li = ['taibai','alex','wusir'] # li.clear() # print(li) # del可根据索引删除,也可根据切片(加步长)删除 # li = ['taibai','alex','wusir','egon','dww'] # del li[0] # del li[0:3] # del li[0:4:2] # print(li) # 改 切片先删除,迭代着添加 li = ['taibai','alex','wusir','egon','dww','tianmao'] # li[0] = 'haoren' # li[0:3] = '黑人' # li[0:5] = '都是黑人' # li[0:2] = ['aa','bb','cc','dd',123] # print(li) # 查 li = ['taibai','alex','wusir','egon','dww','tianmao'] # print(li[1:4]) # for i in li: # print(i)
列表的其他方法
# count 计数 # li = [1,2,3,4,'a',1,'4'] # print(li.count(1)) # sort正序排序 # li = [2,3,5,4,6,8,7,1,9] # li.sort() # li.sort(reverse=True) -->倒叙排序 # print(li) # reverse反转 # li = [2,3,5,4,6,8,7,1,9] # li.reverse() # print(li)
列表的嵌套
# li = [1,2,5,'taibai','yuanhao',[1,'alex',3],True] # print(li[3]) # print(li[3][3]) # s = li[4].capitalize() # li[4] = s # print(li) # li[4] = 'Yuanhao' # print(li) # s2 = li[4].replace('hao','日天') # li[4] = s2 # li[4] = li[4][0:4] + '日天' #--->字符串的拼接 # print(li) # li[5][1] = 'ALEX' # li[5][1] = li[5][1].upper() # print(li)
元祖
#元祖type() t = (1,2,3,'adsf',True,[12,3,'日天','taibai'],('fdsa',2,3)) # print(t[4]) # print(t[1:4]) # for i in t: # print(i) # t[4] = False -->元祖中的儿子不能修改 # print(t) # t[5][2] = 'dww' # print(t)
range
#range范围,相当于可迭代对象 # for i in range(1,10): # print(i) # for i in range(1,10,2): #加步长 # print(i) # for i in range(10,1,-2): #加步长 # print(i)
字典dict
1.字典无序 2.数据关联性强,3,是以键值对的方式成对出现的,唯一一个映射的数据类型
# dic = {'name':'jinxin','age':20} # print(dic['name'])
# 字典的键必须是可哈希的(不可变的数据类型:字符串,数字,布尔值,元祖)并且键是唯一的
# 不可哈希的(可变的数据类型:列表,字典,set(集合)
# dic = {'name':'alex','age':18,'job':'teacher'} # print(dic)
增
# 方式一: # dic = {'name':'alex','age':18,'job':'teacher'} # dic['sex'] = 'male' # dic['name'] = '日天' -->如果字典中有这个键则直接覆盖该键的值,如果没有则是添加键值对 # print(dic) # 方式二: # setdefault()有键值对不做任何改变,如果没有键值对才添加 # dic = {'name':'alex','age':18,'job':'teacher'} # dic.setdefault('k') # dic.setdefault('k','v') #-->k在这里指的是键,v指的是值 # print(dic)
删
# 删 pop # dic = {'name':'alex','age':18,'job':'teacher'} # print(dic.pop('name')) # print(dic.pop('ajsdlf',None))#-->如果删除的键不是该字典中的键,执行时会有报错,可以指定一个返回值None,也可以是其他的,例如‘输错了’ # popitem()随机的删除一个键值对 # dic = {'name':'alex','age':18,'job':'teacher','high':159} # dic.popitem() # print(dic) # clear()清空 # dic = {'name':'alex','age':18,'job':'teacher','high':159} # dic.clear() # print(dic) # del # dic = {'name':'alex','age':18,'job':'teacher','high':159} # del dic['name'] # del dic['jdaj'] #如果该键不是该字典中的键,就会有报错 # print(dic)
改
# 方法一: # dic = {'name':'alex','age':18,'job':'teacher','high':159} # dic['name'] = '日天' # print(dic) # 方法二: # update():将dic1所有的键值对覆盖添加(相同的覆盖,没有的添加)到dic中 # dic = {'name':'alex','age':18,'job':'teacher','high':159} # dic1 = {'name':'barry','hobby':'oldwoman'} # dic.update(dic1) # print(dic) # print(dic1)
查
# 方法一:通过键查值 # dic = {'name':'alex','age':18,'job':'teacher','high':159} # print(dic['name']) # print(dic['djakj']) #差不到时会报错 # 方法二:get() # dic = {'name':'alex','age':18,'job':'teacher','high':159} # print(dic.get('name')) # print(dic.get('slkjd')) # print(dic.get('slkjd','sb,没有这个键')) #-->对于没有查到的内容可以指定返回值
其他操作
# dic = {'name':'alex','age':18,'job':'teacher','high':159} # item = dic.items() # print(item,type(item)) -->#这个类型就是dict_items类型,可迭代的 # # keys = dic.keys() # print(keys,type(keys)) # # values = dic.values() # print(values,type(values))
for循环
# for循环:用户按照顺序循环可迭代对象的内容 dic = {'name':'alex','age':18,'job':'teacher','high':159} #---->返回的是键 # for item in dic: # print(item) # for i in dic: # print(i) # for key in dic.keys(): # print(key) #---->返回的是值 # for values in dic.values(): # print(values) #---->把键和值返回成一个个的元祖 # for item in dic.items(): # print(item) # --->返回键和值 for key,value in dic.items(): print(key,value)
习题
7,有如下列表li = [1,3,4’,alex’,[3,7,8,’taibai’],5,’ritian’] 循环打印列表中的每个元素,遇到列表则再循环打印出它里面的元素。 我想要的结果是(用三种方法实现): # 1 # 3 # 4 # ‘alex’ # 3 # 7, # 8 # ‘taibai’ # 5 # ritian # 方法一: # li = [1,3,4,'alex',[3,7,8,'taibai'],5,'ritian'] # for i in range(0,len(li)): # if i ==4: # for j in li[i]: # print(j) # else: # print(li[i]) # 方法二: # li = [1,3,4,'alex',[3,7,8,'taibai'],5,'ritian'] # for i in li: # if type(i) == list: # for j in i: # print(j) # else:print(i) # 方法三: # li = [1,3,4,'alex',[3,7,8,'taibai'],5,'ritian'] # index = 0 # while index < len(li): # if index == 4: # for i in li[4]: # print(i) # else:print(li[index]) # index += 1