1. abs() 取绝对值函数
1 #!/usr/bin/env python
2 # _*_ coding: UTF-8 _*_
3 # Author:taoke
4 i = 100
5 print(abs(i))
6 i = -100
7 print(abs(i))
2.dict() 创建字典
1 print(dict({
"a":1,
"b":2,
"c":3}))
3.help() 帮助函数
4.min() 返回最小项iterable或最小的两个或两个以上的参数。
1 print(min([1,2,34,5,6,7,0,100,-2]))
运行结果:-2
5.setattr() 给对象相应的属性赋值
setattr(x, 'foobar', 123)等同于x.foobar = 123
6.all() 全部为True返回True,否者返回Flase
1 print(all([1,23,45,3,-1
]))
2 print(all([1,23,45,3,0,-1]))
运行结果:
TrueFalse
7.ascii() 把一个内存的对象变为可打印的字符串形式
8.bin() 把一个整形转换成二进制字符串
1 print(bin(1
))
2 print(bin(2
))
3 print(bin(3
))
4 print(bin(4
))
5 print(bin(5
))
6 print(bin(255))
运行结果:
0b10b100b110b1000b1010b11111111
9.dir()函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
1 print(dir([1,2,3,4,5]))
运行结果:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
10 hex() 转换一个整数对象为十六进制的字符串表示,比如像0x的格式。
1 #!/usr/bin/env python
2 # _*_ coding: UTF-8 _*_
3 # Author:taoke
4 #将一个整形数转换为十六进制的字符串
5 print(12
)
6 print(hex(12
))
7 print(type(hex(12
)))
8 print(hex(-127))
View Code
11 next() 返回迭代器的下一个项目
1 #!/usr/bin/env python
2 # _*_ coding: UTF-8 _*_
3 # Author:taoke
4 n = iter([i*i
for i
in range(10
)])
5 #print(type(n))
6 for i
in range(10
):
7 print(next(n))
View Code
运行结果:
1 0
2 1
3 4
4 9
5 16
6 25
7 36
8 49
9 64
10 81
View Code
12 slice() 切片操作
slice[start, stop, step]: 创建一个slice类型. slice(none)等价于冒号':'. 用法:s = slice(1,3), a=[1,2,3,4,5,6,7]. 则a[s] == a[1:3] == [2, 3]多个axis的切片只要把slice类型变量list起来就好~: [s1,s2,...]可以用于指定axis的切片. 比如要求a在第axis个轴的切片, 从start到end:slc = [slice(None)]*len(a.shape)slc[axis] = slice(start,end)a[slc]即是.
13 any():当传入空可迭代对象时返回False,当可迭代对象中有任意一个不为False,则返回True
1 print(any([0,1,2,3,4
]))
2 print(any(
'1234567890'))
3 #可迭代对象中有一项为True
4 print(any([1,
'']))
5 print(any(
''+
'1'))
6 #可迭代对象中无任意一项为True
7 print(any([0,
'']))
8 print(any(
''+
''))
View Code
运行结果:
TrueTrueTrueTrueFalseFalse
14 divmod():divmod(a,b)实现a除以b,然后返回商与余数的元组。如果两个参数a,b都是整数,那么会采用整数除法,结果相当于(a//b, a % b)。如果a或b是浮点数,相当于(math.floor(a/b), a%b)。
1 print(divmod(100,20
))
2 print(divmod(100.12,20))
View Code
运行结果:
(5, 0)(5.0, 0.12000000000000455)
15 id() 获取内存地址
1 vir1 = 1232
2 vir2 = 1232
3 vir3 = 22222
4 print(id(vir1))
5 print(id(vir2))
6 print(id(vir3))
View Code
运行结果:
440243524402435244024368
16 object()
官方文档:
Return a new featureless object. object is a base for all classes. It has the methods that are common to all instances of Python classes. This function does not accept any arguments.
Note:object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class.
翻译:
返回一个新的无特征对象。对象是所有类的基础。它拥有所有Python类实例都通用的方法。此函数不接受任何参数。
注意:对象没有__dict__,所以你不能任意属性分配给对象类的一个实例。
17 sorted()
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
其中第一个参数是可迭代对象,后面的参数都是具有默认值的,重点阐述如下:
1、cmp,比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0
2、key,主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序
3、reverse,是否反转,默认情况下不反转
1 def cmp_value(x,y):
2 if x >
y :
3 return 1
4 elif x <
y:
5 return -1
6 else:
7 return 0
8 so = sorted(
'this is a string'.split(
' '),cmp=
cmp_value)
9 print so
View Code
运行结果:
['a', 'is', 'string', 'this']
18 ascii() 返回一个可打印的对象字符串方式表示。当遇到非ASCII码时,就会输出\x,\u或\U等字符来表示。
a =
ascii(object)
print(a)
print(type(a))
运行结果:
<class 'object'><class 'str'>
19 enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
1 list1 = [1,2,3,4,5,6
]
2 for i,j
in enumerate(list1):
3 print(i,j)
View Code
20 input() 用来获取控制台的输入。
1 name = input(
"请输入姓名:")
2 print(
"名字:%s "%name)
View Code
运行结果:
请输入姓名:taoke名字:taoke
21 oct() 函数将一个整数转换成8进制字符串。
1 t = oct(123
)
2 print(type(t))
3 print(t)
View Code
运行结果:
<class 'str'>0o173
22 staticmethod() 返回一个静态函数对象,主要用来作为静态函数的修饰符。静态函数的特性是可以直接通过类命名空间访问,也就是说没有定义类实例也可以使用此函数;也可以通过类实例来访问。这跟JAVA或C++里的静态函数是一样的作用。
1 class testclass(object):
2 @staticmethod
3 def func1(a,b):
4 print(a , b)
5
6
7 testclass.func1(1,2)
View Code
运行结果:
1 2
23 bin() 转换一个整数x为二进制的字符串表示。如果参数x不是一个整数对象,可以通过重载__index__()函数来实现返回一个整数。
1 print(type(bin(123
)))
2 print(bin(123))
View Code
运行结果:
<class 'str'>0b1111011
24 eval() 将字符串str当成有效的表达式来求值并返回计算结果
1 str = input(
"请输入:")
2 obj =
eval(str)
3 print(obj)
View Code
运行结果:
请输入:1+2+3+410
25 int() 函数用于将一个字符串会数字转换为整型
1 print(int(1.23
))
2 print(int(0x123
))
3 print(int(
'123',10
))
4 print(int(
'123',16
))
5 print(int(
'123',8))
View Code
运行结果;
129112329183
26 open() 函数用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写。
27 str() 函数将对象转化为适于人阅读的形式。
28 bool() 函数
1 #整数0,浮点数0.0,空列表,空元组,空字典,空字符串,均为False
2 print bool(0.0),bool(0),bool([]),bool(()),bool({}),bool(
'')
3 #正数,负数均为True
4 print bool(8),bool(-3),bool(-3.9),bool(9.8
)
5 #非空列表,非空元组,非空字典,非空字符串均为True
6 print bool([2,3]),bool((3,4)),bool({
'name':
'song'}),bool(
'python')
View Code
运行结果:
False False False False False FalseTrue True True TrueTrue True True True
10 getattr() 、hasattr()、getattr()和delattr()
setattr():设置对象的某个方法或属性getattr():运用对象的某个方法或属性hasattr():判断对象是否存在某种方法或属性delattr():删除对象某种方法或属性
转载于:https://www.cnblogs.com/taoke2016/p/7502558.html