美文网首页IT@程序员猿媛
class2-序列的公共方法与内置函数

class2-序列的公共方法与内置函数

作者: 凌航 | 来源:发表于2019-05-05 17:31 被阅读16次

公共方法

运算符

name python 表达式 结果 描述 支持的数据类型
+ [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串、列表、元组
* ['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] 复制 字符串、列表、元组
in 3 in (1, 2, 3) True 元素是否存在 字符串、列表、元组、字典
not in 3 not in (1, 2, 3) True 元素是否不存在 字符串、列表、元组、字典

+

>>> "hello " + "neusoft"
'hello neusoft'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')

*

>>> 'ab' * 4
'ababab'
>>> [1, 2] * 4
[1, 2, 1, 2, 1, 2, 1, 2]
>>> ('a', 'b') * 4
('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')

in

>>> 'neu' in 'hello neuedu'
True
>>> 3 in [1, 2]
False
>>> 4 in (1, 2, 3, 4)
True
>>> "name" in {"name":"Delron", "age":24}
True

注意:in在对字典进行操作时,判断的是字典的键

python内置函数

Python包含了以下内置函数

序号 方法 描述
1 len(item) 计算容器中元素个数
2 max(item) 返回容器中元素最大值
3 min(item) 返回容器中元素最小值
4 del(item) 删除变量

len

>>> len("hello itcast")
12
>>> len([1, 2, 3, 4])
4
>>> len((3,4))
2
>>> len({"a":1, "b":2})
2

注意:len在操作时,返回的时键值对个数

max

>>> max("hello itcast")
't'
>>> max([1,4,522,3,4])
522
>>> max({"a":1, "b":2})
'b'
>>> max({"a":10, "b":2})
'b'
>>> max({"c":10, "b":2})
'c'

del

del有两种用法,一种是del加空格,另一种是del()

>>> a = 1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = ['a', 'b']
>>> del a[0]
>>> a
['b']
>>> del(a)
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

相关文章

  • class2-序列的公共方法与内置函数

    公共方法 运算符 + * in 注意:in在对字典进行操作时,判断的是字典的键 python内置函数 Python...

  • Python公共方法,内置函数

    公共方法,内置函数

  • Python基础知识10: 容器类型公共方法

    一、公共方法的理解所谓公共方法就是:列表,元组,字典,字符串 都可以使用的方法 二、Python 内置函数函数描述...

  • 模块、面向对象

    1. 内置函数(下) filter(函数,序列) 执行结果: zip合并列表 执行结果 map(函数,序列) 执行...

  • python中的map、filter、reduce函数

    三个函数比较类似,都是应用于序列的内置函数。常见的序列包括list、tuple、str。 map函数 map函数会...

  • python 10天快速教程 Day4

    本节重点 递归函数 匿名函数 python内置函数 切片 列表生成式 内存地址 可变类型与不可变类型详解 公共运算...

  • 公共语法

    公共语法 5.1 Python 内置函数 Python 包含了以下内置函数: 5.2 切片 5.3 运算符 成员运...

  • 面向对象基本语法

    目标 ●dir内置函数●定义简单的类(只包含方法)●方法中的self函数●初始化方法●内置方法和属性 01.内置函...

  • varnish变量

    VCL 内置公共变量 VCL 内置的公共变量可以用在不同的 VCL 函数中,下面根据使用的不同阶段进行介绍 当请求...

  • class2-序列解析以及函数定义

    解析解析应该会比较常用,先看下面几个例子: {'Student1': 60, 'Student2': 76, 'S...

网友评论

    本文标题:class2-序列的公共方法与内置函数

    本文链接:https://www.haomeiwen.com/subject/wzmhoqtx.html