
正文
python基本函数汇总 python的常见函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python的函数参数总结
import math
a = abs
print(a(-1))
n1 = 255
print(str(hex(n1)))
def my_abs(x):
# 增加了参数的检查
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x = 0:
return x
else:
return -x
print(my_abs(-3))
def nop():
pass
if n1 = 255:
pass
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
x, y = move(100, 100, 60, math.pi / 6)
print(x, y)
tup = move(100, 100, 60, math.pi / 6)
print(tup)
print(isinstance(tup, tuple))
def quadratic(a, b, c):
k = b * b - 4 * a * c
# print(k)
# print(math.sqrt(k))
if k 0:
print('This is no result!')
return None
elif k == 0:
x1 = -(b / 2 * a)
x2 = x1
return x1, x2
else:
x1 = (-b + math.sqrt(k)) / (2 * a)
x2 = (-b - math.sqrt(k)) / (2 * a)
return x1, x2
print(quadratic(2, 3, 1))
def power(x, n=2):
s = 1
while n 0:
n = n - 1
s = s * x
return s
print(power(2))
print(power(2, 3))
def enroll(name, gender, age=8, city='BeiJing'):
print('name:', name)
print('gender:', gender)
print('age:', age)
print('city:', city)
enroll('elder', 'F')
enroll('android', 'B', 9)
enroll('pythone', '6', city='AnShan')
def add_end(L=[]):
L.append('end')
return L
print(add_end())
print(add_end())
print(add_end())
def add_end_none(L=None):
if L is None:
L = []
L.append('END')
return L
print(add_end_none())
print(add_end_none())
print(add_end_none())
def calc(*nums):
sum = 0
for n in nums:
sum = sum + n * n
return sum
print(calc(1, 2, 3))
print(calc())
l = [1, 2, 3, 4]
print(calc(*l))
def foo(x, y):
print('x is %s' % x)
print('y is %s' % y)
foo(1, 2)
foo(y=1, x=2)
def person(name, age, **kv):
print('name:', name, 'age:', age, 'other:', kv)
person('Elder', '8')
person('Android', '9', city='BeiJing', Edu='人民大学')
extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **extra)
def person2(name, age, *, city, job):
print(name, age, city, job)
person2('Pthon', 8, city='BeiJing', job='Android Engineer')
def person3(name, age, *other, city='BeiJing', job='Android Engineer'):
print(name, age, other, city, job)
person3('Php', 18, 'test', 1, 2, 3)
person3('Php2', 28, 'test', 1, 2, 3, city='ShangHai', job='Pyhton Engineer')
def test2(a, b, c=0, *args, key=None, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'key=', key, 'kw =', kw)
test2(1, 2, 3, 'a', 'b', 'c', key='key', other='extra')
args = (1, 2, 3, 4)
kw = {'d': 99, 'x': '#'}
test2(*args, **kw)
相关问答
Q1: python内置函数
python内置函数是什么?一起来看下吧:
python内置函数有:
abs:求数值的绝对值
abs(-2) 2
pmod:返回两个数值的商和余数
pmod(5,2) (2,1) pmod(5.5,2) (2.0,1.5)
bool:根据传入的参数的逻辑值创建一个布尔值
bool() #未传入参数 False bool(0) #数值0、空序列等值为False False bool(1) True
all:判断可迭代对象的每个元素是否都为True值
all([1,2]) #列表中每个元素逻辑值均为True,返回True True all(()) #空元组 True all({}) #空字典 True
help:返回对象的帮助信息
help(str) Help on class str in module builtins: class str(object) | str(object='') - str | str(bytes_or_buffer[, encoding[, errors]]) - str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'. | | Methods defined here: | | __add__(self, value, /) Return self+value.
_import_:动态导入模块
index = __import__('index') index.sayHello()
locals:返回当前作用域内的局部变量和其值组成的字典
def f(): print('before define a ') print(locals()) #作用域内无变量 a = 1 print('after define a') print(locals()) #作用域内有一个a变量,值为1 f f() before define a {} after define a {'a': 1}
input:读取用户输入值
s = input('please input your name:') please input your name:Ain s 'Ain'
open:使用指定的模式和编码打开文件,返回文件读写对象
# t为文本读写,b为二进制读写 a = open('test.txt','rt') a.read() 'some text' a.close()
eval:执行动态表达式求值
eval('1+2+3+4') 10
除了上述举例的函数之外,内置函数按分类还可分为:
1、数学运算(7个)
2、类型转换(24个)
3、序列操作(8个)
4、对象操作(7个)
5、反射操作(8个)
6、变量操作(2个)
7、交互操作(2个)
8、文件操作(1个)
9、编译操作(4个)
10、装饰器(3个)
Q2: python常用列表函数
1
len(list)
列表元素个数
2
max(list)
返回列表元素最大值
3
min(list)
返回列表元素最小值
4
list(seq)
将元组转换为列表
序号
方法
1
list.append(obj)
在列表末尾添加新的对象
2
list.count(obj)
统计某个元素在列表中出现的次数
3
list.extend(seq)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4
list.index(obj)
从列表中找出某个值第一个匹配项的索引位置
5
list.insert(index, obj)
将对象插入列表
6
list.pop([index=-1])
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7
list.remove(obj)
移除列表中某个值的第一个匹配项
8
list.reverse()
反向列表中元素
9
list.sort( key=None, reverse=False)
对原列表进行排序
10
list.clear()
清空列表
11
list.copy()
复制列表
Q3: python常用函数
1、complex()
返回一个形如 a+bj 的复数,传入参数分为三种情况:
参数为空时,返回0j;参数为字符串时,将字符串表达式解释为复数形式并返回;参数为两个整数(a,b)时,返回 a+bj;参数只有一个整数 a 时,虚部 b 默认为0,函数返回 a+0j。
2、dir()
不提供参数时,返回当前本地范围内的名称列表;提供一个参数时,返回该对象包含的全部属性。
3、divmod(a,b)
a -- 代表被除数,整数或浮点数;b -- 代表除数,整数或浮点数;根据 除法运算 计算 a,b 之间的商和余数,函数返回一个元组(p,q) ,p 代表商 a//b ,q 代表余数 a%b。
4、enumerate(iterable,start=0)
iterable -- 一个可迭代对象,列表、元组序列等;start -- 计数索引值,默认初始为0‘该函数返回枚举对象是个迭代器,利用 next() 方法依次返回元素值,每个元素以元组形式存在,包含一个计数元素(起始为 start )和 iterable 中对应的元素值。
关于python基本函数汇总和python的常见函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







