I. Basic data types
1. Representation and conversion of each binary number
2 binary, 8-binary, 16-input data needs to be increased by 0b, 0o, 0x, respectively, before the number.
>>> #10 binary 10>>> 0b111 #2 binary 7>>> 0o777 #8 binary 511>>> 0xfeda # 16 binary 65242
The conversion methods for each binary, 2, 8, 10, 16, respectively, are Bin (), Oct (), int (), Hex ().
>>> Bin (16)#10 binary number converted to 2 binary'0b10000'>>> Bin (0o766)#8 binary number converted to 2 binary'0b111110110'>>> Oct (0XFFF)#16 binary number converted to 8 binary'0o7777'>>> Oct (0b1101)#2 binary number converted to 8 binary'0o15'>>> Int (0b1111)#2 binary number converted to 10 binary15>>> Int (0o117)#8 binary number converted to 10 binary79>>> Hex (0b11111111)#2 binary number converted to 16 binary'0xFF'>>> Hex (65535)#10 binary number converted to 16 binary'0xFFFF'
2. Boolean value
The value 0, null object, and ' None ' Boolean values in Python are all false, and all others are true.
>>> BOOL (1.1) True>>> bool (0x00) #16 binary value 0False>> > bool ('# empty string False>>> bool ([All]) True>> > bool (()) false>>> bool (None) False
3. String
* Multi-line strings can be represented by three quotation marks: "" "or" "" "".
>>> s='ab' >>> s'\na\nb'
You can see that the variable s uses three quotation marks at the time of definition, while the content of S is ' \na\nb ' in the command-line format, where ' \ n ' indicates the newline character that was entered when the definition was made. This can also be seen through s[0]:
>>> s[0]'\ n'
Here the ' \ n ' is also called an escape character, and the escape character is displayed in format when using the print () output, such as a newline character \ n the output line feed, \ t output a horizontal tab.
Print (s) AB
* Escape characters: typically invisible characters (such as \ n, \ t, and so on) or characters that conflict with the syntax of the language itself (for example, \ ' denotes ', \ \ = \)
# print out ' Hello \ n world! ' # \ n requires an escape character to represent s='hello \\nWorld'print (s)print( ' Hello \\n World ')
* Original string: all characters in the original string are WYSIWYG
>>> s=r'\nabc\n' # Here is the original string, where \ n is two characters >>> s[ 0] # access to the first character, that is, ' \' \\'# can be seen above the output of ' \ \ ', Here is an ordinary string, where the actual ' \ '>>> s[1'n'print is represented by a transfer string (s) \nabc\n>>> len (s)7
4. Sequence (str, list, tuple)
In Python, Str/list/tuple is an ordered combination of data that can be accessed using the subscript method for an ordered combination, or it can be sliced.
>>>'123'[2]'3'>>> (1,2,5) [0]1>>> [A.'a'][-1]'a'>>> (1,2,3,4,5) [: 5](1, 2, 3, 4, 5)>>>"12345"[-1:]'5'>>> [1,2,3,4,5,6,7,8,9,10][1:10][2, 3, 4, 5, 6, 7, 8, 9, 10]
5. Mutual conversion of STR and list/tuple
(1) Directly through the type conversion function:
>>> List (s)#string conversion to list['1','2','3','4','5']>>> tuple (s)#string conversion to a tuple('1','2','3','4','5')>>> Tuple (list (s))#list conversion to tuples('1','2','3','4','5')>>> list (tuple (s))#tuples converted to lists['1','2','3','4','5']>>>#you can see that the STR () method does not allow the list to be converted into the string content we want.>>>#the Str () method directly treats the contents of the parameter as a string.>>> STR ([All])'[1, 2, 3]'>>> STR ([All]) [0]'['
(2) List and tuple conversions to strings must depend on the Join function
>>>' '. Join (['1','2','3'])'1 2 3'>>>"'. Join ('a','b','s','123'))'abs123'
It is important to note that the join () method can only be used if the element is a string type of list and tuple.
(3) A special way to convert a string to a list: Split ()
>>> s='78,90,100'>>> S.split ()#when there are no parameters, the original string is divided by a space by default to form elements of the list[' A',' the',' About','78,90,100']>>> S.split (',')[' A',' -',' -']
(4) A string to remove the leading and trailing spaces, newline characters: strip ()
>>> s= " \n \ n 123 ABC 999 \ n " >>> S.strip () " 123 ABC 999 " > >> s " \n \ n 123 ABC 999 \ n " >>> S.strip (). Split () [ " 123 " , Span style= "COLOR: #800000" > " abc " , 999 " ]
(5) List sorting method: Sort ()/sorted ()
>>> l=[16,87,35,100,96,62,8,3,19]>>> L.sort ()#sort L in the default order (here is small to large), L becomes a new list>>> l1=[16,87,35,100,96,62,8,3,19]>>> Sorted (L1)#returns a sorted new list, L1 itself does not change[3, 8, 16, 19, 35, 62, 87, 96, 100]>>>l1[16, 87, 35, 100, 96, 62, 8, 3, 19]>>> t= (16,87,35,100,96,62,8,3,19) #tuples and strings can also use the sorted () method to return a sorted new list#but tuples and strings cannot use sort ()>>>sorted (t) [3, 8, 16, 19, 35, 62, 87, 96, 100]>>>T (16, 87, 35, 100, 96, 62, 8, 3, 19)>>> Sorted ('102814124128765')['0','1','1','1','1','2','2','2','4','4','5','6','7','8','8']
6. Set (set), Dictionary (dict)
(1) The set is unordered, so it cannot be accessed by subscript, and because it is unordered, it is indistinguishable in the set for duplicate elements, because there is no order to say, so there cannot be duplicate elements in the collection.
>>> {1,2,3,4,5}-{1,3,5} # Two set differential set {2, 4}>>> {1,2,3,4,5}|{ 6,7,8} # Find the collection {1, 2, 3, 4, 5, 6, 7, 8}>>> {1,2,3,4,5,6,7}&{1,3,5,7,9} # ask for intersection {1, 3, 5, 7}
(2) A dictionary is a special type of collection whose key value must be an immutable type. Immutable types include int/float/bool/str/tuple.
Python Knowledge point record one