单或双引号均可
(var=" Adc ad "或者var=' Adc ad ')函数
var=" Adc ad " var.title() #RS: Adc Ad #将每个单词首字母改成大写 var.upper() #RS: ADC AD #每个字母大写 var.lower() #RS: adc ad #每个单词小写拼接
message="I'm "+var+"!" #使用+进行拼接删除字符串前后空白
var.lstrip() #RS:"Adc ad " #删除字符串left空白 var.rstrip() #RS:" Adc ad" #删除字符串right空白 var.strip() #RS:"Adc ad" #删除字符串两侧空白索引
list=['asd','ads','rf'] list[-1] #RS:返回倒数第一个print(list[-1]):rf list[-2] #RS:返回倒数第二个print(list[-2]):ads添加
list=['asd','ads','rf'] list.append('shane') print(list) #RS:['asd','ads','rf','shane'] (print列表打印出的是包括中括号的格式)插入
list=['asd','ads','rf','shane'] list.insert(1.'aaa') #RS:在下标索引为1的位置插入'aaa'的字符串 print(list) #RS:['asd',‘aaa','ads','rf','shane']删除 根据索引删除
list=['asd',‘aaa','ads','rf','shane'] del list[2] #语法与上述稍有不同(删除之后所删除的值将无法继续使用) print(list) #RS:['asd',‘aaa','rf','shane'] 或者 list=['asd',‘aaa','ads','rf','shane'] list_item=list.pop(2) #(删除之后所删除的值存储到变量中继续使用) print(list) #RS:['asd',‘aaa','rf','shane'] print(list_item) #RS:ads根据最新加入的删除即列表末尾的元素
list=['asd',‘aaa','ads','rf','shane'] list_item=list.pop() print(list) #RS:['asd',‘aaa','ads','rf'] print(list_item) #RS:shane根据列表中的元素的值来删除元素
list=['asd',‘aaa','ads','rf','shane','rf'] remove_item=rf list.remove(remove_item) #(多个值将被删除) print(list) #RS:['asd',‘aaa','ads','shane']排序 sort()方法:(永久性)
list=['csd',‘baa','ads','drf'] list.sort() #(没有参数,默认按照字母顺序排序) print(list) #RS:'ads',‘baa','csd','drf'] 或者 list=['csd',‘baa','ads','drf'] list.sort(reverse=True) #(传递参数,按照字母顺序逆序排序) print(list) #RS:['drf',‘csd','baa','ads']sorted()方法:(临时性)
list=['csd',‘baa','ads','drf'] print(sorted(list)) #RS:['ads',‘baa','csd','drf'] print(list) #RS:['csd',‘baa','ads','drf']反转列表
list.reverse()列表长度
len(list)遍历 使用for循环遍历:
list=['csd',‘baa','ads'] for list_item in list: #冒号 print(list_item) print("_listItem") #①缩进 print("ok!") #②没有缩进 #RS: csd _listItem baa _listItem ads _listItem ok! #注意事项:①处在for中缩进,执行次数由for循环决定;②没有缩进,不属于for循环,只执行了一次切片(使用列表中的一部分)
list=['shane,‘holmes','sherlock','koko'] print(list[1:3]) #RS:[‘holmes','sherlock'] (输出下标包含1,2的列表) print(list[2:]) #RS:['sherlock','koko'] (输出下标从2开始直至末尾的列表) print(list[-3:]) #RS:[‘holmes','sherlock','koko'] (输出下标从倒数第3个开始直至末尾的列表)遍历切片
list=['shane,‘holmes','sherlock','koko'] for list_item in list[:3]: print(list_item.title()) #RS: Shane Holmes Sherlock复制列表
list=['shane,‘holmes','sherlock','koko'] my_list=list[:] #从头到尾的每一个元素 print(my_list) #RS:['shane,‘holmes','sherlock','koko']如果使用my_list=list(没有使用切片)而不是my_list=list[:],只是将list赋给了my_list,并没有在my_list中单独创建属于my_list的存储空间,即两者指向同一个对象,如下代码所示:
list=['shane,‘holmes','sherlock','koko'] my_list=list #并没有以切片的形式赋值 my_list.append("marlocks") print(my_list) #RS:['shane,‘holmes','sherlock','koko','marlocks'] print(list) #RS:['shane,‘holmes','sherlock','koko','marlocks']增删改查
dictionary={} #创建空的字典 dictionary['color']='red' #添加键为color值为red的键值对 dictionary['size']=255 #添加键为size值为255的键值对 dictionary['des']='none' #添加键为size值为255的键值对 print(dictionary['color']) #RS: red dictionary['color']='white' #将键为color的值修改为white for key,value in dictionary.items(): print("key:"+key+",value:"+value) #注:1.item()方法返回键值列表.2.key,value可以随意选取其他字符串.3.注意冒号以及缩进 #RS: key:color,value:red key:size,value:255 key:des,value:none #END RS del dictionary['des'] #删除键值对 RS: {'color':'red','size'=255} for key in dictionary.keys(): print(key) #注:1.keys()方法返回键的列表,依次将键值存到key中.2.若没有使用keys()方法即for key in dictionary:效果相同,默认情况 #RS: color size #END RS for value in dictionary.values(): print(value) #注:1.将值循环输出.2.若有重复值也会重复打印出,使用set()方法可避免: for value in set(dictionary.values()): #RS: red 255 #END RS嵌套 字典列表
dic1={'name':'shane','age':12} dic2={'name':'holmes','age':13} dic3={'name':'sherlock','age':14} list_dics={dic1,dic2,dic3} for dic in list_dics: print(dic) #RS: {'name':'shane','age':12} {'name':'holmes','age':13} {'name':'sherlock','age':14} #END RS #注:若取出每一个字典中的name的值,语句为:print(dic['name']) RS: shane holmes sherlock字典中存储列表
cart={ 'shane':['cookie','banana'], 'holmes':['cookie','shalla','apple'] } for name,foods in cart.items(): print(name.title()+"'s favorite foods are:") for food in foods: print(food) #RS: Shane's favorite foods are: cookie banana Holmes's favorite foods are: cookie shalla apple #END RS字典中存储字典 例如网站中用户名唯一,在字典中使用用户名作为键,然后将每一位用户的信息存储在一个字典中,并将该字典作为与该用户名相关联的值
users={ 'shane':{ 'age':12, 'sex':'male' }, 'holmes':{ 'age':13, 'sex':'female' } } for username,userinfo in users.items(): print(username+":"+str(userinfo['age'])+","+userinfo['sex']) #RS: shane:12,male holmes:13,female #END RS