码迷,mamicode.com
首页 > 编程语言 > 详细

Python数据类型

时间:2019-07-02 00:15:11      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:number   res   syn   父类   构造函数   nta   error:   字符串操作   引号   

1、Python标准数据类型

Number(数字),序列:String(字符串)、List(列表)、Tuple(元组),Set(集合)、Dictionary(字典)

1.1、Number(数字)

1.1.1、分类

int(长整型)、float(浮点型)、bool(布尔型)、complex(复数)

int:a = 10。(python2中使用long表示长整型)

bool:a = True;b = False。True的值为1,False的值为0.(python2中没有True、False,用1表示真,0表示假)

complex:4+3j或者complex(4, 3)

1.1.2、获取变量所指的对象类型

type(变量名),isinstance(变量名, 变量类型)

>>> a = 10; b = 3.14; c = True; d = 4+3j

>>> print(type(a), type(b), type(c), type(d))

<class ‘int‘> <class ‘float‘> <class ‘bool‘> <class ‘complex‘>
>>> print(isinstance(a, int), isinstance(b, float), isinstance(c, bool), isinstance(d, complex))
True True True True

type不会认为子类是一种父类类型,isinstance认为子类是一种父类类型

>>> class A:
pass

>>> class B(A):
pass

>>> print(type(B()) == A, type(B()) == B)

False True
>>> print(isinstance(B(), A), isinstance(B(), B))
True True

1.1.3、操作符

