python个人总结2.0
str()函数:
str函数是python内置的字符串函数,当str被当作变量名赋值后str内置函数不可用,例:
str=123 n=str(456) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> n=str(456) TypeError: 'int' object is not callable
确定参数类型的函数:type、isinstance
type()函数:
可用来确定参数的类型,例:
type(123) <class 'int'> type("123") <class 'str'> type(0.1) <class 'float'>
isinstance()函数
isinstance()函数要有两个参数,返回的是布尔类型的值,例:
isinstance(123,int) True isinstance("123",str) True isinstance(123,str) False
assert()断言函数
assert 3>5 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> assert 3>5 AssertionError
当assert后面的条件为假时程序崩溃,弹出一个Error
len()函数
打印元素长度
n="123456789"
len(n)
9
range()函数;借用list()函数打印元素
语法:range ( [ start , ] stop [ , step = 1 ] )
有三个参数 [ ]里面的可以选
生成一个从start参数的值开始开始到stop参数的值结束的数字序列 ,step为步长,例:
range(1,5):意思是从1开始到5结束一共四个元素
n=range(5,10,2) list(n) [5, 7, 9]
#第二种方法结果一样
list(range(5,10,2))
[5, 7, 9]
像列表添加元素
append()函数
在列表最后添加一个参数(只能添加一个!!)
n=[4,5,6] n.append(7) n [4, 5, 6, 7]
extend()函数
可以以列表的形式添加多个元素
n.extend([8,9,10])
n
[4, 5, 6, 7, 8, 9, 10]
insert()函数
可以在任意位置添加元素
n.insert(0,3)
n
[3, 4, 5, 6, 7, 8, 9, 10]
remove()删除函数
可以直接元素的名称删除
n.remove(4)
n
[3, 5, 6, 7, 8, 9, 10]
del语句
del n[5] n [3, 5, 6, 7, 8, 10]
从0开始删除了第5个元素
del n则会删除整个列表
pop()函数
会将最后一个元素取出
n.pop() 10
n
[3, 5, 6, 7, 8]
当pop()加入参数时,将会取出第几个元素
n.pop(0) 3 n [5, 6, 7, 8]
分片处理
从最左边开始到最右边结束但是取不到最右边
n[1:3] [6, 7] n[:3] [5, 6, 7] n[:] [5, 6, 7, 8] n[1:] [6, 7, 8]
利用分片进行拷贝列表
m=n[:]
m
[5, 6, 7, 8]
finish~

浙公网安备 33010602011771号