Home >Backend Development >Python Tutorial >Detailed explanation of type conversion methods in python

Detailed explanation of type conversion methods in python

高洛峰
高洛峰Original
2017-03-17 15:53:371565browse

int(x [,base ]) Convert x to a integer

long(x [,base ]) Convert x to a long integer

float(x ) Convert x to a floating point number

complex(real [,imag ]) Create a complex number

str(x ) Convert object x Convert to string

repr(x ) Convert object x to Expression String

eval(str ) Used Evaluates a valid Python expression in a string and returns an object

tuple(s) Converts the sequence s into a tuple

list(s) Convert sequence s to a list

chr(x) Convert an integer to a character

unichr(x) Convert an integer to Unicode characters

ord(x) Convert a character to its integer value

hex(x) Convert an integer to a hexadecimal string

oct(x) Convert an integer to an octal string

The sequence supports the following operations:

Operation description

s + r Sequence connection

s * n, n * s n copies of s, n is an integer

s % d String formatting (only strings)

s [i] Index

s[i :j ] Slice

x in s , x not in s Dependency relationship

for x in s : Iteration

len(s) length

min(s) minimum element

max(s) maximum element

s[i] = x is s [i] Reassign

s[i :j ] = r Reassign the list fragment

del s[i ] Delete an element in the list

del s[i :j ] Delete a segment in the list

Operation description

s + r Sequence connection

s * n, n * s n copies of s, n is an integer

s % d string formatting (string only)

s[i] index

s[i :j ] slice

x in s , x not in s dependency relationship

for x in s: iteration

len(s) length

min(s) minimum element

max(s ) Maximum element

s[i ] = x Reassign s[i]

s[i :j ] = r Reassign the list fragment

del s[ i ] Delete an element in the list

del s[i :j ] Delete a fragment in the list

Numeric operation:

x << y shift left

x >> y Right shift

x & y Bitwise AND

x | y Bitwise OR

x ^ y Bitwise XOR( exclusive or)

~x Bitwise flip

x + y Add

x - y Subtract

x * y Multiply

x / y regular division

x // y floor division

x ** y raised to power(xy)

x % y modulo(x mod y)

-x Change the sign bit of the operand

+x Do nothing

~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 % modulo

round(x ,[n]) Rounding, n is the number of decimal places

x < y is less than

x > y is greater than

x == y is equal to

x != y is not equal to (same as <>)

x >= y is greater than or equal to

x < = y is less than or equal to


The above is the detailed content of Detailed explanation of type conversion methods in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn