逻辑运算符
and
or
not
is 和 == 的区别
is 比较id是否相等
== 比较value是否相等
list创建方式
- 方式一
list = [1,2,3,‘abc’] - 方式二
l1 = (1,2,3)
list1 = list(l1)
list访问列表元素
- 通过下标访问
list1 = ['hello','world','haha']
print(list1[2])
print(list1[-2])
- 切片
list1 = ['haha','heihei','huhu','hehe','xixi']
print(list1[0:3:1])
# list添加元素(被添加元素为一个整体)
- append 在列表末尾添加元素,可以是字符串、元组、列表
```python
list1 = ['python','java']
list1.append('shell')
print(list1)
list2 = (1,2,3)
list1.append(list2)
print(list1)
- extend 在列表末尾添加元素(只可添加可迭代元素)
list1 = ['python','java']
list1.extend('shell')
print(list1)
- insert 在指定位置添加元素
list = ['python','java']
list1 = list.insert(1,'shell')
print(list1)
- list删除元素
pop 根据索引删除
list = ['python','java']
list.pop(1)
list = ['python','java','c++']
del 根据索引删除
list.del(1)
list.del(0,2) #可以删除一段元素
remove 根据值删除
list = ['python','java','c++']
list.remove('java')
clear 将列表中的元素全部删除
list = ['python','java','c++']
list.clear()
- list查找元素
list = ['python','java']
#index整个列表搜索
list.index('java')#如果java不存在会报错valueError,要先用count判断元素是否存在
list.count('java')
#count也可以统计元素出现的次数
tuple元组
- 创建元组
#元组是不可变元素
tuple = ('qbc','dcd','cde')
list = [1,2,3]
tuple1 = tuple(list)
print(tuple1)
dict字典
- 特点
1、通过key来获取元素
2、任意数据类型的无序集合
3、key是唯一不可变的,列表不可以用作key
4、字典是可变的
- 创建字典
#方式一
pet = {'dog':'wang','cat':'miao','bird':'gege'}
dict1 = {'list1':[1,2,3],('a','b'):30}
#方式二 dict.fromkeys(list,value)
dog = ['小黄','小黑','小白']
dog_dict = dict.fromkeys(dog,'wangwang')
print(dog_dict)
#方式三
scores = dict('数学'=95,'语文'=90,'英语'=88)
print(scores)
people = [('women','女人'),('man','男人'),('boy','男孩'),('girl','女孩')]
people_dict = dict(people)
print(people)
demo_key = [1,2,3]
demo_value = ['one','two','three']
demo_dict = dict.zip(demo_key,demo_value)
#添加元素
dict1 = {'one':1,'two':2,'three':3}
dict1['four']=4
print(dict1)
访问字典元素
dict1 = {'one':1,'two':2,'three':3}
#通过key值访问
print(dict1('one'))
#通过get方法,key不存在可给默认值
print(dict1.get('one',2))
print(dict1.get('four',4))
判断key是否在字典中
dict1 = {'one':1,'two':2,'three':3}
print('one' in dict1)
print('one' not in dict1)
字典方法
#获取字典中所有key值
dict1 = {'one':1,'two':2,'three':3}
print(dict1.keys())
#获取字典中所有的value值
dict1 = {'one':1,'two':2,'three':3}
print(dict1.values())
#获取字典中的键值对
dict1 = {'one':1,'two':2,'three':3}
print(dict1.items)
#字典copy()
dict1 = {'one':1,'two':2,'three':3}
dict2 = dict1.copy()
print(dict2)
#更新字典update()不存在的key新增,存在的key更新value
dict1 = {'one':1,'two':2,'three':3}
dict3 = {'four':4,'five':5}
dict1.update(dict3)
print(dict1)
dict1 = {'one':1,'two':2,'three':3}
dict4 = {'one':'one'}
dict1.update(dict4)
print(dict1)
#删除键值对
#pop()删除指定key
dict1 = {'one':1,'two':2,'three':3}
dict1.pop('one')
print(dict1)
#popitem()删除任意键值对并返回为一个元组
dict1 = {'one':1,'two':2,'three':3}
# dict1.pop('one')
dict2 = dict1.popitem()
print(dict1,dict2)
#返回某个key对应的value,没有默认值为none
dict1 = {'one':1,'two':2,'three':3}
print(dict1.setdefault('hh'))
print(dict1.setdefault('one'))
print(dict1.setdefault('one',3))
set集合
#创建集合
set1 = {'one',1,'two',2,'three',3}
print(set1)
list1 = [1,2,3,4]
set2 = set(list1)
print(set2)
#set会自动去重
set3 = set('hello')
print(set3)
set4 = set(range(0,10))
print(set4)
#set添加元素add()
set1 = {'one',1,'two',2,'three',3}
tuple1 = (6,7,8)
set1.add(tuple1)
set1.add('four')
print(set1)
#set删除元素remove()
set1 = {'one',1,'two',2,'three',3}
set1.remove(1)
print(set1)
#set交集 setname.intersection(setname)
#交集:set1={1,2,3},set2={3,4,6},交集为{3}
set1 = {'one',1,'two',2,'three',3}
set2 = {'three',3,'four',4,'five',5}
set3 = set1.intersection(set2)
print(set3)
#set差集 setname1.difference(setname2)
#差集:set1={1,2,3},set2={3,4,6},差集为{1,2}
set1 = {'one',1,'two',2,'three',3}
set2 = {'three',3,'four',4,'five',5}
set4 = set1.difference(set2)
print(set4)
#set并集 setname1.union(setname2)
#并集:set1={1,2,3},set2={3,4,6},并集为{1,2,3,4,6}
set1 = {'one',1,'two',2,'three',3}
set2 = {'three',3,'four',4,'five',5}
set5 = set1.union(set2)
print(set5)
len()函数
#字符串、列表、元组、集合、字典
set1 = {'one',1,'two',2,'three',3}
print(len(set1))
len1 = '人生'
print(len(len1))
join()函数,合并字符串
#字符串!!!!
set_str = {'one','two'}
str1 = ''.join(set_str)
print(str1)
字符串格式化输出
- 方式一:%格式化字符串
%s 代表一段字符串
%d 代表一个整数
%f 代表一个浮点数
abc = 'xiaoming'
print('我的名字是:%s' % abc)
-方式二:format()方式
name = "I have a {}, He's name is {}".format('dog','一休')
print(name)
-方式三:f-string方式(花括号包裹字符串变量)
my_pet = 'dog'
pet_name = 'xiaohuang'
owner = f'I have a {my_pet}, He\'s name is {pet_name}'
print(owner)
dict1 = {'name':'xiaozhang','age':18}
print(f'my name is {dict1.get("name")},my age is {dict1.get("age")}')
name = 'XIAOMING'
name1 = 'xiaoming'
print(f'my name is {name.lower()}')
print(f'my name is {name1.upper()}')
推导式
- 列表推导式
格式:[变量 for 变量 in 列表 if 表达式]
list_one = [i for i in range(0,10)]
print(list_one)
list_two = [1,2,3,4,5,6,7,8]
td_list = [a for a in list_two if a>1]
print(td_list)
# b**2表示b的平方
td_list1 = [b**2 for b in list_two]
print(td_list1)
-字典推导式
格式:{变量 for 变量 in 列表 if 表达式}
dict_one = {'name':'zhangsan','age':67}
td_dict1 = {k:v for k,v in dict_one.items()}
td_dict2 = {k for k in dict_one.keys()}
print(td_dict1)
print(td_dict2)
-集合推导式(无序、不重复)
格式:格式:{变量 for 变量 in 列表 if 表达式}
set_one = {'dog','cat','bird','fish','pig'}
td_set1 = {p for p in set_one}
print(td_set1)
td_set2 = {v**2 for v in [1,2,3]}
print(td_set2)