Python int()函数 返回给定对象中的整数或将给定基数中的数字转换为十进制。
null
Python int()语法:
int(字符串,基)
Python int()参数:
- 字符串: 由1和0组成
- 基数: (整数值)数字的基数。
Python int()返回:
返回一个整数值,它相当于给定基中的二进制字符串。
Python int()异常和错误:
类型错误: 当字符串或整数以外的任何数据类型在其等效位置传递时,返回TypeError。
Python int()方法示例
示例1:在Python中使用int()函数
Python3
# Python3 program for implementation # of int() function num = 13 String = '187' # Stores the result value of # binary "187" and num addition result_1 = int (String) + num print ( "int('187') + 13 = " , result_1, "" ) # Example_2 str = '100' print ( "int('100') with base 2 = " , int ( str , 2 )) print ( "int('100') with base 4 = " , int ( str , 4 )) print ( "int('100') with base 8 = " , int ( str , 8 )) print ( "int('100') with base 16 = " , int ( str , 16 )) |
输出:
int('187') + 13 = 200 int('100') with base 2 = 4int('100') with base 4 = 16int('100') with base 8 = 64int('100') with base 16 = 256
示例2:将二进制字符串转换为Python int base
Python3
# Python3 program for implementation # of int() function # "111" taken as the binary string binaryString = "111" # Stores the equivalent decimal # value of binary "111" Decimal = int (binaryString, 2 ) print ( "Decimal equivalent of binary 111 is" , Decimal) # "101" taken as the octal string octalString = "101" # Stores the equivalent decimal # value of binary "101" Octal = int (octalString, 8 ) print ( "Decimal equivalent of octal 101 is" , Octal) |
输出:
Decimal equivalent of binary 111 is 7Decimal equivalent of octal 101 is 65
例3: 程序来演示类型错误
Python3
# Python3 program to demonstrate # error of int() function # when the binary number is not # stored in as string binaryString = 111 # it returns an error for passing an # integer in place of string decimal = int (binaryString, 2 ) print (decimal) |
输出:
TypeError: int() can't convert non-string with explicit base
示例4:Python int()异常
Python3
try : var = "Geeks" print ( int (var)) except ValueError as e: print (e) |
输出:
以10为基数的int()的文本无效:“极客”
申请:
它在所有的应用程序中都使用 标准转换 .例如,二进制到十进制、八进制到十进制、十六进制到十进制的转换。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END