身份验证
-
is;type(name) is int
返回值为true或false、
-
is not; type(name) is not int
返回值为true或false
三元运算
-
d = a if a > 15 else b d = 值1 if 条件A else值2 如果条件A成立,就取左边的值1,否则取值2
数据类型-列表
-
extend 合并
n1 = ["bob","as","sdf"] name = ["张三","李四","王二"] name.extend(n1) print(name) ['张三', '李四', '王二', 'bob', 'as', 'sdf']
-
列表嵌套
n1 = ["bob","as","sdf"] name = ["张三","李四","王二"] name.insert(1,n1) print(name) ['张三', ['bob', 'as', 'sdf'], '李四', '王二']
n1 = ["bob","as","sdf"] name = ["张三","李四","王二"] name.insert(1,n1) print(name) print(name[1][1]) ['张三', ['bob', 'as', 'sdf'], '李四', '王二'] as
-
删除操作
-
del删
name = ["张三","李四","王二","bob","as","sdf"] del name[1] print(name) ['张三', '王二', 'bob', 'as', 'sdf']
-
pop删
name = ["张三","李四","王二","bob","as","sdf"] print(name.pop()) print(name.pop(1)) sdf 李四
-
clear清空
name = ["张三","李四","王二","bob","as","sdf"] print(name.clear()) None
-
-
修改操作
name = ["张三","李四","王二","bob","as","sdf"] name[0]="123" print(name) ['123', '李四', '王二', 'bob', 'as', 'sdf']
-
查操作
name = ["张三","李四","王二","bob","as","sdf"] print(name.index("bob"))#返回从左开始匹配到的第一个eva的索引 print(name.count("bob"))#返回eva的个数 3 1
-
切片 #原列表没有变化
- 切片的特性是顾头不顾尾,即start的元素会被包含,end-1是实际取出来的值
name = ["张三","李四","王二","bob","as","sdf"] print(name[1:4]) print(name[1:-1]) print(name[1:]) #:后面不写 就是取到最后 print(name[:3]) #:前面不写 就是从头取 ['李四', '王二', 'bob'] ['李四', '王二', 'bob', 'as'] ['李四', '王二', 'bob', 'as', 'sdf'] ['张三', '李四', '王二']
- 倒着切。倒着切也是从左往右切
name = ["张三","李四","王二","bob","as","sdf"] print(name[-5:-1]) ['李四', '王二', 'bob', 'as']
- 步长
name = ["张三","李四","王二","bob","as","sdf"] print(name[0:-1:2]) #头:尾:步长 ['张三', '王二', 'as']
- 真正的倒着切:从右往左切。需要把步长设为负数。
name = ["张三","李四","王二","bob","as","sdf"] print(name[-1:-5]) print(name[-1:-5:-1]) #倒着切 [] ['sdf', 'as', 'bob', '王二']
- 全切
name = ["张三","李四","王二","bob","as","sdf"] print(name[::2]) ['张三', '李四', '王二', 'bob', 'as', 'sdf']
-
反转
-
切边:
name[::-1]
-
.reverse #把原来的列表进行反转
name = ["张三","李四","王二","bob","as","sdf"] name.reverse() print(name) #原列表发生了改变 ['sdf', 'as', 'bob', '王二', '李四', '张三']
-
字符串反转:
n = '银角大王' print(n[::-1]) 王大角银
-
-
-
排序
- .sort() #对原列表进行改变
number = [12,32,123,432,1,3,5543,123] number.sort() print(number) [1, 3, 12, 32, 123, 123, 432, 5543]
number = ['12','32','123','432','1','3','5543','123',"张三","李四","王二","bob","as","sdf"] number.sort() #对ASCII值进行排序 print(number) ['1', '12', '123', '123', '3', '32', '432', '5543', 'as', 'bob', 'sdf', '张三', '李四', '王二']
-
循环
- for循环
name = ["张三","李四","王二","bob","as","sdf"] for i in name: print(i) 张三 李四 王二 bob as sdf
数据类型-元组
- 元组不可修改-只读列表
name = ("apple","bob")
print(name[1])
bob
-
元组中有列表,可以更改列表的值;
为啥呢?因为元组只是存每个元素的内存地址,上面['金角大王','Jack' ]这个列表本身的内存地址存在元组里确实不可变,但是这个列表包含的元素的内存地址是存在另外一块空间里的,是可变的。
name = ("apple","bob",["张三","李四"])
name[2][0]="王五"
print(name)
('apple', 'bob', ['王五', '李四'])
- 元组本身不可修改,如果包含可修改的数据类型,那么被包含的数据类型可以被修改。
数据类型-字符串
- 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,‘或”“或”"中间包含的内容称之为字符串
s = "Hello,Eva! How are you?"
print(s[1])
e
- 按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序。
- 可以进行切片操作。
s = "Hello,Eva! How are you?"
print(s[::3])
HlE!oa u
- 不可变,字符串是不可变的,不能像列表一样修改其中某个元素,所有对字符串的修改操作其实都是相当于生成了一份新数
据。 - 字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r(原生字符)。
name = "jack\t\n"
print(name)
print("=========")
name1 = r"jack\t\n"
print(name1)
jack
=========
jack\t\n
- center() 填充
name = "GuanSuo"
print(name.center(20, '-'))
------GuanSuo-------
- count() 计算
name = "GuanSuoasbbb"
print(name.count("a"))
print(name.count("a", 1, 4))
2
1
- find() 找
name = "GuanSuoasbbb"
print(name.find("a"))
print(name.find("a", 2)) #从2开始找
2
2
- format()
#两种方法实现
s = "My name is %s , i am %s years old" %("alex",23) #1
print(s)
s1 = "My name is {0} , i am {1} years old" #2
print(s1.format("gs", 22))
s2 = "Welcome {name} to Apeland , you are No.{user_num} user."
print(s2.format(user_num=24, name="张三"))
My name is alex , i am 23 years old
My name is gs , i am 22 years old
Welcome 张三 to Apeland , you are No.24 user.
- join 拼接
name = ["shangsan","lisi","Wanger"]
print("? ".join(name))
shangsan? lisi? Wanger
- lower() #大写变成小些
name1 = "ASDasd"
print(name1.lower())
asdasd
- ljust() rjust()
str.ljust(width[, fillchar])
width -- 指定字符串长度。
fillchar -- 填充字符,默认为空格。
str = "this is string example....wow!!!"
print(str.ljust(50, '0'))
this is string example....wow!!!000000000000000000
000000000000000000this is string example....wow!!!
- strip() 删除首尾空白字符
- rstrup() 删除右边空白字符
- lstrup()删除左边空白字符
str1 = " Welcome to Guru99! "
after_strip = str1.strip()
print(after_strip)
Welcome to Guru99!
- replace() 替换 默认所有的都改
a = "My score is 580, not 580 very good."
print(a.replace("580", "666"))
print(a.replace("580", "666",1)) # 改一次
My score is 580, not 580 very good.
My score is 666, not 580 very good.
- split()split():就是将一个字符串分隔成多个字符串组成的列表
- 默认用空格分开
string.split("sep",num)
num为分隔次数,有sep时按sep的值分隔:- rsplit()从右开始。lsplit()从左开始
a = "aaaaa bbbbb."
print(a.split())
print(a.split("a",2))
['aaaaa', 'bbbbb.']
['', '', '', '', '', ' bbbbb.']
- swapcase() 大写变小写 小写变大写
a = "aaaaa BBBbb"
print(a.swapcase())
AAAAA bbbBB
- upper() 变成大写
a = "aaaaa BBBbb"
print(a.upper())
AAAAA BBBBB
- casefold() 字符串全变小写
a = "aaaaa BBBbb"
print(a.casefold())
aaaaa bbbbb
- capitalize() 首字母大写,其他都变成小写
a = "aaaaa BBBbb"
print(a.capitalize())
Aaaaa bbbbb
-
straswith() 判断字符串是否以指定字符或子字符串开头。返回值为 bool
endswith() 判断字符串是否以指定字符或子字符串结尾。返回值为 bool
-
语法:str.endswith("suffix", start, end) 或str[start,end].endswith("suffix")
用于判断字符串中某段字符串是否以指定字符或子字符串结尾。
str = "hello,i love python"
print("1: ",str.startswith("h"))
print("2:",str.startswith("l",2,10))# 索引 llo,i lo 是否以“n”结尾。
print("3:",str.startswith(""))#空字符
print("4:",str[0:6].startswith("h")) # 只索引 hello,
print("5: ",str[0:6].startswith("e"))
print("6: ",str[0:6].startswith(""))
print("7:",str.startswith(("h","z")))#遍历元组的元素, 存在即返回True,否者返回False print("8:",str.startswith(("k","m")))
1: True
2: True
3: True
4: True
5: False
6: True
7: True
数据类型-字典
-
key-value结构。
-
key必须为不可变数据类型、必须唯一。
-
可存放任意多个value、可修改、可以不唯一。
-
无序。
-
查询速度快,且不受dict的大小影响。