Python3快速入门-2数据类型

2.Python3数据类型

2.1基础知识

2.1.1 Python3注释

在Python中,有2种注释方式:单行注释、多行注释。

单行注释:使用#作为单行注释符号。

>>> print('hello world') #单行注释

hello world

>>> #print('hello world')

>>> 

多行注释:使用三引号(‘’’或”””)作为多行注释符号。

>>> '''

hello python

多行注释

hello world

'''

'\nhello python\n多行注释\nhello world\n'

>>> """

hello python

多行注释

hello world

"""

'\nhello python\n多行注释\nhello world\n'

>>> 

注:三引号中的注释本质上是字符串。当这个字符串不使用时,表示的就是注释;当字符串使用时,如赋值给变量,其当作字符串来处理。

2.1.2变量

问题:当对一个很长的列表进行复杂运算时,难道每一次运算都要书写这个长列表吗?

解决:使用变量来表示这个长列表。

变量,简而言之就是数据的名称。变量 = 数据,其中”=”称为赋值符号。

变量命名规则:

(1)由字母、数字、下划线(_)组成;

(2)首字母不能是数字;

(3)不能使用特殊符号,如*、$、%等;

(4)变量名不能使用系统关键字,如False、Ture、and、if等。

(5)尽量不要使用函数名来作为变量,如下所示使用type、print函数举例。

重点1:变量的重新赋值

在C语言中,命名一个整形变量int a = 1,a只能是整形变量,无法将其他类型的数据赋值给它,如a = ‘1’就是错误的;在Python中,变量是没有数据类型的,如a = 1,a还可以表示列表,如a = [1,2,3]。

重点2:将多个值分配给多个变量

使用逗号隔开即可。

重点3:变量类型

在Python中,有值类型和引用类型。

数字、字符串、元组都是值类型;列表、集合、字典都是引用类型。数字由于是重新赋值,因此是指类型;字符串和元组不可变,因此也是值类型。

若是重新赋值,则会重新开辟内存空间来存放变量,如下所示:

图中,字符串无法改变但是可以重新赋值,重新赋值后变量str1指向的地址改变,而str2还指向原来的地址,因此str1改变、str2不变。同理,列表A也是重新赋值。

注:字符串中的”+=”也是重新开辟内存空间。

2.1.3关键字

在Python shell中输入help('keywords'),可以查看所有系统关键字。

关键字的含义在后面会一一介绍。

2.2 Number-数字

在数字类型中,分为整形(int)、浮点型(float)、bool型、复数(complex)。

2.2.1整形-int

在C++中,整形有short、int、long、long long;而在Python中,整形只有int。

>>> a = 123456

>>> print(a)

123456

>>> type(a)

<class 'int'>

我们常用的数据都是10进制,而在Python中还有2进制、8进制、16进制。2进制数据以0b/0B开头;8进制以0o/0O开头;16进制以0x/0X开头。

bin()函数将数据转换为2进制;oct()函数将数据转换为8进制;int()函数将数据转换为10进制;hex()函数将数据转换为16进制。

2.2.2浮点型-float

在C++中,浮点数可以分为单精度float、双精度double;但是在Python中,浮点数只用float表示。

在Python中,整数可以是任何长度,但是浮点数只能精确到16位。

>>> a = 0.1234567890123456789

>>> print(a)

0.12345678901234568

int()函数不仅可以进制转换,还可以将浮点数转换为整数;而float()函数可以将整数转换为浮点数。

>>> int(1.2)

1

>>> float(1)

1.0

当执行除法运算时,其返回值必定是浮点数。

>>> type(1/1)

<class 'float'>

注:当执行0.1+0.2,应该等于0.3,但Python并不这么认为!

>>> 0.1 + 0.2

0.30000000000000004

原因:由于计算机只能识别0和1,因此浮点数在计算机中不能准确表达。这是计算机硬件的局限性。

2.2.3复数-complex

复数在Python中以x+yj的形式构成,x表示实部,y表示虚部。

>>> type(1+2j)

<class 'complex'>

complex()函数可以将整数、浮点数转换为复数。

>>> complex(2)

(2+0j)

>>> complex(1.1)

(1.1+0j)

2.2.4布尔类型-bool

布尔类型用非0值表示True,True表示“真”的意思,转换成int等于1;用0、’’、[]、()、{}、None表示False,False表示“假”的意思,转换成int等于0。

>>> type(True)

<class 'bool'>

>>> type(False)

