2-2.Python基础学习笔记day02-标识符、数据类型及运算符

2-2.Python基础学习笔记day02-标识符、数据类型及运算符

5、list列表
'''
列表:
本质:是一种有序的集合。
列表的创建:
list1 = [元素1,元素2,元素3,...]

#创建空列表
list1 = list()
list2 = []
注意:列表中的元素类型可以是任意python中的基本数据类型或者是自定义的数据类型

列表中元素的访问:
使用索引的方式访问
list1[index]
index取值范围[0,len(list1))
当index超出取值范围的时候会出现IndexError的错误【下标越界的错误】
取值可以为负,为负的时候从倒数第一个开始取

获取列表的长度:
len(list1)
功能:获取列表的长度

列表元素的替换
list1[index] = 值
功能:更改列表中指定下标处的值


列表的组合
list3 = list1+list2
功能:将list1中的元素与list2中元素取出组合成一个新的列表并且返回。


列表的重复
list2 = list1*n
功能:将list1中的元素重复n次输出到新的列表中

判断元素是否在列表中存在
元素 in 列表
功能:若存在返回True,否则返回False

列表的截取
list1[start,stop,step]
start默认0
stop默认len(list1)
step默认1,取值可以为负
若指定start与stop取值范围[start,stop)


二维列表:
列表中元素可以是Python的基本数据类型,也可以是自定义的数据类型。
当列表中存放的元素刚好又是列表的时候,我们可以称这个列表为二维列表
list1 = [列表1,列表2,...,列表n]

二维列表的访问:
list1[index1][index2]
index1:代表第几个列表
index2:代表列表中第几个元素
'''
list1 = list()
list2 = []
list3 = list((1, 2, 3))
list4 = ["a", "good"]

print(list1)  # []
print(list2)  # []
print(list3)  # [1, 2, 3]
print(list4)  # ['a', 'good']


list1 = ["hello",2,True,False,3.14]
print(list1[0:3:2])  # ['hello', True]

list2 = list1[:]
list3 = list1
print(id(list1))  # 36421768
print(id(list2))  # 36487560  生成新列表
print(id(list3))  # 36421768

print(list1[::-1])  # [3.14, False, True, 2, 'hello'] 倒序
print(list1[:3:-1])  # [3.14]
print(list1[3::-1])  # [False, True, 2, 'hello']


list1 = ["hello",2,True,False,3.14]
list2 = ["good",34,"nice"]
print(list1+list2)  # ['hello', 2, True, False, 3.14, 'good', 34, 'nice']
print(list1*3)  # ['hello', 2, True, False, 3.14, 'hello', 2, True, False, 3.14, 'hello', 2, True, False, 3.14]
print(True in list1)  # True
print(True in list2)  # False

list1[0] = "nice"
print(list1[0])  # nice
print(len(list1))  # 5

print(list1)  # ['nice', 2, True, False, 3.14]
# print(list1[10])  # 报错


list3 = [1,2,3,["hello",True]]
list4 = [[1,2,3],[4,5,6],list3]
print(list4)  # [[1, 2, 3], [4, 5, 6], [1, 2, 3, ['hello', True]]]
print(list4[-1][-1][0].upper())  # HELLO
print(list4[-1][::-1])  # [['hello', True], 3, 2, 1]


'''
list1.append(obj)
功能:在列表的末尾添加指定的对象

list1.extend(序列)
功能:将序列中的元素打碎追加到list1中,
注意:extend后面必须是序列

list1.insert(index,obj)
功能:将obj插入到指定的下标处,原下标处的元素依次顺延。

list1.pop(index)
功能:将指定下标处的元素删除,并且将删除的元素返回。
若index不指定则默认删除最后一个元素。

list1.remove(元素)
功能:将列表中第一个匹配的元素移除

list1.index(obj,start,stop)
功能:在list1中查找obj是否存在,若查找到则返回obj第一次查询到的下标值
若查找不到则报错。查询范围[start,stop),若不指定则查询整个列表。

list1.count(obj)
功能:统计obj在list1中出现的次数。

max(list1)
功能:返回列表中的最大值

min(list1)
功能:返回列表中的最小值

list1.reverse()
功能:将列表中的元素倒叙,操作原列表,不返回新的列表。

list1.sort(reverse=False)
功能:将list1中的元素进行升序排列【默认reverse=False】
当reverse为True的时候,降序排列。

list1.clear()
功能:清除列表中元素【列表还存在】

del list1
功能:直接删除列表

'''

