python int()
时间: 2023-12-13 15:04:23 浏览: 32
The int() function in Python is used to convert a given value into an integer. It takes a string, float or any other numeric value as input and returns the integer equivalent of that value. If the input value cannot be converted into an integer, it raises a ValueError.
Syntax:
int(x, base=10)
Parameters:
x: It can be a string, float or any other numeric value that needs to be converted to an integer.
base: It is an optional parameter that specifies the base of the given number. It can be any value between 2 and 36. By default, it is set to 10.
Example:
a = int(10.5) #converts float 10.5 to int 10
b = int("100") #converts string "100" to int 100
c = int("1010", 2) #converts binary string "1010" to int 10
d = int("A", 16) #converts hexadecimal string "A" to int 10
print(a)
print(b)
print(c)
print(d)
Output:
10
100
10
10
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)