<class 'bool'>

bool()函数可以将其他类型转换为布尔类型。

>>> bool(1.1)

True

>>> bool('1')

True

>>> bool(None)

False

2.3 str-字符串

2.3.1字符串的表示方法

在Python中,字符串用单引号(’’)或双引号(””)来表示;多行字符串使用三引号(‘’’或”””)表示。

>>> str1 = 'hello'

>>> type(str1)

<class 'str'>

>>> str2 = "world"

>>> type(str2)

<class 'str'>

>>> str3 = '''hello

world'''

>>> print(str3)

hello

world

>>> type(str3)

<class 'str'>

>>> str4 = """

hello world

hello python

"""

>>> print(str4)

 

hello world

hello python

 

>>> type(str4)

<class 'str'>

问题1:如何表示Let's learn Python?

>>> 'let's learn Python'

SyntaxError: invalid syntax

方法1:使用双引号(推荐)

>>> "let's learn Python"

"let's learn Python"

方法2:外部单引号,内部双引号

>>> 'let"s learn Python'

'let"s learn Python'

方法3:使用转移字符

>>> 'let\'s learn Python'

"let's learn Python"

问题2:前面str4的打印中为什么前后空一行?

原因:当表示多行字符串中,换行符不被显示。

>>> '''

hello world

hello Python

'''

'\nhello world\nhello Python\n'

问题3:多行字符串除了使用三引号,单引号和双引号无法表示吗?

解决:可以使用\来换行。

>>> 'hello \

world'

'hello world'

2.3.2转义字符

常用的转移字符:\表示续行符;\n表示换行符;\r表示回车;\’表示字符’;\\表示字符\;\t表示横向制表符。

转义字符在print()函数中显示作用。

>>> s = 'hello\nworld'

>>> print(s)

hello

World

问题1:如何打印出C:\nvidia corporation?

>>> s = 'C:\Windows'

>>> print(s)

C:\Windows

>>> s = 'C:\nvidia corporation'

>>> print(s)

C:

vidia corporation

方法1:使用\\表示字符\。(推荐)

>>> s = 'C:\\nvidia corporation'

>>> print(s)

C:\nvidia corporation

方法2:使用r/R表示原始字符串。

>>> print(r'C:\nvidia corporation')

C:\nvidia corporation

2.3.3字符串基本操作

字符串是字符的有序序列,因此可以通过去位置来访问具体的字符。

字符串中的索引分2种方式:正向索引、反向索引。如下所示。

使用方法1:索引单个字符。若索引值超范围,则显示错误。

>>> s ='Hello world'

>>> s[0]

'H'

>>> s[-11]

'H'

>>> s[10]

'd'

>>> s[-1]

'd'

>>> s[20]

Traceback (most recent call last):

  File "<pyshell#8>", line 1, in <module>

    s[20]

IndexError: string index out of range

使用方法2:索引多个字符,又称切片。

>>> s[0:5]   #第0个字符到第5个字符,包括第0个字符,不包括第5个字符

'Hello'

>>> s[6:11] #第6个字符到第11个字符,包括第6个字符,不包括第11个字符

'world'

>>> s[6:] #第6个字符到最后一个字符

'world'

>>> s[-11:-6] #第-11个字符到第-6个字符,包括第-11个字符,不包括第-6个字符

'Hello'

>>> s[-5:] #第-5个字符到最后一个字符

'world'

>>> s[0:-6] #第0个字符到第-6个字符,包括第0个字符,不包括第-6个字符

'Hello'

>>> s[:] #所有字符

'Hello world'

注1:从第5个字符到最后一个字符不可表示为s[-5:0]。

>>> s[-5:0]

''

注2:索引单个字符时,不可越界;索引多个字符时,正向索引可以越界表示最后一个字符。

>>> s[6:20]

'world'

字符串无法修改,但可以重新赋值。

>>> s = 'Hello world'

>>> s[0] = 'h'

Traceback (most recent call last):

  File "<pyshell#4>", line 1, in <module>

    s[0] = 'h'

TypeError: 'str' object does not support item assignment

>>> s = 'hello'

>>> print(s)

hello

(1)字符串可以进行+、*运算。

>>> s = 'hello'

>>> s += ' world'

>>> print(s)

hello world

>>> s * 2

'hello worldhello world'

字符串中的重新赋值、+、*等操作,都是在内存中重新开辟空间。

(2)in:判断元素是否在序列中。

>>> s = 'hello'