list1 = ["hello1","good1","nice","good","hello"]

list1.append("hello")  # 添加字符串
list1.append(True)  # 添加boolen
list1.append(214)  # 添加number
list1.append(2.14)  # 添加number
list1.append([1,2,3])  # 添加列表
list1.append((1,2,3))  # 添加元组
list1.append({1,2,3})  # 添加集合
list1.append({1:"2",2:"3"})  # 添加字典
print(list1) # [' hello1', 'good1', 'nice', 'good', 'hello', 'hello', True, 214, 2.14, [1, 2, 3], (1, 2, 3), {1, 2, 3}, {1: '2', 2: '3'}]


list1 = []
list1.extend("hello")
list1.extend({1,2,4})
list1.extend([1,3,4])
list1.extend((34,66,677))
list1.extend(range(10))
print(list1)  # ['h', 'e', 'l', 'l', 'o', 1, 2, 4, 1, 3, 4, 34, 66, 677, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

list1 = ["hello1","good1","nice","good","hello"]
list1.insert(0, "world")
list1.insert(1, "world")
print(list1)  # ['world', 'world', 'hello1', 'good1', 'nice', 'good', 'hello']

print(list1.pop(0))  # world
list1.remove("good")
print(list1)  # ['world', 'hello1', 'good1', 'nice', 'hello']


list1 = ['world', 'hello1', 'good1', 'nice', 'hello']
print(list1.index("hello"))  # 4
print(list1.count("good"))  # 0

print(max(list1))  # world
print(min(list1))  # good1

list1.reverse()
print(list1)  # ['hello', 'nice', 'good1', 'hello1', 'world']

list1.sort(reverse=True)
print(list1)  # ['world', 'nice', 'hello1', 'hello', 'good1']

list1.clear()
print(list1)  # []

del list1

'''
基本类型的拷贝:
1.赋值拷贝/引用拷贝
特点:共用同一块内存区域,更改其中任意一个变量,其他的变量都会受到影响。
2.浅拷贝
list2 = list1.copy()
只适用于一维列表。
不完全内存拷贝,对于一维列表重新开辟了一块内存空间,但若出现二维列表的情况下,
因为二维列表存放在一维列表中存放的是列表的地址,因此,若出现二维列表相当于间接的引用了同一块
内存区域。

3.深拷贝
import copy
list2 = copy.deepcopy(list1)
完全内存拷贝,相当于将list1中的所有列表中元素重新复制了一份,对于多维的也重新开辟了
内存空间,因此它不会出现内存共用的情况。
'''
import copy


list1 = [1,2,3,4,[1,2,3,4]]
list2 = list1
print(id(list1))  # 43545608
print(id(list2))  # 43545608

list2[-1] = True
print(list2)  # [1, 2, 3, 4, True]
print(list1)  # [1, 2, 3, 4, True]


list1 = [1,2,3,4,[1,2,3,4]]
list3 = list1.copy()
list3[-1][-1] = True
print(id(list1))  # 43546888
print(id(list3))  # 43546824
print(list1)  # [1, 2, 3, 4, [1, 2, 3, True]]
print(list3)  # [1, 2, 3, 4, [1, 2, 3, True]]


