Python的基本数据类型(二)

来源于:视频《Python3入门与进阶》
Python第四章-组的概念与定义

列表list

type([1,2,3,4,5,6])   #list
type(["hello world",1,9])   #list
type([[1,2],[3,4],[True,False]])   #list

列表的索引、切片

["新月打击",“苍白之瀑”,“月之降临”,“月神冲刺”][0]      #'新月打击'
["新月打击",“苍白之瀑”,“月之降临”,“月神冲刺”][3]      #'月神冲刺'
["新月打击",“苍白之瀑”,“月之降临”,“月神冲刺”][0:2]   #['新月打击','苍白之瀑']
["新月打击",“苍白之瀑”,“月之降临”,“月神冲刺”][-1:]    #['月神冲刺']

面试小考点:
单一数字索引访问 得到的是str
用冒号:索引,哪怕是一个元素,得到的也是list

元组tuple
功能:创建一系列不可修改的元素

(1,2,3,4,5)     #(1, 2, 3, 4, 5)  #tuple
(1,'-1',True)   #(1, '-1', True)
(1,2,3,4)[0]    #1
(1,2,3,4)[0:2]   #(1, 2)
(1,2,3)+(4,5,6)  #(1, 2, 3, 4, 5, 6)
(1,2,3)*3           #(1, 2, 3, 1, 2, 3, 1, 2, 3)

小知识点
type((1))        #int  把括号视为运算优先 ,没有当做元组标志
type((1,))      #tuple
type(())         #tuple  表示空的元组

str,list,tuple 序列
共有的操作:索引 、切片

3 in [1,2,3,4,5,6]      #True
10 in [1,2,3,4,5,6]    #False
len([1,2,3,4,5,6])      #6
len("hello world")     #11
max([1,2,3,4,5,6])    #6
min([1,2,3,4,5,6])     #1

集合set特点:无序,不重复

type({1,2,3,4,5,6})    #set
{1,2,3,4,5,6}[0]          #会报错,因为集合set是无序的
{1,2,3,4,5,6}[0:2]       #会报错,因为set不支持切片操作
{1,1,2,2,3,3,4,4}        #{1,2,3,4}   因为set是不重复的
{1,2,3,4,5,6} - {3,4}   #差集  {1,2,5,6}
{1,2,3,4,5,6}&{3,4}    #交集{3,4}
{1,2,3,4,5,6} | {3,4,7} #并集{1,2,3,4,5,6,7}
type(set())                 #set      定义空的set

字典dict
字典是一种集合set,因此用花括号,并且无序

{'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}['Q']   #'新月打击'
#key的类型必须是      不可变类型 int str tuple
#value的类型是       任意的     str int float set list dict
type({})                   #dict    空的花括号代表空的字典
type(set())                 #代表空的集合

总结:时间的力量是不可抗拒的,还是要勤看。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42610407/article/details/86661203