>>> 'h' in s

True

>>> 'w' in s

False

(3)int()函数将字符串转化为10进制整数;float()函数将字符串转化为10进制小数;complex()函数将字符串转化为复数。

>>> int('123')

123

>>> float('1.11')

1.11

>>> complex('1.2')

(1.2+0j)

(4)len()函数获取字符串长度

>>> s = 'hello'

>>> len(s)

5

(5)min()函数和max()函数是根据对应的ASCII码进行比较,获取最小值和最大值。

>>> s = 'hello'

>>> min(s)

'e'

>>> max(s)

'o'

(6)ord()函数表示字符转ASCII码;chr()函数表示ASCII码转字符。

>>> ord('o')

111

>>> chr(111)

'o'

(7)str()函数可以将其他数据类型转换为字符串

>>> str(1.2)

'1.2'

>>> str([1,2,3])

'[1, 2, 3]'

2.3.4字符串常用函数

(1)lower()函数转换所有字符为小写;upper()函数转换所有字符为大写。

>>> s = 'Hello'

>>> s.lower()

'hello'

>>> s.upper()

'HELLO'

(2)find()函数返回字符串的开始索引值。

>>> s = 'Hello world'

>>> s.find('wo')

6

(3)replace()函数替换字符串中的一部分,并返回。

>>> s = 'Hello world'

>>> s1 = s.replace('ello','i')

>>> print(s)

Hello world

>>> print(s1)

Hi world

(4)split()函数将字符串拆分成列表,并返回。

>>> s = 'Hello world'

>>> l = s.split()

>>> print(l)

['Hello', 'world']

2.3.5字符串和%的用法

%是Python中的标记转换说明符,具体用法如下。

(1)%d:标记并转换为十进制整数,适合数据类型为整形、浮点型、布尔型。

>>> i = 1

>>> print('i = %d'%i)

i = 1

>>> f = 1.2

>>> print('f = %d'%f)

f = 1

>>> b = True

>>> print('b = %d'%b)

b = 1

(2)%s:标记并转换为字符串,适合数据类型为数字、字符串、列表、集合、字典,不适合数据类型为元组。

>>> i = 1

>>> print('i = %s'%i)

i = 1

>>> type(i)

<class 'int'>

>>> c = 2 + 3j

>>> print('c = %s'%c)

c = (2+3j)

>>> l = [1,2,3]

>>> print('l = %s'%l)

l = [1, 2, 3]

>>> set1 = {1,2,3}

>>> print('set1 = %s'%set1)

set1 = {1, 2, 3}

>>> d = {1:'hello'}

>>> print('d = %s'%d)

d = {1: 'hello'}

为什么元组不行?

>>> tup = (1,2,3)

>>> print('tup = %s'%tup)

Traceback (most recent call last):

  File "<pyshell#8>", line 1, in <module>

    print('tup = %s'%tup)

TypeError: not all arguments converted during string formatting

因为在第二个%中可以使用()来标记多个变量,如下所示。

>>> a = 1

>>> b = 2

>>> print('a = %d, b = %d'%(a, b))

a = 1, b = 2

(3)%f:标记并转化为浮点数,适合数据类型为整形、浮点型、布尔型。

>>> pi = 3.1415926

>>> print('pi = %5.3f'%pi) #数字长度为5,精度为3

pi = 3.142

>>> b = True

>>> print('b = %1.2f'%b)

b = 1.00

(4)%o:标记并转换为8进制;%x:标记并转换为16进制(小写);%X:标记并转换为16进制(大写)。

>>> a = 0b1111

>>> print('a = %d'%a)

a = 15

>>> print('a = %o'%a)

a = 17

>>> print('a = %x'%a)

a = f

>>> print('a = %X'%a)

a = F

补充:在%后可以加入一些符号表示特殊的含义。

#:在八进制前显示0o,在十六进制前显示0x或0X(取决于%x和%X);

0:显示的数字前填充0,而不是空格;

M.N:M表示数据的宽度,N表示小数点后精确的位数。

>>> a = 0b1111

>>> print('a = %#o'%a)

a = 0o17

>>> print('a = %#06o'%a)

a = 0o0017

>>> print('a = %#04x'%a)

a = 0x0f

2.4 list-列表

2.4.1列表的表示方法

列表和字符串类似,都是Python中有序的元素序列。列表中元素的类型可以各不相同,可以是数字、字符串、列表(二维列表)、元组、集合、字典。