加(+)、减(-)、乘(*)、除(/):返回一个浮点数、除(//):返回一个整数,向下圆整、求余(%)、乘方(**)

>>> type(4/2)
<class ‘float‘>
>>> type(3/2)
<class ‘float‘>
>>> type(3//2)
<class ‘int‘>
>>> 5%2
1
>>> 2**3
8

1.2、String(字符串)

1.2.1、表示方法

字符串内容使用单引号‘或者双引号"括起来,使用反斜杠‘\‘转义特殊字符。

>>> a = ‘hello world‘; b = "hello world"; c = ‘D:\\test\\test‘
>>> print(a, b, c)
hello world hello world D:\test\test

1.2.2、操作

索引、截取、复制、拼接

1.2.2.1、索引

从左往右:索引值(下标)从0开始,从右往左:索引值(下标)从-1开始。

>>> a = ‘hello world‘
>>> print(a[0], a[10], a[-1], a[-11])
h d d h

1.2.2.2、截取

使用方法:变量名[[左下标]:[右下标]:[步长]],不截取右下标对应的字符

>>> a = ‘hello world‘
>>> a[0:4]
‘hell‘
>>> a[0:]
‘hello world‘
>>> a[-11:]
‘hello world‘
>>> a[:4]
‘hell‘

>>> a[-4:-1]
‘orl‘
>>> a[-4:]
‘orld‘
>>> a[:-1]
‘hello worl‘

使用步长:

>>> a[0:4:2]
‘hl‘
>>> a[0:5:2]
‘hlo‘

>>> a[0::2]
‘hlowrd‘
>>> a[0::3]
‘hlwl‘

>>> a[:-1:2]
‘hlowr‘
>>> a[:-1:3]
‘hlwl‘

反转字符串:

>>> a[-1::-1]
‘dlrow olleh‘

1.2.2.3、复制

>>> a = ‘hello world ‘
>>> print(a * 3)
hello world hello world hello world

1.2.2.4、拼接

>>> print(a + a + a)
hello world hello world hello world

1.2.3、特性

字符串内容不可变,字符串中的字符可重复,有序

>>> a = ‘hello world‘
>>> a[1] = ‘a‘
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
a[1] = ‘a‘
TypeError: ‘str‘ object does not support item assignment

1.3、List(列表)

1.3.1、表示方法

使用方括号"[]"将元素括起来,元素之间用逗号‘,‘分割,元素类型可以不同,内容可以是数字、字符串、列表、元组、集合、字典。

>>> lst = [1, "hello", [2, 3], (4, 5), {‘Tom‘, ‘Jack‘}, {‘name‘:‘Tom‘, ‘age‘:20}]
>>> lst
[1, ‘hello‘, [2, 3], (4, 5), {‘Tom‘, ‘Jack‘}, {‘name‘: ‘Tom‘, ‘age‘: 20}]

1.3.2、操作

索引、截取、复制、拼接和字符串操作相同。

1.3.3、特性

元素可变,可重复,有序

1.4、Tuple(元组)

1.4.1、表示方法

使用小括号"()"将元素括起来,元素之间用逗号‘,‘分割,元素类型可以不同,内容可以是数字、字符串、列表、元组、集合、字典。

>>> tup = (1, "hello", [2, 3], (4, 5), {‘Tom‘, ‘Jack‘}, {‘name‘:‘Tom‘, ‘age‘:20})
>>> tup
(1, ‘hello‘, [2, 3], (4, 5), {‘Tom‘, ‘Jack‘}, {‘name‘: ‘Tom‘, ‘age‘: 20})

1.4.2、操作

索引、截取、复制、拼接和字符串操作相同。

1.4.3、构造包含0个和1个元素的元组

>>> tup = ()
>>> type(tup)
<class ‘tuple‘>
>>> tup = (1)
>>> type(tup)
<class ‘int‘>
>>> tup = (1,)
>>> type(tup)
<class ‘tuple‘>

1.4.4、特性

元素不可变,可重复,有序

1.5、Set(集合)

1.5.1、表示方法

使用大括号“{}”或者set(iterable)创建集合,使用大括号创建时,元素之间用逗号“,”隔开,元素类型可以不同,内容可以是数字、字符串、列表、元组、集合、字典。使用set()创建时,必须使用序列。

>>> student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘}
>>> student
{‘Jim‘, ‘Rose‘, ‘Tom‘, ‘Jack‘, ‘Mary‘}

>>> set(‘Tom‘)
{‘T‘, ‘o‘, ‘m‘}
>>> set([1, 2, 3, 4])
{1, 2, 3, 4}

1.5.2、操作

成员测试、集合运算、创建空集合

1.5.2.1、成员测试

>>> if ‘Tom‘ in student:
print(‘Tom 在集合中‘)
else:
print(‘Tom 不在集合中‘)

 

Tom 在集合中

1.5.2.2、集合运算

交集、并集、差集、对称差

>>> a = set(‘abcd‘)
>>> b = set(‘bdef‘)
>>> a
{‘d‘, ‘a‘, ‘b‘, ‘c‘}
>>> b
{‘e‘, ‘d‘, ‘b‘, ‘f‘}
>>> a | b
{‘a‘, ‘d‘, ‘c‘, ‘f‘, ‘b‘, ‘e‘}
>>> a & b
{‘d‘, ‘b‘}
>>> a - b
{‘a‘, ‘c‘}
>>> b - a
{‘e‘, ‘f‘}
>>> a ^ b
{‘a‘, ‘c‘, ‘f‘, ‘e‘}

1.5.2.3、创建空集合

使用set(),不能使用大括号,因为大括号时创建空字典。

1.5.3、特性

元素可变、不可重复、无序

1.6、Dictionary(字典)

1.6.1、表示方法

使用大括号“{}”或者构造函数dict()创建字典,字典由键值对组成,key时唯一的,必须是不可变的数据类型。

>>> d = {1: ‘one‘, 2: ‘two‘, ‘name‘: ‘Tom‘, ‘age‘: 20}
>>> d
{1: ‘one‘, 2: ‘two‘, ‘name‘: ‘Tom‘, ‘age‘: 20}

>>> dict([(1, ‘one‘), (2, ‘two‘), (3, ‘three‘)])
{1: ‘one‘, 2: ‘two‘, 3: ‘three‘}
>>> dict(1=one, 2=two, 3=three)
SyntaxError: keyword can‘t be an expression
>>> dict(one=1, two=2, three=3)
{‘one‘: 1, ‘two‘: 2, ‘three‘: 3}

1.6.2、操作

获取某个键对应的值,获取所有键,获取所有值,创建空字典

>>> d = {1: ‘one‘, 2: ‘two‘, ‘name‘: ‘Tom‘, ‘age‘: 20}
>>> d
{1: ‘one‘, 2: ‘two‘, ‘name‘: ‘Tom‘, ‘age‘: 20}
>>> d[1]
‘one‘
>>> d[‘name‘]
‘Tom‘
>>> d.keys()
dict_keys([1, 2, ‘name‘, ‘age‘])
>>> d.values()
dict_values([‘one‘, ‘two‘, ‘Tom‘, 20])
>>> d = {}
>>> type(d)
<class ‘dict‘>

1.6.3、特性

元素可变、不可重复、无序

2、数据类型转换

Python数据类型

标签:number   res   syn   父类   构造函数   nta   error:   字符串操作   引号   

原文地址:https://www.cnblogs.com/wbz-blogs/p/11116443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!