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

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’: ‘月神冲刺’}

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

**

回顾见思维导图

**在这里插入图片描述

发布了22 篇原创文章 · 获赞 0 · 访问量 443

猜你喜欢

转载自blog.csdn.net/qq_36956082/article/details/85336806
今日推荐