Data Type Number (number) int (integer)
It is usually called an integer or integer. It is a positive or negative integer without a decimal point.
# Assign a value to a variable A = 100 # assign a value to multiple variables a = B = 100
Float (float type)
The floating point type consists of the integer part and the decimal part. The floating point type can also be expressed by scientific notation (2.5e2 = 2.5x102 = 250)
a = 3.45
Bool (Boolean)
# Built-in function bool (), convert other data types to bool type A = bool ("") B = truec = false
Complex (plural)
A complex number consists of a real number and a virtual number. It can be expressed by a + BJ or complex (A, B). The real part A and virtual part B of a complex number are float.
a = 4+5.6j
b = complex(4,4.5)
Type conversion functions
Function |
Description |
INT (X) |
Converts X to an integer. |
Float (X) |
Convert X to a floating point number. |
Ord (X) |
Returns the corresponding ASCII value for one character. |
Bin () |
Convert decimal to binary |
Oct (X) |
Convert decimal to octal |
Hex () |
Convert decimal to hexadecimal |
Hexadecimal Representation
>>> 0b1010 # 0b indicates binary 10 >>> 0o12 # 0o indicates octal 10 >>> 0x0a # 0x indicates hexadecimal 10
Operator
Operator |
Description |
& |
Bitwise AND operator: 1, the result of this bit is 1, otherwise it is 0 |
| |
Bitwise OR operator: if one is 1, The result bit is 1. |
^ |
Bitwise OR operator: 1 when the difference is. |
~ |
Bitwise inversion OPERATOR: returns the inverse of each binary bit of the data. That is, 1 is changed to 0, and 0 is changed to 1 .~ X is similar to-X-1 |
< |
Left moving OPERATOR: the number of digits to be moved by the number on the right of "<" indicates the number of digits to be moved. The value is discarded at the highest position and 0 is supplemented at the lowest position. |
> |
Right-moving OPERATOR: shifts all the binary numbers on the left several places to the right, and ">" the number on the right specifies the number of digits to move. |
% |
Remainder operation |
// |
Division operation |
Operation example
'''0000 0101 50000 1000 8''' print (5 ^ 8) #13 print (5 | 8) #13 print (5 & 8) #0 print (5> 1) #2 print (~ 100) #-101 Add 1 and then obtain the reverse
Note: % and // operations. If the result is a negative number, perform a downward round.
>>> 10%31>>> 10%-3-2>>> 10//33>>> 10//-3-4
Math)
Function |
Description |
ABS (X) |
Returns the absolute value of a number. For example, ABS (-10) returns 10. |
Round (X [, N]) |
Returns the rounding value of floating point X. If n is given, it indicates the number of digits rounded to the decimal point. |
Pow (x, y) |
The value after the X ** y operation. |
Max (x1, x2 ,...) |
Returns the maximum value of a given parameter, which can be a sequence. |
Min (x1, x2 ,...) |
Returns the minimum value of a given parameter, which can be a sequence. |
Math. Ceil (X) |
Rounded up. For example, math. Ceil (4.1) returns 5. |
Math. Floor (X) |
Round down. For example, math. Floor (4.9) returns 4. |
Math. SQRT (X) |
Returns the square root of number X. |
Retain two decimal places
# Retain two decimal places to automatically rounding a = 2.455 # solution 1 B = "% 0.2f" % A # string c = float (B) print (c) # result: 2.46 # solution 2 D = round (A, 2) # floatprint (d) # result: 2.46
NOTE: If round () is as far away from both sides, it will be retained to the even side. For example, both round (0.5) and round (-0.5) are retained to 0, while round (1.5) is retained to 2.
String (string) STR (string)
In python3, all strings are Unicode strings.
U/u: Unicode string (default)
R/R: unescaped original string
B: The prefix represents bytes.
# Convert to unicodea = B 'Hello everyone '. decode ("UTF-8") print (a) # Hello everyone # convert to bytesb =. encode ("UTF-8") print (B) # B 'Hello everyone'
String slices
# Slice (regardless of tail, step size) MSG = 'Hello world' print (MSG []) # hellprint (MSG []) # hlprint (MSG [0: -2]) # Hello wor
Slice inverted string
a = ‘hello everyone‘b = a[::-1]print(b)
Note: If the step is positive, the slice is left to right. If the index at the starting position is greater than the index at the ending position, the slice is empty. If the step is negative, the slice is left to right, if the index at the starting position is smaller than the index at the ending position, the slice is empty.
String Common Operations
Method |
Description |
Find () |
Mystr. Find (STR, start, end) to check whether STR is contained in mystr. If it is the initial index value, otherwise-1 is returned. |
Count () |
Mystr. Count (STR, start, end), returns the number of times STR appears in mystr between start and end. |
Replace () |
Mystr. Replace (str1, str2, 2), replace str1 in mystr with str2. If count is specified, replace it with no more than count |
Strip () |
Removes the characters and line breaks on both sides of the string. spaces are removed by default. |
Split () |
Mystr. Split ("", 2), slice mystr with space as the separator, return to the list |
Startwith () |
Str. startwith ("A") to determine whether the string starts with a specified character. A boolean value is returned. |
Isdigit () |
"123". isdigit (), used to determine whether a string is composed of digits. Only strings of pure numbers can be judged. |
Join () |
String. Join (SEQ), which uses string as the separator. All elements (represented by strings) in seq are combined into a new string. |
List)
Method |
Description |
Len (list) |
Number of list elements |
Max (list) |
Maximum number in the list |
List. Count (OBJ) |
Objects in the list |
List. Extend (SEQ) |
Append multiple values in another sequence at the end of the list. |
List. insert (index, OBJ) |
Insert element to list |
List. Pop ([Index =-1]) |
Removes an element from the list (the last element by default) and returns the value of this element. |
List. Remove (OBJ) |
Removes the first matching item of a value from the list. |
List. Reverse () |
Elements in the reverse list |
List. Sort (Key = none, reverse = false) |
Sort the original list |
List. Clear () Clear the list |
Clear list |
List deduplication
# Set. The type is set. No repeated elements a = {1, 3, 4, 5, 3, 1, 2, 4} print (A, type ()) # list deduplication A = [1, 3, 4, 5, 3, 1, 2, 4] # method 1 B = [] For I in: if I not in B: B. append (I) B. sort () print (B) # method 2 S = set (a) C = List (s) print (c) # deduplicated and non-disordered d = List (SET ()) d. sort (Key =. index) print (d)
String Conversion to list
a = "dwqrdfqwe"b = list(a)print(b) #[‘d‘, ‘w‘, ‘q‘, ‘r‘, ‘d‘, ‘f‘, ‘q‘, ‘w‘, ‘e‘]
# Generate the following list # [[3.4, 0, 0], [, 2,], [, 8, 12] L = [] for a in range (4): L1 = [] for B in range (5): l1.append (B * A) L. append (L1) print (l) ret = [[I * J for J in range (5)] For I in range (4)] # print (RET) print ('-' * 50) list1 = [21,32, 12,56, 23] # A = sorted (list1, reverse = true) # list1.sort (reverse = true) list1.reverse () # Flip print (list1) print (list1.index (21) Import randomrandom. shuffle (list1) print (list1) print ('-' * 50) L = [,] ret = "+ ". join (STR (x) for X in L) print (RET)
Case
Tuple
Tuples are an unchangeable list used to store multiple values. It is most suitable for storing multiple values that require only reading but not modifying.
Dict (dictionary)
Method |
Description |
Dict. Clear () |
Delete all elements in the dictionary |
Dict. Get (Key, default = none) |
Returns the value of the specified key. If the value is not in the dictionary, the default value is returned. |
Dict. setdefault (Key, default = none) |
Similar to the get () method, if the key does not exist in the dictionary, a key is added and the value is set to the default value. |
Dict. Items () |
Returns a list of traversal (Key, value) tuples. |
Dict. Keys () |
Obtain the keys in the dictionary. List () conversion is required. |
Dict. Values () |
Obtain the value in the dictionary. List () conversion is required. |
Del dict [Key] |
Delete key |
Dict. Pop (key) |
Returns the deleted Value. |
Dict. popitem () |
Returns and deletes a pair of keys and values in the dictionary randomly (usually deletes the end pair) |
Set (SET) add element
# Creating an empty set a = set () # adding element. update ([11,22, 33]). add (12) print (a) # {33, 11, 12, 22}
Delete Element
# Deleting an element. If the element does not exist, an exception A = {33, 11, 12, 22} A will occur. remove (11). discard (22) # randomly Delete. pop () print (a) # {12}
Set operation
A = set ('abracadaba') print (a) # {'R', 'D', 'C', 'B ', 'A'} B = set ('adcazam') print (B) # {'M', 'z', 'D', 'C ', 'A'} # intersection. Print (A & B) # {'A', 'D ', 'C'} # print (a-B), a special element in set a # {'R', 'B'} # print (B-A), a special element in Set B) # {'M', 'z'} # Union, set all elements of a and B, print (A | B) # {'M', 'R', 'D ', 'Z', 'C', 'B', 'A'} # sum of unique elements in A and B, and set-intersection print (a ^ B) # {'M', 'z', 'R', 'B '}
Set deduplication Mechanism
Call hash first, and then call Eq. EQ is not triggered every time. It is triggered only when the hash value is equal.
Variable type and immutable type
Number, string, and tuple)
Note: int () integers are in the [-5,257) resident memory. These integer objects are created in advance and will not be garbage collected. (Same ID)
Determine Data Type
The difference between isinstance and type is: isinstance (10, INT) type (10)
Type () is not considered as a type of parent class.
Isinstance () considers subclass as a parent class type.
01-Data Type