列表将所有元素放在[]内,用逗号(英文格式)隔开。

>>> l = [1,2,'hello','world',[3,4,'Python']]

>>> print(l)

[1, 2, 'hello', 'world', [3, 4, 'Python']]

列表的索引方法和字符串一样,这里不再多数。

索引单个元素:

>>> l = [1,2,'hello','world',[3,4,'Python']]

>>> l[0]

1

>>> l[-5]

1

>>> l[4]

[3, 4, 'Python']

>>> l[-1]

[3, 4, 'Python']

>>> l[4][0]

3

>>> l[4][-3]

3

>>> l[2][0:2]

'he'

索引多个元素:

>>> l[0:3]   #返回值是列表

[1, 2, 'hello']

>>> l[:3] #返回值是列表

[1, 2, 'hello']

>>> l[3:] #返回值是列表

['world', [3, 4, 'Python']]

>>> l[2:4] #返回值是列表

['hello', 'world']

>>> l[2:4][1] #返回值是元素

'world'

2.4.2列表基本操作

(1)列表和字符串一样,支持+、*运算。

>>> l1 = [1,2,'hello']

>>> l1 += ['world']

>>> print(l1)

[1, 2, 'hello', 'world']

>>> l2 = l1 * 2

>>> print(l2)

[1, 2, 'hello', 'world', 1, 2, 'hello', 'world']

(2)字符串中的元素无法修改,而列表中的元素可以修改。

>>> l = [1,2,3,'python']

>>> l[3] = 4

>>> print(l)

[1, 2, 3, 4]

字符串是值类型,列表是引用类型。

>>> #字符串:值类型

>>> s1 = 'hello'

>>> s2 = s1

>>> s1 += ' world'

>>> print(s1)

hello world

>>> print(s2)

hello

>>> #列表:引用类型

>>> l1 = [1,2,3,4]

>>> l2 = l1

>>> l1 += [5,6]

>>> print(l1)

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

>>> print(l2)

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

>>> l1[5] = 'hello'

>>> print(l1)

[1, 2, 3, 4, 5, 'hello']

>>> print(l2)

[1, 2, 3, 4, 5, 'hello']

(3)in:判断元素是否在序列中

>>> l = [1,2,3,4,5]

>>> 5 in l

True

(4)len()函数获取列表元素个数

>>> l = [1,2,3,4,[5,6]]

>>> len(l)

5

(5)del:从列表中删除元素或删除变量

>>> l = [1,2,3,4,5]

>>> del l[2]

>>> print(l)

[1, 2, 4, 5]

>>> del l[2:4]

>>> print(l)

[1, 2]

>>> del l   #删除变量

>>> print(l)

Traceback (most recent call last):

  File "<pyshell#16>", line 1, in <module>

    l

NameError: name 'l' is not defined

(6)list()函数可以将字符串、元组、集合等转换为列表。

>>> list('hello')

['h', 'e', 'l', 'l', 'o']

>>> list((1,2,3))

[1, 2, 3]

2.4.3列表常用函数

(1)append()函数表示添加一个元素到列表中;extend()函数表示添加多个元素到列表中。

>>> l = [1,2,3,4,5]

>>> l.append(6)

>>> print(l)

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

>>> l.extend([7,8,9])

>>> print(l)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

(2)insert()函数表示将元素插入到列表指定位置。

>>> l = [1,2,3,4]

>>> l.insert(2,'hello')

>>> print(l)

[1, 2, 'hello', 3, 4]

(3)remove()函数表示删除指定的元素。

>>> l = [1,2,3,4,'hello']

>>> l.remove('hello')

>>> print(l)

[1, 2, 3, 4]

(4)pop()函数表示删除并返回给定索引值的元素;若不给索引值,表示删除并返回最后一个元素。

>>> l = [1,2,3,4,5]

>>> l.pop()

5

>>> l.pop(2)

3

>>> print(l)

[1, 2, 4]

(5)clear()函数表示清除列表中的元素。

>>> l = [1,2,3,4,5]

>>> l.clear()

>>> print(l)

[]

2.5 tuple-元组

2.5.1元组的表示方法

元组和列表类似,都是有序的元素序列。元组中元素的类型可以各不相同,可以是数字、字符串、列表、元组、集合、字典。

元组将所有元素放在()内,用逗号(英文格式)隔开。

>>> tup = (1,2,'hello','world',[3,4,'Python'])

>>> print(tup)