list1 = [1,2,3,4,[1,2,3,4]]
list4 = copy.deepcopy(list1)
list4[-1][-1] ="hello"
print(list1)  # [1, 2, 3, 4, [1, 2, 3, 4]]
print(list4)  # [1, 2, 3, 4, [1, 2, 3, 'hello']]
print(id(list1))  # 43481160
print(id(list4))  # 43481352
6、tuple元组
'''
元组的定义:
本质:也是一种有序的集合。但与list不同,元组一旦初始化则不能修改。
元组的创建:
tuple1 =()  #创建空元组
tuple2 = tuple()  #创建空元组
tuple3 = (1,)  #创建只有一个元素的元组
tuple4 = 1,   #创建只有一个元素的元组
注意:当元组中只有一个元素的时候,我们必须在该元素的末尾添加一个逗号,来消除歧义。
元组小括号可以省略,但是逗号不能省略


获取元组元素的长度
len(tuple1)


元组元素的访问:
tuple1 = (元素1,元素2,...,元素n)
通过索引值/下标来进行访问
tuple1[index]  index的取值范围 [0,len(tuple1))
index取值可以为负,为负的时候,从右往左进行取值。

修改元组:
指的是在元组的内部添加一个可变对象,通过修改可变对象进而来修改我们的元组
在元组中一旦初始化则不能修改指的是,元组一旦创建,它对应元素指向不会再发生变化。

删除元组
del tuple1
'''
tuple1 = ()
tuple2 = tuple()
tuple3 = (1)
tuple4 = (1,)
tuple5 = 1,2,3,4,5
print(tuple1)  # ()
print(tuple2)  # ()
print(tuple3)  # 1 不是元组
print(tuple4)  # (1,)
print(tuple5)  # (1, 2, 3, 4, 5)

print(type(tuple1))  # <class 'tuple'>
print(type(tuple2))  # <class 'tuple'>
print(type(tuple3))  # <class 'int'>
print(type(tuple4))  # <class 'tuple'>
print(type(tuple5))  # <class 'tuple'>

tuple1 = (1,2,3,4,5,True,"hello",[1,2,3])

print(len(tuple1))  # 8
print(tuple1[5])  # True
print(tuple1[-2])  # hello
print(tuple1[-1])  # [1, 2, 3]
tuple1[-1][-1] = "good"
print(tuple1)  # (1, 2, 3, 4, 5, True, 'hello', [1, 2, 'good'])

del tuple1

'''
1.元组的连接/组合
使用"+"
tuple3 = tuple1+tuple2
功能:将tuple1与tuple2中的元素取出重新组合成一个新的元组并且返回。

2.元组的重复
tuple2 = tuple1*n
功能:将tuple1中的元素重复n次输出到新的元组中

3.元素 in 元组
判断元素是否在元组中存在,若存在则返回True,否则返回False

4.元组的截取
tuple1[start:end:step]
start:默认0
end:默认len(tuple1)
step:默认1,取值可以为负

5.max(tuple1)
功能:返回tuple1中最大值

6.min(tuple1)
功能:返回tuple1中最小值

7.tuple(list1)
功能:将列表转为元组

8.二维元组
tuple1 = (t1,t2,...,tn)
当元组中的元素刚好又是元组的时候,我们称这个元组为二维元组。

9.二维元组的访问:
tuple1[index1][index2]
index1:代表第几个元组
index2:元组中第几个元素

10.tuple1.count(x)
功能:统计x在tuple1中出现的次数

11.tuple1.index(obj,start,end)
功能:在tuple1中查找obj,若找到则返回第一匹配到的下标值,若找不到则报错
查询范围[start,end),若不指定则查询整个元组
'''

