python系统提供了下面常用的函数:
① 数学库模块(math)提供了很多数学运算函数;
注意:在调用系统函数之前,先要使用import 语句导入 相应的模块
该语句将模块中定义的函数代码复制到自己的程 序中,然后就可以访问模块中的任何函数,其方 法是在函数名前面加上"模块名.".
希望能帮到你.
数学相关
abs(a) : 求取绝对值.abs(-1)
sorted(list) : 排序,返回排序后的list.
类型转换
int(str) : 转换为int型.int('1') 1
float(int/str) : 将int型或字符型转换为浮点型.float('1') 1.0
str(int) : 转换为字符型.str(1) '1'
bool(int) : 转换为布尔类型. str(0) False str(None) False
enumerate(iterable) : 返回一个枚举对象.
相关操作
exec() : 执行python语句. exec('print("Python")') Python
type():返回一个对象的类型.
id(): 返回一个对象的唯一标识值.
help():调用系统内置的帮助系统.
isinstance():判断一个对象是否为该类的一个实例.
issubclass():判断一个类是否为另一个类的子类.
globals() : 返回当前全局变量的字典.
next(iterator[, default]) : 接收一个迭代器,返回迭代器中的数值,如果设置了default,则当迭代器中的元素遍历后,输出default内容.
reversed(sequence) : 生成一个反转序列的迭代器. reversed('abc') ['c','b','a']
all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reverse() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set()
delattr() help() next() setattr()
dict() hex() object() slice()
dir() id() oct() sorted()
①.、get() 返回指定键的值,如果值不在字典中返回default值.
语法:dict.get(key,default=None)
参数:
key 字典中要查找的键.
default 如果指定键的值不存在时,返回该默认值值.
例:
print("Name is:%s"% dict.get('Name')+"\n"+ "Age is:%d"% dict.get('Age'))
显示结果为:
Name is:alex
print("Value is %s" % dict)
python的常用内置函数
①abs() 函数返回数字的绝对值
dict()
{} ? ? ?#创建一个空字典类似于u={},字典的存取方式一般为key-value
help('math')查看math模块的用处
help(a)查看列表list帮助信息
dir(help)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
a
①.0
next(it)
id(a)
a=["tom","marry","leblan"]
list(enumerate(a))
oct(10)
①.0. bin() 返回一个整数 int 或者长整数 long int 的二进制表示
bin(10)
'0b1010'
'0b1111'
①.1.eval() 函数用来执行一个字符串表达式,并返回表达式的值
f=open('test.txt')
bool()
False
bool(1)
True
bool(10)
bool(10.0)
isinstance(a,int)
isinstance(a,str)
class ? User(object):
? ? def__init__(self):
class Persons(User):
? ? ? ? ? super(Persons,self).__init__()
float(1)
①0
float(10)
①.0.0
iter(a)
for i in iter(a):
... ? ? ? ? print(i)
...
tuple(a)
s = "playbasketball"
len(s)
len(a)
class User(object):
? ?def __init__(self,name):
? ? ? ? ? ? self.name = name
? def get_name(self):
? ? ? ? ? ? return self.get_name
? @property
? ?def name(self):
? ? ? ? ? ?return self_name
list(b)
range(10)
range(0, 10)
class w(object):
a = w()
getattr(a,'s')
complex(1)
(1+0j)
complex("1")
max(b)
class Num(object):
...? ? a=1
.. print1 = Num()
print('a=',print1.a)
a= 1
print('b=',print1.b)
print('c=',print1.c)
delattr(Num,'b')
Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'
hash("tom")
a= set("tom")
b = set("marrt")
a,b
({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})
ab#交集
{'t', 'm'}
a|b#并集
{'t', 'm', 'r', 'o', 'a'}
a-b#差集
{'o'}
以上就是土嘎嘎小编为大家整理的python常用函数保相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!