(1, 2, 'hello', 'world', [3, 4, 'Python'])

创建空元组和空列表。

>>> tup = ()

>>> type(tup)

<class 'tuple'>

>>> l = []

>>> type(l)

<class 'list'>

创建只有一个元素的元组和列表。

>>> tup = (1,)

>>> type(tup)

<class 'tuple'>

>>> l = [1]

>>> type(l)

<class 'list'>

问题:为什么创建一个元素的元组不是tup = (1)?

解决:若创建tup = (1),查看其数据类型如下。为了区分元组的()和运算符(),因此将(1)中的小括号表示为运算符,将(1,)中的小括号表示为元组。

>>> tup = (1)

>>> type(tup)

<class 'int'>

元组的索引方法和列表、字符串等一致,因此不再多说。

2.5.2元组的基本操作

(1)元组的+、*运算。

>>> tup = (1,2,3)

>>> tup += (4,5)

>>> print(tup)

(1, 2, 3, 4, 5)

>>> tup = tup * 2

>>> print(tup)

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

(2)in:判断元素是否在序列中

>>> tup = (1,2,3,4)

>>> 5 not in tup

True

(3)len()函数获取元组的元素个数

>>> tup = (1,2,3,4)

>>> len(tup)

4

(4)tuple()函数可以将字符串、列表、集合等转换为元组。

>>> tuple('hello')

('h', 'e', 'l', 'l', 'o')

>>> tuple([1,2,3])

(1, 2, 3)

2.5.3元组和列表

元组和列表之间的不同:列表元素可以修改,而元组元素不可改。

>>> tup = (1,2,3,4)

>>> tup[3] = 'hello'

Traceback (most recent call last):

  File "<pyshell#11>", line 1, in <module>

    tup[3] = 'hello'

TypeError: 'tuple' object does not support item assignment

若是元组中列表,则列表可以修改。

>>> tup = (1,2,[3,4,5])

>>> tup[2][2] = 'hello'

>>> print(tup)

(1, 2, [3, 4, 'hello'])

2.6 set-集合

2.6.1集合的表示方法

集合是一个无序的、不同的、确定的元素组合。集合中元素的类型可以是数字、字符串、元组;不可以是列表、集合、字典。

集合将所有元素放在{}内,用逗号(英文格式)隔开。

>>> s = {1,2,'hello',(3,4,'world')}

>>> print(s)

{'hello', 1, 2, (3, 4, 'world')}

注;元组内不能有列表、集合、字典。

>>> s = {(1,2,[3,4])}

Traceback (most recent call last):

  File "<pyshell#11>", line 1, in <module>

    s = {(1,2,[3,4])}

TypeError: unhashable type: 'list'

创建空集合:使用set()函数来创建。

>>> s = set()

>>> type(s)

<class 'set'>

创建空字典。

>>> d = {}

>>> type(d)

<class 'dict'>

集合的3大特性:无序性、互异性、确定性。

>>> #无序性:无法索引

>>> s = {1,2,3,4}

>>> s[0]

Traceback (most recent call last):

  File "<pyshell#6>", line 1, in <module>

    s[0]

TypeError: 'set' object does not support indexing

>>> #互异性

>>> s = {1,2,3,4,5,1,2,3,4,5}

>>> print(s)

{1, 2, 3, 4, 5}

>>> #确定性

>>> s = {1,2,3,4,5}

>>> 1 in s

True

>>> 1 not in s

False

2.6.2集合的基本操作

(1)差集

>>> s1 = {1,2,3,4}

>>> s2 = {2,3,5}

>>> s1 - s2

{1, 4}

(2)交集

>>> s1 = {1,2,3,4}

>>> s2 = {2,3,5}

>>> s1 & s2

{2, 3}

(3)并集

>>> s1 = {1,2,3,4}

>>> s2 = {2,3,5}

>>> s1 | s2

{1, 2, 3, 4, 5}

(4)集合没有+、*运算,但是in、len()、min()、max()都支持。

>>> s = {'h','e','l','o'}

>>> min(s)

'e'

>>> max(s)

'o'

>>> len(s)

4

(5)set()函数可以将字符串、列表、元组等转换为集合。

2.6.3集合常用函数

(1)discard()函数表示删除一个已存在的元素;remove()函数也删除一个已存在的元素,若元素不存在则会报错;clear()函数表示清除所有元素。

>>> s = {1,2,3,4}

>>> s.discard(4)

>>> print(s)

{1, 2, 3}

