python中int函数_int()函数以及Python中的示例

python中int函数

Python int()函数 (Python int() function)

int() function is used to convert a string (that should contain a number/integer), number (integer, float) into an integer value.

int()函数用于将字符串(应包含数字/整数),数字(整数,浮点数)转换为整数值。

Syntax:

句法:

    int(value, [base=10])

Parameter:

参数:

  • value – source value (string, number(integer/float)) to be converted in an integer value.

    value –要转换为整数值的源值(字符串,数字(整数/浮点数))。

  • base – it’s an optional parameter with default 10, it is used to define the base of source value, for example: if source string contains a binary value then we need to use base 2 to convert it into an integer.

    base –这是一个可选参数,默认值为10,用于定义源值的基数,例如:如果源字符串包含二进制值,则需要使用基数2将其转换为整数。

Return value: int – returns an integer value.

返回值: int –返回一个整数值。

Example:

例:

    Input:
    a = "1001"
    print(int(a))
    
    Output:
    1001

    Input:
    a = 10.23
    print(int(a))
    
    Output:
    10

    Input:
    a = "1110111"
    print(int(a,2))
    
    Output:
    119

Python code to convert values to an integer

Python代码将值转换为整数

# python code to demonstrate example
# of int() function

a = 10.20
b = "1001"
c = "000"

print("a: ", a)
print("int(a): ", int(a))

print("b: ", b)
print("int(b): ", int(b))

print("c: ", c)
print("int(c): ", int(c))

Output

输出量

a:  10.2 
int(a):  10
b:  1001
int(b):  1001
c:  000
int(c):  0

Python code to convert different numbers systems (binary, octal and hexadecimal) to integer value (decimal)

Python代码可将不同的数字系统(二进制,八进制和十六进制)转换为整数值(十进制)

# python code to demonstrate example
# of int() function

a = "10101010"
print("a: ", int(a,2))

a = "1234567"
print("a: ", int(a,8))

a = "5ABC12"
print("a: ", int(a,16))

Output

输出量

a:  170
a:  342391
a:  5946386


翻译自: https://www.includehelp.com/python/int-function-with-example.aspx

python中int函数

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值