python--内置函数

1、python内置函数:

类型转换 数学运算 常用    
int() max() all() range() help()
float() min() any() set() format()
long() sum() type() zip()  
str() abs()   enumerate()  
bool() pow()      
complex() round()      
dict() sorted()      
list() len()      
tuple()        

 

 

 

 

 

 

 

 

 

 

2、内置函数语法及使用:

(1)  int(x,base = 10) :将一个字符串或数字转换为整型;

参数:x:字符串或数字;base:进制数,默认十进制。

return:返回整型数据。

int(3.1415926)    --> 3
int(2.5,10)       -->报错 若 x 为纯数字,则不能有 base 参数,否则报错;其作用为对入参 x 取整
int("9",2)        -->报错,因为2进制无9
int("1.2")        -->报错,str须为整数
int("1001",2)     -->9  "1001"才是2进制格式,并转化为十进制数字9

(2) float(x) :将整数和字符串转换成浮点数:

参数:x 为整数或字符串;

return:浮点数

float(1)        --> 1.0
float(-123.6)   -->-123.6
float('123')    --> 123.0  字符串

(3) long(x,base = 10):将数字或字符串转换为一个长整型;

参数:x:字符串或数字;base:进制数,默认十进制

return :长整型

long()         --> 0L
long(1)        --> 1L
long("123")    --> 123L

(4) str(object = ' '):将对象转化为适于阅读的形式;

参数:object:对象;

return:返回对象的 string 格式;

s = 'RUNOOB'
str(s)    --> 'RUNOOB'
dict = {'runoob': 'runoob.com', 'google': 'google.com'};
str(dict)    --> "{'google': 'google.com', 'runoob': 'runoob.com'}"

(5) bool

(6) complex()

(7) dict()

(8) list (tuple):将元组转换为列表;

参数:tuple:要转换为元组的列表;

return:列表

aTuple = (123, 'xyz', 'zara', 'abc');
aList = list(aTuple)
print ("列表元素 : ", aList)     --> [123, 'xyz', 'zara', 'abc']

(9) tuple( iterable ):将列表转换为元组;

参数:iterable:转换为元组的可迭代序列;

return:元组

tuple([1,2,3,4])    --> (1, 2, 3, 4)
tuple({1:2,3:4})    --> (1, 3)  #针对字典 会返回字典的key组成的tuple
tuple((1,2,3,4))    --> (1, 2, 3, 4) #元组会返回元组自身

(1) type( object ) :返回对象的类型

type(name, bases, dict):返回新的类型对象;

world_alcohol = numpy.genfromtxt(file,delimiter=",")
print(type(world_alcohol))    --> <class 'numpy.ndarray'>

(2) help( [object] ) :查看函数或模块用途的详细说明;

参数:object:对象;

return:返回对象帮助信息;

help(numpy.genfromtxt)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章