Python入门笔记二(注意知识点)

本文深入探讨了Python中的基本数据结构,包括列表、元组、集合、字典等的使用方法及特性,如列表的切片操作、元组的不可变性、集合的无序性和唯一性以及字典的键值对映射。同时,文章还讲解了这些数据结构的类型检查和常见操作,如长度、最大最小值、成员资格判断等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python入门笔记二(注意知识点)

2018.12.29(晚上)
列表
type([“hello”,“world”,1,9])
<class ‘list’>

[“hello”,“world”,1,9,True,False]
[‘hello’, ‘world’, 1, 9, True, False]#任意类型

type([[1,2],[3,4],[True,False]])#二维数组;嵌套列表(python中称呼)
<class ‘list’>

列表子元素选取:

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”][0]
‘新月打击’

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”][3]
‘月神冲刺’

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”][0:2]
[‘新月打击’, ‘苍白之瀑’]

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”][-1:]
[‘月神冲刺’]

更改列表元素值

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”]+[‘点燃’,‘虚弱’]]#加法运算
[‘新月打击’, ‘苍白之瀑’, ‘月之降临’, ‘月神冲刺’, ‘点燃’, ‘虚弱’]

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”]*3#乘法运算
[‘新月打击’, ‘苍白之瀑’, ‘月之降临’, ‘月神冲刺’, ‘新月打击’, ‘苍白之瀑’, ‘月之降临’, ‘月神冲刺’, ‘新月打击’, ‘苍白之瀑’, ‘月之降临’, ‘月神冲刺’]

[“新月打击”,“苍白之瀑”,“月之降临”,“月神冲刺”]-[“月神冲刺”]#减法不适用
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’

二:元组(类似于列表)

(1,2,3,4,5)
(1, 2, 3, 4, 5)

(1,‘1’,True)
(1, ‘1’, True)

(1,2,3,4)[0]
1

(1,2,3,4)[0:3]
(1, 2, 3)

(1,2,3)+(4,5,6)
(1, 2, 3, 4, 5, 6)

(1,2,3,4)*3
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

三:类型/类

type((1,2,3))
<class ‘tuple’>

type(1)
<class ‘int’>

type([1,2,3])
<class ‘list’>

type(‘hello’)
<class ‘str’>

type((1))
<class ‘int’>

type((‘hello’))
<class ‘str’>

(1+1)*2
4

type((1))#将(1)运算出来
<class ‘int’>

type((1,))
<class ‘tuple’>

type(())
<class ‘tuple’>

type([1])
<class ‘list’>

四:序列(组)str,list,tuple
A共有的操作,都有一个序号

‘hello world’[2]
‘l’

[1,2,3][2]
3
B切片

‘hello world’[0:8:2]
‘hlow’

3 in [1,2,3,4,5,6]
True

10 in [1,2,3,4,5,6]
False

3 not in [1,2,3,4,5,6]
False

C

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

max(‘hello world’)
‘w’

min(‘hello world’)
’ ’

min(‘helloworld’)
‘d’

D:ASCII码ord

ord(‘w’)
119

ord(‘d’)
100

ord(’ ')
32

四:集合—set 无序 {}

type({1,2,3,4,5,6})
<class ‘set’>

{1,2,3,4,5,6}[0]#无序且无法切片
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘set’ object does not support indexing

{1,1,2,2,3,3}#集合不重复
{1, 2, 3}

len({1,2,3})
3

1 in {1,2,3}
True

1 not in {1,2,3}
False

从第一个集合中去掉与第二个集合有的/共有的(可以使用减号/&,实际上求两个集合的差集he交集)

{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({})#错误
<class ‘dict’>

type(set())#正确定义
<class ‘set’>

len(set())
0

五:字典
Key Value
一个字典可以有很多Key和Value,集合类型(set)但不是序列,序列有顺序
”{}“
{key1:value,key2:value2…} set
作用:通过key来访问value
释放新月打击

{‘Q’:‘新月打击’,‘W’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}[‘Q’]
‘新月打击’

字典里有相同的键(字典里不能有相同的key)

{‘Q’:‘新月打击’,‘Q’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}[‘Q’]
‘苍白之瀑’
(字典里不能有相同的key)

{‘Q’:‘新月打击’,‘Q’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}
{‘Q’: ‘苍白之瀑’, ‘E’: ‘月之降临’, ‘R’: ‘月神冲刺’}

键值不一定是字符串

{1:‘新月打击’,‘1’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}}}
{1: ‘新月打击’, ‘1’: ‘苍白之瀑’, ‘E’: ‘月之降临’, ‘R’: ‘月神冲刺’}

数字1和字符串1是不一样的


总结:

value可取任意值str,int,float,list,set,dict
key:必须是不可变的类型int,‘str’

{[1,2]:‘新月打击’,‘1’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}
Traceback (most recent call last):
File “”, line 1, in
TypeError: unhashable type: ‘list’
;;列表不可以,元组可以(如下)


{(1,2):‘新月打击’,‘1’:‘苍白之瀑’,‘E’:‘月之降临’,‘R’:‘月神冲刺’}
{(1, 2): ‘新月打击’, ‘1’: ‘苍白之瀑’, ‘E’: ‘月之降临’, ‘R’: ‘月神冲刺’}

问题?空的字典定义 ”{}“

**

回顾见思维导图

**在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值