One or three-dollar operation
if True: name='a'else: name='B ' '# The above code is represented by a ternary operation:name="a"if Else " b "
Second, the method in the Class View
All things in Python are objects, objects are created by classes
Type View Object types
Dir (type name) View all the features available in the class
Help (type name) view the features provided in the class in detail
Help (Type name. Method name) To view the details of a feature in a class
Iii. types of common functions
1. Shaping
ABS (x)#return absolute Valuex+y,x-y,x*y,x/y#SubtractionX/Y#fetch Quotient, the number of floating-point numbers is divided to preserve remainderx//y#fetch, the number of floating-point number division remainder is 0X%y#Take surplusX**y#Power SquareCMP (x, y)#two number comparison, return TRUE or false equals 0Coerce (x, y)#Force two numbers to generate a tupleDivmod (x, y)#divide a tuple of quotient and remainderFloat (x)#Convert to floating point typeSTR (x)#Convert to StringHex (x)#convert to 16 binaryOct (x)#Convert 8 binary
int
2. Long integer, floating point type and shaping are basically similar
3. String
Name='ABC'name.capitalize ()#Capitalize first letterName.center (20)#Length 20 CenterName.center (20),"*" #Length 20 Center, others with * fillName.ljust (20)#length 20 left, rjust () rightName.count ('a')#the number of a in a stringName.count ('a', 0,10)#string Specifies the number of a in the rangeName.endswith ('BC')#whether the string ends in BCName.expandtabs (8)#convert Tab key to space, default 8 spacesName.find ("b")#looking for the subscript of character B, cannot find return-1 if there are more than just the first oneName.index ("b")#Find the subscript of the character B, can not find the errorName.isalnum ()#determine if it is a letter or a numberName.isalpha ()#determine if it is a letterName.isdigit ()#determine whether a numberName.islower ()#Determine if lowercaseName.isspace ()#determine if all spaces are blankName.isupper ()#whether to capitalize all changesName.lower ()#Full Change lowercaseName.upper ()#whether to capitalize all changesName.swapcase ()#uppercase to lowercase, lowercase to uppercaseName.replace ('cc','DD')#all replacements that matchName.strip ()#Remove SpacesName.split ("b")#split with B#Joinli=["AA","BB"]'*'. Join (LI)#connect the elements of the list with *#IstitleName="Aa"Name.title ()#change to Title, title (all capitals are headings)Name.istitle ()#Judging is not the title#PartitionName="AACCBB"Name.partition ("cc")#split into 3 parts (AA,BB,CC)Str
Format formatted in the 4 method
Name="I {0},age {1}" #by orderName.format ("Zhangsan", 18) name="I {0},age {1}"Li=["Zhangsan", 18]name.format (*li)#Send list to add *name="I {aa},age {bb}" #by nameName.format (aa="Zhangsan", bb=18) name="I {aa},age {bb}"DiC={"AA":"Zhangsan","BB": 18}name.format (**dic)#Dictionary plus * *
Translate conversion
ImportSTRINGA1="Myis"A2="1234"Trantab=string.maketrans (A1,A2)#make a corresponding table firstStrstr="My name is Zhang"PrintStrstr.translate (Trantab)#Replace the letters in the A1 in the strstr with the corresponding numbers in the A2.PrintStrstr.translate (Trantab," is")#Delete "is" in strstr and replace#Output Result:#na1e Zhang#na1e Zhang
4. List
li=[11,22,33,44# append li.count () # to find the number of occurrences li.extend ([55,66]) # extend to the original list # find the subscript of the character, can not find the error # inserts at the specified subscript position # deletes and returns the specified subscript value, and returns the last Li.remove ()# remove li.reverse ()# Removes the list order reversal If no subscript is specified Li.sort ()# sort
List
5. Tuples
tup= (# # # # # # # # # of occurrences # Find the subscript of a character # find the error # The elements of a tuple cannot be modified tup= ( #)# tuples in elements of a tuple can be modified tup= (1,2,3,[5,6])
tuple
6. Dictionaries
#dictionaries are unordered.dic={'AA': 123,'BB': 456}dic.clear ()#Clear ContentDic.get ('BB')#when no BB is found, the error returned is none.Dic.get ('BB','No')#No error will be returned if no BB is found.#ItemsDic.keys#Remove all keysDic.values#Remove all valueDic.pop ("AA")#DeleteDic.setdefault ("AA",[' A'])#add ' aa ' = ' 22 ' If there is no AA in the dictionary, it will remain unchangeda={"BB": 555,"cc": 666}dic.update (a)#integration into the DIC dictionary, if the key in a is present in DIC, the value of the key that exists in DIC is changed to the values of key in a, which does not exist and is directly integratedDict
Shallow copy, deep copy
ImportCopya={"a": 1,"b": [+],"C": 3}b=AC=a.copy ()#Shallow CopyD=copy.deepcopy (a)#Deep Copya["D"]=4Print(a)Print(b)Print(c)Print(d)#Output Result:#{' A ': 1, ' C ': 3, ' B ': [1, 2], ' d ': 4}#{' A ': 1, ' C ': 3, ' B ': [1, 2], ' d ': 4}#{' A ': 1, ' C ': 3, ' B ': [1, 2]} shallow copy#{' A ': 1, ' C ': 3, ' B ': [1, 2]} deep copya["b"].pop (0)Print(a)Print(b)Print(c)Print(d)#Output Result:#{' A ': 1, ' C ': 3, ' B ': [2], ' d ': 4}#{' A ': 1, ' C ': 3, ' B ': [2], ' d ': 4}#{' A ': 1, ' C ': 3, ' B ': [2]} shallow copy, the corresponding array of B in the dictionary is changed#{' A ': 1, ' C ': 3, ' B ': [1, 2]} deep copy, completely independentCopy
7. Set Set
# set is an unordered and non-repeating collection of elements a=[1,1,2,2< Span style= "color: #000000;" >]set (a) # redo a =set (range (1,5)) b =set (range (4,7)) A &b intersection a|b # "set a^b # intersection A-b # a A.issubset (b) a is not included in B. a.remove (1) # delete A.update (b) # b incorporates a
Set
Four, decoding code
Cases:
GBK "Unicode" "Utf-8
Utf-8 "Unicode" "GBK
Unicode can be encoded (encode) into GBK and Utf-8
GBK and Utf-8 can decode decode into Unicode
A= "Good" A is GBK encoded
A.decode (' GBK ') decodes the GBK into Unicode
A.decode (' GBK '). Encode (' Utf-8 ') decoded after encoding into Utf-8
Python Basics Second Article