It's really handy to write some small scripts in Python, but there are a number of data types involved in working with the data, so here's a collation of Python's type conversions.
Some of the more frequently Used (code tests are in the python2.7 environment) are listed first:
- Ord (x) converts the character to the corresponding ASCII code decimal value
>>> Ord ('4')52>>> ord ('a' )97
- Chr (x) converts the decimal ASCII value to the corresponding character
>>> chr (+)'A'
- Int (x[, base]) converts x to the corresponding binary number, x can be numeric characters (strings), integers, floating-point numbers. When using the base option, x must be a string type
>>> int ('4')4>>> int ('ten') 10>>> Int ('ten', +)16>>> Int (10) >>> Int (4.2)4
- UNICHR (x) converts a decimal ASCII value to a Unicode character
>>> UNICHR ($) u'B'
- Hex (x) converts integers to 16 binary strings
>>> hex'0xa'
Finally, some summaries of type conversion, sequence manipulation and numerical operation seen on the internet are listed.
function Description Int (x [, Base]) converts x to an integer long (x [, Base]) converts X to a long integer, float (x) converts x to a floating-point number complex (real [, Imag]) Create a complex number str (x) to convert an object x to a string repr (x) to convert an object x to an expression string eval (str) to evaluate a valid Python expression in a string and return an object tuple (s) Converts a sequence s to a tuple list (s) converts a sequence s to a list Chr (x) converts an integer to a character unichr (x) converts an integer to a Unicode character, Ord (x) Converts a character to its integer value hex (x) converts an integer to a hexadecimal string Oct (x) converts an integer to an octal string
Operation Description S+R sequence Connection s* N, N *N-Times copy of S S, N is an integer s%d string Formatting (string only) S[i] index s[i:j] Slice xinchS, x not inchs Dependent relationship forXinchS: Iteration len (s) length min (s) min element max (s) max element S[i]=x is s[i] re-assigned S[I:J]=R re-assigns the list fragment to a valuedelS[i] Delete an element in a listdelS[I:J] Delete a fragment from the list
X <<y shift left X>>y shift right X&y bitwise AND x|y bitwise OR x^ Y Bitwise XOR (Exclusiveor)~x bitwise Flip X+y plus x-y minus x*y multiply by x/y General except X//y floor except x**y-exponentiation (XY) x%y modulo (x mod y)-x Change the symbol bit of the operand+x don't do anything.~x ~x=-(x+1) ABS (x) absolute value divmod (x, y) returns (int (x/y), X%y) Pow (x, y [, modulo]) returns (X* * y) x%moduloround (x, [n]) rounded, n is the number of decimal places x<y is less than x>y is greater than x==y equals x! = y does not equal (with <>same) x>=y is greater than or equal to x<= y is less than or equal to
Python type conversion