Basic data type supplement
str:
title case
name = 'alKx' name1 = name.capitalize() # title case print(name1)
Capitalize initial
name = 'alex haha' name1 = name.title() # Capitalize initial print(name1)
Case reversal
print(name.swapcase()) # Case reversal
Center, center fill
print(name.center(20, "*")) # Center, center fill
Splicing
print("-".join(name)) # Splicing, splicing with contents in quotation marks, key points
lookup
print(name.find("x")) # lookup # From left to right, only one is found, and - 1 is not returned. print(name.index("x")) # Index. In some cases, the index position is returned according to the position of the string. If not, the throne
Format
# %s # f # Fill in sequence position name = "alex,{}, {}, {}" print(name.format(1,2,3)) # tuple #Fill in by index value name = "alex,{2}, {0}, {1}" print(name.format(1,2,3)) # Fill by key name = "alex,{a}, {c}, {b}" print(name.format(a=11, b=22, c=99))
list definition method
list("123")
Other methods
Sort (default ascending)
lst = [1,2,23,234,435,36,23,213421,421,4231,534,65] lst.sort() print(lst) lst = ["Hello","I am good."] lst.sort() print(lst)
Descending order
lst = [1,2,23,234,435,36,23,213421,421,4231,534,65] lst.sort(reverse=True) # Descending order print(lst)
Reversal
lst.reverse() print(lst) Another way print(lst[::-1]) #Reverse, reverse; or use [:: - 1]
Sort before reverse
lst = [1, 2, 3, 4, 5123, 21345, 231123, 4, 1235, 234, 123] lst.sort() lst.reverse() print(lst) # First sort, then reverse; first sort, then reverse;
Interview questions
lst = [[]] new_lst = lst * 5 new_lst[0].append(10) print(new_lst) # The first level element is modified, so the source list is modified, and the new list is also changed. lst = [1,[]] new_lst =lst * 5 new_lst[0] = 10 print(new_lst) # The first level element of the new list is modified, and the source list is not modified. lst = [1,[]] new_lst =lst * 5 new_lst[1] = 10 print(new_lst) #Principle is the same as above.
Merge two lists
Method 1: lst = [1] lst1 = [2] lst.extend(lst1) print(lst) Method two: print(lst + lst1)
Lists can be multiplied by numbers
lst = [[]] new_lst = lst * 5 new_lst[0].append(10) print(new_lst)
tuple
The data type is () the data itself in brackets Adding a comma is a tuple. Adding two commas will result in an error. A tuple is an immutable list, which can be added, multiplied, not shared, or both.
tu = (1,) # (1,) is a tuple print(type(tu))
dic
The dictionary is out of order. Define a dictionary
print(dict(key=1, key2=2))
Randomly delete popitem
dic = {"key":1, "key2":2} print(dic.popitem()) # Random deletion, which returns the deleted key value pair print(dic) # Python 36, delete the last one by default
Batch addition
dic = {} dic1 = dic.fromkeys("123",[23]) print(dic1) # Batch add key value pair {"1": [23], "2": [23], "3": [23]} dic = dict.fromkeys("123", [23]) dic['1'] = [5] print(dic) # Batch add key value pairs {'1': [23]}, '2': [23], "3": [23]} dic = dict.fromkeys("123456789",1) dic["1"] = 18 print(dic) # To batch add key value to "key is an iterative object", the value -- will be shared
set
set() --- empty set {} ------ empty dictionary Define set: set("alex") (add iteratively
# bool: False # Number: 0 # String: '' # List: [] # Tuple: () # Dictionary: {} # Set: set() # Others: None
split cuts out a list.
Type conversion between data
list tuple tuple list # str list name = "alex" print(name.split()) # list str lst = ["1","2","3"] # print(''.join(lst)) split It's a list. # dict -- str dic = {"1":2} print(str(dic),type(str(dic))) print(dict("{1:1}")) //Convert list to string