常用的内建函数

目录

 

1、abs函数

2、bool函数

3、dir函数

4、eval函数

5、exec函数

6、float函数

7、int函数

8、long函数

9、max和min函数

10、range函数

11、sum函数

12、文件的使用


1、abs函数

abs(10)    abs(-10)

2、bool函数

bool(0)——bool(1)   //只要非0,均返回True

bool(None)和bool('')返回False

3、dir函数

是“directory”的简写,可以返回关于任何值的相关信息。

4、eval函数

evaluate的简写,把一个字符串作为参数并返回它作为一个Python表达式的结果。

eval('print("wow")'):实际上会执行语句print("wow")

eval('10*5'):返回50

5、exec函数

exec函数和eval函数差不多,但它可以运行更复杂的程序。两者的不同在于eval返回一个值,而exec不会。

6、float函数

foat函数把字符串或者数字转换成“浮点数”。

7、int函数

int函数把字符串或者数字转换成整数,这样会把小数点后面的内容丢掉。

int(‘123.456’)是错误的

8、long函数

返回一个对象的长度,对于字符串则返回字符串中的字符个数。

9、max和min函数

numbers = [5, 4, 10, 30, 22]

print(max(numbers))

30

strings = 's, t, r, i, n, g, S, T, R, I, N, G'

print(max(strings))

t

10、range函数

range函数实际上返回了一个叫做迭代器的特殊对象,他能重复一个动作很多次。

11、sum函数

把列表中的元素加在一起并返回这个总和。

12、文件的使用

在Windows中打开文件

test_file = open('c:\test.txt')

text = test_file.read()

print(text)

写入到文件

test_file = open('c:\myfile.txt','w')

test_file.write('this is my test file')

test_file = open('c:\myfile.txt','w')

test_file.write('What id green and loud? A froghorn!')

test_file.close()

test_file = open('myfile.txt')

print(test_file.read())

What id green and loud? A froghorn!

原文地址:https://www.cnblogs.com/strawqqhat/p/10602519.html