day2--list/tuple/string/dictionary

Source: Internet
Author: User

1. List

#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:DCC#Listnames = ["a","b","C","D","e"]Print(names)Print(Names[0])Print(Names[1:3])#slicesPrint(names[1:])Print(Names[:3])Print(names[-2:]) Names.append ("F")#AppendPrint(names) Names.insert (2,"A")#InsertPrint(names) names[5] ="D"  #ModifyPrint(names) Names.remove ("D")#DeletePrint(names)delNAMES[2]#DeletePrint(names) Names.pop ()#The default is to delete the last one, in parentheses you can enter the coordinates to delete, Names.pop (2) with Del names (2)Print(names)Print(Names.index ("C"))#find where an element is locatedNames.reverse ()#reversalPrint(names) Names.sort ()#SortPrint(names) name2= ["a","b","C","D","e"]names.extend (name2)#splicing and connecting a listPrint(names,name2)Print(Names.count ("a"))#number of statistics aname2.clear ()#Clear ListPrint(name2)delName2#Delete ListPrint(name2) name1= Names.copy ()#CopyPrint(names)Print(name1) names[1] ="B" #Modify a single elementPrint(names)

2. String

#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:DCCname="\TABCDE AAA"Print(Name.capitalize ())#Capitalize first letterPrint(Name.count ("a"))#number of occurrences of statisticsPrint(Name.center (30,'-'))#Supplemental FormatPrint(Name.endswith ("a"))#determine what the string ends withPrint(Name.expandtabs (tabsize=10))#Convert the Tab key to how many spaces. Print(Name.find ("C"))#find the index where the character is located#name = "ABCDE {name} AAA {year}"#Print (Name.format (name= "Alex", Year= ")")Print("abc123". Isalnum ())#determine if it is an Arabic characterPrint("123". IsDigit ())#determines whether an integerPrint("A123". Isidentifier ())#determine if the variable name is validPrint("123". IsNumeric ())#determine if a pure number is similar to IsDigitPrint("My Name is". Istitle ())#Capitalize all initialsPrint("ABC". Isupper ())#are all uppercase. Print(",". Join (["1","2","3"]))#Change a list to a stringPrint(Name.ljust (50,"*"))Print(Name.rjust (50,"*"))#left and right complement allPrint("AAA". Lower ())#Uppercase Edge LowercasePrint("AAA". Upper ())#lowercase gets bigger .Print("\nname". Lstrip ())#go to the left, enter.Print("name\n". Lstrip ())#go to the right, enter.Print("name\n". Strip ())#go to all the carriage returns/spacesP= Str.maketrans ("abcdef","123456")#one by one corresponding Replacement/translationPrint("Alex Li". Translate (p))Print("Alex a A". replace ("a","A", 2))#Replace replacement, control numberPrint("Aebcdef". RFind ("e"))#Find it from the right and find the one on the right.Print("Alex Li Aa". Split ())#To convert a string to a list, you can customize the delimiterPrint("al\nex\nli\n". Splitlines ())#Split by change behavior, to listPrint("Alex Li". Swapcase ())#Case- insensitive

3. Dictionaries

#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:DCC#Key-value" "info = {"A": "A", "B": "B", "C": "C", "E": "E"}print (info) print (info["a"]) #通过key找valueinfo ["a"] = "AA" #修改val Ueprint (info["a"]) info["f"] = "F" #如果存在, modify, do not exist add print (info) #del info["C"] #删除 #info.pop ("C") #删除 #info.popitem () # Random Delete info["a"] #获取value, if not, will error, generally do not. Print (Info.get ("a")) #最安全的获取valueprint ("a" in info) #查找字典中是否有. Print (Info.values ()) #打印所有的valueinfo. SetDefault ("3": "1") #有的话, unchanged, no words added. Info.update (b) #将b更新到 info, the info is overwritten and no print (Info.items ()) #将字典转为列表 is added. c = Dict.fromkeys ([6,7,8], "test") #初始化一个字典print (c)" "#the cycle of the dictionary. Info1= {    "a":"A",    "b":"B",    "C":"C",    "e":"E"} forIinchINFO1:#The best way    Print(i)Print(I,info1[i])##For k,v in Info1.items (): #会先把dict转成list, the data is not used in large#print (k,v)

day2--list/tuple/string/dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.