tuple1 = (1,2,3,"hello","good",True,12.34)
tuple2 = (4,5,6)
print(tuple1+tuple2)  # (1, 2, 3, 'hello', 'good', True, 12.34, 4, 5, 6)
print(tuple1*3)  # (1, 2, 3, 'hello', 'good', True, 12.34, 1, 2, 3, 'hello', 'good', True, 12.34, 1, 2, 3, 'hello', 'good', True, 12.34)
print(2 in tuple2)  # False
print(2 in tuple1)  # True
print(tuple1[:-1][1::-1][-1])  # 1
print(max(tuple2))  # 6
print(min(tuple2))  # 4
print(tuple([1,2,3,4]))  # (1, 2, 3, 4)

print(tuple2.count(5))  # 1
print(tuple1.index(3,0,3))  # 2

tuple3 = (tuple1,tuple2)
print(tuple3)  # ((1, 2, 3, 'hello', 'good', True, 12.34), (4, 5, 6))
print(tuple3[0][-1])  # 12.34


7、dict字典
'''
字典:
本质也是一个集合,是一个无序的集合,存储的时候以键值对的方式来进行存储。
key-value来进行存储
key要求:
1.字典中的key必须是唯一的
2.字典中的key必须是不可变对象
不可变对象:
str,number,bool,None,tuple
可变对象:
list,dict,set
'''

'''
字典的创建:
dict1 = {key:value,key2:value2,...,keyn:valuen}

元素访问:
dict1[key]
dict1.get(key)
使用上面两种方式我们都可以访问元素,使用get方法获取的时候,当key不存在的
时候,不会报错,而是返回None,若使用key直接获取,当key不存在的时候则报错。

添加元素/更改元素
dict1[key] = value
原因:字典中key不能重复,它对应的值只有一个,后面添加的会将前面的覆盖。

删除元素
dict1.pop(key)
功能:根据key删除对应的键值对,并且将对应的value值返回。
'''
scoredict = {"张三":89,"李四":80,"王二":20,"麻子":67}
print(scoredict["王二"])  # 20
# print(scoredict["王三"])   # 报错 KeyError: '王三'
print(scoredict.get("王二"))  # 20
print(scoredict.get("王三"))  # None
scoredict["韩梅梅"] = 90
print(scoredict)  # {'张三': 89, '李四': 80, '王二': 20, '麻子': 67, '韩梅梅': 90}

scoredict["张三"] = 88
print(scoredict)  # {'张三': 88, '李四': 80, '王二': 20, '麻子': 67, '韩梅梅': 90}

print(scoredict.pop("麻子"))  # 67
print(scoredict)  # {'张三': 88, '李四': 80, '王二': 20, '韩梅梅': 90}


'''
字典的遍历
'''
scoredict = {"张三":89,"李四":80,"王二":20,"麻子":67}
for k in scoredict:
    print(k, end=" ") # 张三 李四 王二 麻子
print()

#单独访问key
for k in scoredict.keys():
    print(k, end=" ")  # 张三 李四 王二 麻子
print()

#单独访问value
for v in scoredict.values():
    print(v, end=" ")  # 89 80 20 67
print()

#同时key与value
for item in scoredict.items():
    print(item, end=" ")  # ('张三', 89) ('李四', 80) ('王二', 20) ('麻子', 67)
print()

for k,v in scoredict.items():
    print(k, v, end=" ")  # 张三 89 李四 80 王二 20 麻子 67 
print()

8、set集合
'''
set集合:本质也是一个无序的集合,但是只存储了字典中key,没有存储字典中的value。
set集合中的元素与字典中key的元素有共同的特征:
1.set集合中元素也是唯一的
2.set集合中元素也是不可变的

创建set集合:
set1 = set()  #创建空的set集合
set2 = {1,2,3} #创建具有元素的set集合
set3 = set([])  #创建set集合

set1.add(ele)
功能:向set1中添加元素,
注意:当添加的元素与set1集合中的元素出现重复的时候,不会有任何效果,但是并不报错。
添加的元素必须是不可变类型的,如可变类型的元素则报错。

set1.update(序列)
功能:将序列中的元素打碎插入到set1中
注意:使用update函数的时候,其参数一定是序列。

set1.remove(ele)
功能:将指定的元素移除

set集合的遍历
for x in set1:
    pass

set1 & set2  获取集合的交集  【两个集合元素重叠的部分】
set1 | set2  获取集合的并集  【两个集合的所有元素去除重叠的部分】

set集合最常用的功能就是去重。
'''

