python内置函数

abs()

返回参数的绝对值。

>>> help(abs)
Help on built-in function abs in module builtins:

abs(x, /)
    Return the absolute value of the argument.
#例子
>>> abs(-5)
5

divmod()

返回元组值分别为商和余数

>>> help(divmod)
Help on built-in function divmod in module builtins:

divmod(x, y, /)
    Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.
#例子
>>> divmod(5,3)
(1, 2)



range()

先让我们看一下range()的函数说明

   range(stop) -> range object
   range(start, stop[, step]) -> range object

Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. 
 range(i, j) produces i, i+1, i+2, ..., j-1.
 start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements。
When step is given, it specifies the increment (or decrement).

先让我们看一下range()的常规使用,下面代码,分别为设置1个参数(默认从0开始,指定值结束,步长为1),2个参数(从指定数开始,结束,步长为1),3个参数(从指定数开始,结束,指定步长)。

>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list(range(5,9))
[5, 6, 7, 8]
>>> list(range(1,9,2))
[1, 3, 5, 7]

既然range能够生成指定的列表,那么能不能生成负列表,小数列表呢?可以发现 range()步长可以为正也可以为负,但只能整数步长

# 浮点步长
>>> list(range(1,3,0.2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

# 负步长
>>> list(range(2,-3,-1))
[2, 1, 0, -1, -2]


应用

如果让你计算50以内能够被3整除的数,你会怎么做呢?还在想用for循环遍历吗? 使用range()能够轻松解决:

>>> list(range(3,100,3))
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

在用列表的时候,有的时候我们想使用例如5以内所有数的平方,这时候该如何创建呢?

>>> dis=[i**2 for i in range(1,6)]
>>> dis
[1, 4, 9, 16, 25]



zip()

先让我们看一下函数说明:

zip(iter1 [,iter2 [...]]) --> zip object

 Return a zip object whose .__next__() method returns a tuple where the i-th element comes 
 from the i-th iterable argument(第i个迭代实参). 
The .__next__() method continues until the shortest iterable in the argument sequence is 
exhausted and then it raises StopIteration.

简言之,就是以最短迭代参数为基准,压缩成元组到一个zip对象中。

a = [1, 2, 3, 4]
b = ['a', 'b', 'c', 'd']
c = [9]
print(list(zip(a)))
print(list(zip(a, b)))
print(list(zip(a, b, c))

# 输出结果
[(1,), (2,), (3,), (4,)]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
[(1, 'a', 9)]

应用

对于实现两个列表对应元素相加,可以先zip压缩然后通过for循环。

a = [1, 2, 3, 4]
b = [5, 8, 9, 2]
c = []
for i, j in zip(a, b):
    c.append(i + j)
print(c)

# 输出结果
[6, 10, 12, 6]

因为两个列表压缩后每项元素是一个二元元组,所以我们可以通过这个格式创建字典。

a = ['name', 'sex']
b = ['zhangsan', 'man']
print(dict(zip(a, b)))

# 输出结果
{'name': 'zhangsan', 'sex': 'man'}



enumerate()

先让我们看一下enumerate()的函数说明:

class enumerate(object)
 |  enumerate(iterable, start=0)
 |  Return an enumerate object.
 |    iterable
 |      an object supporting iteration
 |  The enumerate object yields pairs containing a count (from start, which
 |  defaults to zero) and a value yielded by the iterable argument.
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

简言之,enumerate()就是将所给的可迭代的对象,以(0, seq[0]), (1, seq[1]), (2, seq[2]), …格式输出。

# 字符串
>>> str="asd"
>>> list(enumerate(str))
[(0, 'a'), (1, 's'), (2, 'd')]

# 列表
>>> lis=["asd",'name',1,['i',2]]
>>> list(enumerate(lis))
[(0, 'asd'), (1, 'name'), (2, 1), (3, ['i', 2])]

应用

如果我们想同时知道列表中第i个元素所对应的k值中的i和k,使用enumerate()就能很好实现:

a = ['name', 'sex']
for i, k in enumerate(a, start=1):
    print("第", i, "个元素是:", k)

# 输出结果1 个元素是: name
第 2 个元素是: sex

未完待续......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值