>>> s.remove(3)

>>> print(s)

{1, 2}

>>> s.remove(3)

Traceback (most recent call last):

  File "<pyshell#5>", line 1, in <module>

    s.remove(3)

KeyError: 3

>>> s.clear()

>>> print(s)

set()

(2)add()函数表示添加一个元素;update()函数表示添加多个元素,可以是字符串、列表、元组。

>>> s = {1,2,3,4}

>>> s.add(5)

>>> print(s)

{1, 2, 3, 4, 5}

>>> s.update([6,7,8])

>>> print(s)

{1, 2, 3, 4, 5, 6, 7, 8}

>>> s.update('hello')

>>> print(s)

{1, 2, 3, 4, 5, 6, 7, 8, 'l', 'e', 'o', 'h'}

注:set集合是引用类型,并且可以修改;除了set外,还有frozenset集合表示不可修改的集合。关于frozenset后面再介绍。

2.7 dict-字典

2.7.1字典的表示方法

Python中的字典类似于C++中的关联容器。同样是由key、value组成,用key值来表示value。

由于字典的key值不可修改,因此key不可以是列表、集合、字典;可以是数字、字符串、元组。而value值可以是Python中任何数据类型,并且可以修改。

>>> d = {1:'hello',2:'world'}

>>> print(d)

{1: 'hello', 2: 'world'}

注:key值是唯一的。若字典中有2个相同的key,则后面的key:value会覆盖前面的key:value。

>>> d = {1:'hello',2:'world',2:'www'}

>>> print(d)

{1: 'hello', 2: 'www'}

创建空字典。

>>> d = {}

>>> type(d)

<class 'dict'>

2.7.2字典的基本操作

(1)字典通过key值来访问value值。

>>> d = {1:'hello',2:'world'}

>>> d[1] = 'HELLO'

>>> print(d)

{1: 'HELLO', 2: 'world'}

(2)字典不支持+、*等运算,但支持in、len、min、max等操作。

>>> d = {1:'hello',2:'world'}

>>> 2 in d   #使用key值来查找

True

>>> 'world' in d #不能使用value值来查找

False

>>> len(d)

2

>>> min(d)

1

>>> max(d)

2

(3)字典支持使用=来添加、修改元素。

>>> d = {1:'hello',2:'world'}

>>> d[3] = 'www'

>>> print(d)

{1: 'hello', 2: 'world', 3: 'www'}

>>> d[3] = 'zzz'

>>> print(d)

{1: 'hello', 2: 'world', 3: 'zzz'}

(4)dict()函数可以把包含元组(2个元素)的列表转换为字典。

>>> dict([(1,'hello'),(2,'world')])

{1: 'hello', 2: 'world'}

>>> dict([(1,'hello','HELLO'),(2,'world')])  

Traceback (most recent call last):

  File "<pyshell#16>", line 1, in <module>

    dict([(1,'hello','HELLO'),(2,'world')])

ValueError: dictionary update sequence element #0 has length 3; 2 is required

2.7.3字典常用函数

(1)get()函数表示获取对应key值得value数值,若没有找到返回空对象None。

>>> d = {1:'hello',2:'world'}

>>> d.get(1)

'hello'

>>> d.get(3)

(2)pop()函数表示删除对应key值得value。

>>> d = {1:'hello',2:'world'}

>>> d.pop(2)

'world'

>>> print(d)

{1: 'hello'}

(3)clear()函数表示清除字典的所有元素。

>>> d = {1:'hello',2:'world'}

>>> d.clear()

>>> print(d)

{}

2.8总结

(1)数字分为整形int、浮点型float、布尔型bool、复数complex。组分为序列、集合、字典;序列分为字符串、列表、元组,它们都是有序的,支持下标索引、切片操作等;集合是无序的,因此不能索引;字典通过key值来索引value。

(2)在前面的代码中,使用print来打印变量的数值。在Python可以直接输入变量名,就可以出现变量的数值。

>>> l = [1,2,3,4]

>>> print(l)

[1, 2, 3, 4]

>>> l

[1, 2, 3, 4]

(3)None在Python中的数据类型是NoneType。在Python中,不同数据类型的对象之间无法比较,因此None无法和0、False、’’、[]、()、set()、{}这些对象进行比较(如==、is等)。

>>> type(None)

<class 'NoneType'>

可以将None赋值给任何一个变量。

>>> l = [1,2,3,4]

>>> l = None

>>> print(l)

None

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值