set1 = {1}
set2 = set()
set3 = set([1,2,3,4,454,4])
print(set1)  # {1}
print(type(set1))  # <class 'set'>
print(set2)  # set()
print(type(set2))  # <class 'set'>
print(set3)  # {1, 2, 3, 4, 454}
print(type(set3))  # <class 'set'>



set1.add("hello")
set1.add(1)
print(set1)  # {1, 'hello'}

set1.update([1,2,3,4,5])
print(set1)  # {1, 2, 3, 4, 5, 'hello'}
set1.update("hello")
print(set1)  # {1, 2, 3, 4, 5, 'h', 'e', 'l', 'hello', 'o'}

set1.remove(1) 
print(set1)  # {'h', 2, 3, 4, 5, 'o', 'hello', 'e', 'l'}

# for x in set1:
#     print(x)
set2 = {1,2,3,4,5,6}
set3 = {4,5,6,7,8,9}
print(set2 & set3)  # {4, 5, 6}
print(set2 | set3)  # {1, 2, 3, 4, 5, 6, 7, 8, 9}
9、类型转换
'''
set,tuple,list 三种类型可以直接进行相互转换。
将dict转为set/tuple/list,只转换了key。
若将set/tuple/list转为字典的时候,对set/tuple/list要求会高一些。
set/tuple/list里面的元素必须元组(列表也行),元组中元素的个数必须是两个。
'''

list1 = [1,2,3,4]
tuple1 = ("a","b","c","d")
set1 = {"I","II","III","IV"}
dict1 = {"1":"hello","2":"hi","3":"haha","4":"heihei"}


# # 将其它的数据类型转为list
print(list(tuple1))  # ['a', 'b', 'c', 'd']
print(list(set1))  # ['I', 'II', 'III', 'IV']
# # 将字典使用list转换的时候,只转换了key没有转换value
print(list(dict1))  # ['1', '2', '3', '4']
print(list(dict1.values()))  # ['hello', 'hi', 'haha', 'heihei']
print(list(dict1.items()))  # [('1', 'hello'), ('2', 'hi'), ('3', 'haha'), ('4', 'heihei')]

print("*"*50)
# #将其它的数据类型转为tuple
print(tuple(list1))  # (1, 2, 3, 4)
print(tuple(set1))  # ('IV', 'I', 'III', 'II')
# 默认只转换了key
print(tuple(dict1))  # ('1', '2', '3', '4')
print(tuple(dict1.values()))  # ('hello', 'hi', 'haha', 'heihei')
print(tuple(dict1.items()))  # (('1', 'hello'), ('2', 'hi'), ('3', 'haha'), ('4', 'heihei'))

print("*"*50)
# 将其它的数据类型转为set
print(set(tuple1))  # {'b', 'a', 'd', 'c'}
print(set(list1))  # {1, 2, 3, 4}
#  默认只转换了key
print(set(dict1))  # {'1', '2', '3', '4'}
print(set(dict1.values()))  # {'hello', 'hi', 'heihei', 'haha'}
print(set(dict1.items()))  # {('4', 'heihei'), ('1', 'hello'), ('3', 'haha'), ('2', 'hi')}


print("*"*50)
# 将其它的数据类型转为字典
#当列表为二维列表,并且二维列表的元素是成对的,也是就说二维列表的元素只有两个的情况下,是可以进行转换的
print(dict([(1,(1233,22)),[2,3]]))  # {1: (1233, 22), 2: 3}
print(dict(((1,2),(2,3))))  # {1: 2, 2: 3}
print(dict({(1,2),(3,4)}))  # {1: 2, 3: 4}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值