Created
December 29, 2018 05:36
-
-
Save caoya171193579/ebd22ebe79d1fe739fcf60e92ed91eda to your computer and use it in GitHub Desktop.
进阶
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
内置函数: 比较重点,自我感觉 | |
案例: | |
1、返回数字绝对值 | |
2、取列表最大最小值 | |
 | |
x大于10,返回20-20 | |
root@kali:~/xuexi# python return.py | |
0 | |
 | |
x小于10,返回8 (return)返回值。 | |
root@kali:~/xuexi# python return.py | |
8 | |
############################################### | |
常用函数: | |
绝对值、最大最小值 | |
abs() 绝对值 、max() 最大值、min() 最小值 | |
abs(): 负数全部返回绝对值正数。 | |
>>> abs(-156) | |
156 | |
>>> abs(-1583) | |
1583 | |
max() 、min() : 先要定义一个序列 | |
>>> l = [211,2213,132,423,5334,312312,"sdd"] | |
>>> max(l) #取最大值,字符串英文在python里面先运算,要比整数型大所有取字符串"sdd" | |
'sdd' | |
>>> min(l) #取最小值 | |
132 | |
>>> | |
############################################# | |
最重要的帮助函数 help() | |
查询序列、列表、字典的长度,有多少个元素: | |
len() | |
>>> s = [123,412412,'sssf',211,(1,2)] | |
>>> len(s) | |
5 | |
>>> d = "wdqwdqw23132423525" | |
>>> len(d) | |
18 | |
>>> a = {21:'sdewdwe',213:3444,"ff":2141241} | |
>>> len(a) | |
3 | |
#取参数的商和余数,也就是当不能整除的时候10/3 ,得3余1,3就是商,1就是余数。 | |
divmod() | |
>>> divmod(10,3) | |
(3, 1) | |
>>> divmod(10,5) | |
(2, 0) | |
>>> divmod(5,5) | |
(1, 0) | |
>>> divmod(1,5) | |
(0, 1) | |
>>> divmod(100,100) | |
(1, 0) | |
>>> divmod(100,99) | |
(1, 1) | |
>>> help(divmod) #查看此函数的用法。 | |
>>> | |
Help on built-in function divmod in module __builtin__: | |
divmod(...) | |
divmod(x, y) -> (quotient, remainder) | |
Return the tuple (x//y, x%y). Invariant: div*y + mod == x. | |
(END) | |
#xy的y次幂,j就是次方,求x的y次方然后在除以z,求余数%z | |
#返回(x**y)% z pow(x,y[,z]) #z是可有可无的。 | |
pow() | |
>>> pow(15,4) #两个参数的时候就直接算出了次方。 | |
50625 | |
>>> pow(15,4,12) #4个15相乘除以12,除不尽,然后余9 | |
9 | |
>>> pow(10,4,12) | |
4 | |
>>> pow(3,3,4) | |
3 | |
#如昂的; 返回浮点数。 | |
round() | |
>>>round(10) | |
10.0 | |
>>>round(12312) | |
12312.0 | |
>>> round(1231.122) | |
1231.0 | |
>>> round(1231) | |
1231.0 | |
#以上全是关于数学的函数。 | |
######################################################## | |
#扣类包儿; 用来查看函数名有没有被定义,对象可不可以被调用。 | |
callable() | |
>>> callable(min) #min 是内置返回最小值的函数,返回True | |
True | |
>>> callable(f) #f 报错f未被做任何定义,可以定义 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'f' is not defined | |
>>> f=100 #定义f为变量 | |
>>> callable(f) # 不是函数,返回False | |
False | |
>>> def f(): #定义f为函数 | |
... pass | |
... | |
>>> callable(f) # 返回True | |
True | |
>>> | |
#一丝嗯思谈次; 用来查看对象 属于什么类型下面有类型表,如果是返回True,如果不是返回False。 | |
isinstance() | |
>>> isinstance(f,int) #用法 | |
True | |
>>> isinstance(f,str) | |
False | |
>>> | |
>>> def f(): | |
... pass | |
... | |
>>> isinstance(f,long) | |
False | |
>>> isinstance(f,str) | |
False | |
>>> type(f) | |
<type 'function'> | |
#上面只看括号前的类型名称 | |
#下面是数据类型转换 | |
int(x [,base ]) 将x转换为一个整数 | |
long(x [,base ]) 将x转换为一个长整数 | |
float(x ) 将x转换到一个浮点数 | |
complex(real [,imag ]) 创建一个复数 | |
str(x ) 将对象 x 转换为字符串 | |
repr(x ) 将对象 x 转换为表达式字符串 | |
eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象 | |
tuple(s ) 将序列 s 转换为一个元组 | |
list(s ) 将序列 s 转换为一个列表 | |
chr(x ) 将一个整数转换为一个字符 | |
unichr(x ) 将一个整数转换为Unicode字符 | |
ord(x ) 将一个字符转换为它的整数值 | |
hex(x ) 将一个整数转换为一个十六进制字符串 | |
oct(x ) 将一个整数转换为一个八进制字符串 | |
#用来比较两个字符串的大小,x>y返回1,x<y返回-1,x=y返回0. | |
cmp(x,y) | |
>>> cmp('qwde','qwde') | |
0 | |
>>> cmp('qwde','qwd') | |
1 | |
>>> cmp('qw','qwd') | |
-1 | |
#快速生成一个序列。用来缩减很长一段整数。。。1,2,3,4,5,6,7,8,9,10,可以写为range(1,11) | |
range() | |
>>> range(1,11) | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
>>> | |
#大数据运算会常用。!!! | |
xrange() | |
>>> range(1,11) | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
>>> xrange(100) | |
xrange(100) | |
>>> print xrange(100) | |
xrange(100) | |
>>> print range(100) | |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment