One, the string s = ' python ' s1 = ' python ' + ' learn ' #相加其实就是简单拼接s2 = ' python ' * aa multiplication is actually how many times you copy yourself, and then stitch together a string slice: a= ' abcdefg ' Print (a[0]) # Index The subscript is the string consisting of elements starting with the first, the initial index is 0 and so on. Print (a[0:]) #默认到最后print (a[0:-1]) #-1 is the last print (A[0:5:2]) #加步长print (a[5:0:-2]) #反向加步长print (S.capitalize ()) # The initial capital print (S.swapcase ()) #大小写翻转print (S.title ()) #每个单词的首字母大写print (' learn ') #居中, total length, blank fill print (' Learnnnn '. Count (' n ', 0,6)) #计算字符串中某元素出现的个数, can be sliced. Print (' Pyt\t12345ho\tn '. Expandtabs ()) #补全 (tab-8 characters) # PYT 12345ho nprint (' Python '. Find ("PYT", 1,6)) #返回的找到的元素的索引, If the return -1print (' Python '. Index ("PYT", 1,6)) is not found #返回的找到的元素的索引, an error is not found. #str <---Turn---> listprint ('. Join ({' 1 ', ' 2 ', ' 3 '}) # "" In what it is, just combine all the STR elements in the parameters into a new string print (' py T,hon ') as a concatenation character. Split ()) #以什么分割 and eventually form a list this list does not contain this split element. The default space. Print (' py t.h.on '. Rsplit ('. ', 1)) #从右往左, take a separator #[' py t.h ', ' on ']print (' python* '. Strip ()) #默认去掉两端的空格. #python *print (' * *python* '. Rstrip (' * ')) #去掉字符串右边的空格 ' * ' #* *pythonprint (' * *python* '. Lstrip (' * ')) #去掉字符串左边的空格 ' * ' # * Python*print (' ABCBC '. replace (' BC ', ' SB ', 1)) #替换 andChange #asbbc#.format three formats output res1= ' {} {} {} '. Format (' A ', ', ' Male ') res2= ' {1} ' {0} {1} '. Format (' A ', ', ' Male ') res3= ' {name} {Age} {sex} '. Format (sex= ' Male ', name= ' a ', age=18) print (res1, ' \ n ', res2, ' \ n ', res3) print (' abc123 '. Isalnum ()) # The string consists of letters or numbers print (' abc123 '. Isalpha ()) #字符串只由字母组成print (' abc123 '. IsDigit ()) #字符串只由数字组成二, tuple tuple tuples are called read-only lists, i.e. data can be queried, But cannot be modified, so the slice operation of the string also applies to tuples. Example: tup1= (tup2=) ("A", "B", "C") tup3= (1,2,[],{}) #tup3中的 [],{} can be modified because the tuple saves the memory address of list and dict with ID () Check Note: If there is only one element in the tuple that does not add "," then what type is the element and what type is it? Third, List list:li = [1, ' A ', ' B ', 2,3, ' a ']# li.insert (0,55) #按照索引去增加 # li.append (' aaa ') #增加到最后 # li.append ([three-to-three]) #增加到最后 # Li.extend ([+]) #迭代添加 # L1 = li.pop (1) #按照位置去删除, with a return value of # del Li[1:3] #按照位置去删除, can also slice, delete no return value. # li.remove (' a ') #按照元素去删除 # li.clear () #清空列表 # Li[1] = ' Dfasdfas ' #改, assigning a value directly to an element # Li[1:3] = [' A ', ' B '] #对切片范围赋可迭代对象, insert the iteration value into the list ( The number of inserts at the selected range, the original index moved backward, the selected range will no longer exist) # check #切片查, or cycle through # Print (Li.count ("a")) #统计某个元素在列表中出现的次数 # Print (Li.index ("a")) # Find the index position of the first occurrence of a value from the list without error # Li.sort () #方法用于在原位置对列表进行排序. STR and int coexist with error # Li.reverse () #方法将列表中The elements are stored in reverse. Depth COPY:L1 = [1,2,3]L2 = l1# ID (L1) =id (L2) #指向同一个内存地址, modify L1,L2 will also change L3 = l1[:]# ID (L1)!=id (L3) #切片赋值给L3, new open memory, non-interference L = [' a ']L1 = [1,2,l] #L1的第三个元素是L的内存地址L4 = l1[:] #切片只是复制一层元素给L4, copy the address of L to L4, do not copy the value L points to l1[-1].append (' B ') #L1 [2] and l4[2] Both point to L, so l1[2] = = L4 [2]import copyL5 = Copy.deepcopy (L1) #对于深copy来说, the two are completely independent, changing any element of any one (no matter how many layers), the other absolutely does not change. Dictionary dict: #键唯一, can be hashed. # dic = {"name": "A", "age": +, "sex": "male"}# Dic1 = {"Name": "B", "Weight": 100}# dic1.setdefault (' weight ') # has a key value pair, does not make any changes, Not only added. # dic1.setdefault (' weight ', +) # value1 = dic["name"]# no error # value2 = Dic.get ("DJFFDSAFG", "Default return value") # if no return value can be returned # dic[ ' k ']= ' V ' # add # print (Dic1.pop (' Age ')) # with return value, press to delete # dic2.update (DIC) # to overwrite all key values of dic add (same overlay, no add) to Dic2 # item = Dic.items () # keys = Dic.keys () # values = Dic.values () # dic_clear = Dic.clear () # Empty Dictionary # Dictionary of Loops # for key in DIC: #key为键 # for item in DIC . Items (): #item = (' name ', ' A ') # for Key,value in Dic.items (): #key, value = ' name ', ' a ' V, enumerate# enumeration, for an iterative (iterable)/ An object that can be traversed (such as a list, a string), #enumerate将其组成一个索引序列, which can be indexed at the same timeand values. >>>s = Enumerate ([1,2,3,4,5,6]) >>>s.__next__ () >>> (0, 1) # for I in Enumerate (LI): # for Index, Name in Enumerate (li,2): # start position default is 0, can change six, range# specified range, generate the specified number. #1. The default is from small to large #2. The range is small to large, and the stride length needs to be positive #3. The range is from large size, the step must be negative # does not meet these three points just empty list []# for I in Range (1,10): # for I in Range (1,10,2): # Step # range is small to large, step Required for positive # for I in Range (10,1,-2): # Reverse Step # range is large from size, step must be negative five, set set: Set is unordered, non-repeating data collection, its elements are hash (immutable type), But the collection itself is non-hashed (so the collection does not have a dictionary key). Here are the most important two points of the collection: to go to the weight, to turn a list into a set, and to automatically go heavy. Relationship test, test the intersection of two sets of data, difference sets, and the relationship between the set. Set1 = set ({hobby '}) Set2 = {, ' Hobby '}print (Set1,set2) # {1, 2, ' hobby '} {1, 2, ' Hobby '}set1.add (' goddess ') set1.update ( [+/-]) #update: Iteration adds, does not repeat Set1.remove (1) # Deletes an element set1.pop () # randomly deletes an element set1.clear () # empties the collection del set1# deletes the collection Set1 & set2# intersection. (& or intersection) Set1 | set2# and set. (| or union) set1-set2# difference set. (-or difference) Set1 ^ set2# anti-intersect. (^ or symmetric_difference) Set1 < Set2 or Set1.issubset (Set2) # Set1 is a subset of Set2. BOOL Values Set2 > Set1 or Set2.issuperset (SET1) # Set2 are Set1 superset. BOOL Value # list de-weight # 1i=[1,2,33,33,2,1,4,5,6,6]# Set1=set (1i) # 1i=list (Set1) #frozenset不可变集合, make the collection immutable (cannot add delete). s = frozenset (' www ') print (S,type (s)) # Frozenset ({' W '})
Python Basic Learning str,list,dict,set,range,enumerate