Python basic functions-int() and float()


The running environment of the following code is (IDLE(Python3.8 64-bit))


One, int() function

The int() function has two writing formats:

1. int([x]) format

The function of this format is to intercept the integer part of a number or convert a string into an integer . Simply put, it is to convert a string (the int() function does not accept numeric strings with decimals ) or a number (can be positive, negative, zero, and decimal) into an integer. This is the most commonly used format and function.

The relevant code tests are as follows:

>>> int()#如果不给参数,则返回 0
0
>>> int(0)
0
>>> int(1.23)#加号 “+” 对值无影响 故一般省略
1
>>> int(-1.23)
-1
>>> int('4')
4
>>> int("4")#加号 “+” 对值无影响 故一般省略
4
>>> int('-4')
-4
>>> int('1.23')#int()函数不接受带小数的数字字符串
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    int('1.23')
ValueError: invalid literal for int() with base 10: '1.23'
>>> int('-1.23')#int()函数不接受带小数的数字字符串
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    int('-1.23')
ValueError: invalid literal for int() with base 10: '-1.23'
>>> int('a')#单纯这样写是不可的!
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    int('a')
ValueError: invalid literal for int() with base 10: 'a'
>>> 

Attach pictures
Insert picture description here

2. int(x,base=10) format

The function of this format is to convert the string x in base hexadecimal to decimal , where base can choose the base by itself, and its value range is 0 and 2~36. The default radix of base is 10 and x is a decimal string by default .

The relevant code tests are as follows:

>>> int('2ef',16)#将十六进制的字符串2ef转换为十进制
751
>>> int('027',8)#将八进制的数027转换为十进制
23
>>> int('100100',10)#将十进制的数100100转换为十进制
100100
>>> int('100100',base = 0)#base为0,此时和base取10一样,将十进制的数100100转为十进制
100100
>>> int('100100')#base默认基数为10
100100
>>> int('100100',2)#将二进制的数100100转换为十进制
36
>>> int('100100',8)#将八进制的数100100转换为十进制
32832
>>> int('100100.0')#特别注意:这里同样不接受带小数的数字字符串
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    int('100100.0')
ValueError: invalid literal for int() with base 10: '100100.0'
>>> int('123',2)#特别注意:base必须要与前面的x相匹配。就比如‘123’,它本就不是一个正常的二进制数,故会报错
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    int('123',2)
ValueError: invalid literal for int() with base 2: '123'
>>>#最后在温馨提醒一下base的取值范围 base=0 and 2<=base<=36

Attach pictures

Insert picture description here

Two, float() function

float(x = 0, /) format

For the float() function, it has only one format: float([x]). The function of this format is to convert a number or character string into a floating point number (return a decimal floating point value (decimal number)). Somewhat similar to int([x]) above.

The value of x in float([x]) can be a decimal number (decimal and integer), bool type (boolean value True and False) or a string representing a decimal number (eg: '7','-7', etc.) .

The relevant test code is as follows:

>>> float()#如果未给定参数,则返回0.0。
0.0
>>> float(1)#如果参数是整数,将返回等值的浮点数(Python的浮点精度内)
1.0
>>> float(1.233)#如果参数是浮点数,将返回等值的浮点数(Python的浮点精度内)
1.233
>>> float(-1.233)#这里需要注意的是,如果参数超出Python浮点范围,将会引发OverflowError错误
-1.233
>>> float('7')
7.0
>>> float('-7.7')
-7.7
>>> float(True)#布尔值True 对应1.0
1.0
>>> float(False)#布尔值False 对应0.0
0.0
>>> float('inf')#“inf”、“Inf”、“INFINITY”和“iNfINity”都是可以被float接受的正无穷大的拼写方式
inf
>>> float('Inf')
inf
>>> float('INFINITY')
inf
>>> float('-inf')#负无穷大
-inf
>>> 

Guess you like

Origin blog.csdn.net/weixin_46658699/article/details/110098537