Python基础数据类型

01. 前言

基础数据类型掌握是一门语言的基础,Python的数据类型很灵活,变量并不是通过声明来指定,而是通过赋值来指定,这个与C和JAVA有很大的不同。

02. 六大标准数据类型

  1. Number - 整型
  2. String - 字符串
  3. List - 列表数组
  4. Tuple - 元祖
  5. Set - 集合
  6. Dict - 字典

03. Number - 整型

数字包含了整数、浮点数、布尔值以及复数:

>>>a, b, c, d = 23, 6.5, False, 4+5j
>>>print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

数值运算:

>>>5 + 4  # 加法
9
>>> 4.3 - 2 # 减法
2.3
>>> 3 * 7  # 乘法
21
>>> 2 / 4  # 除法,得到一个浮点数
0.5
>>> 2 // 4 # 除法,得到一个整数
0
>>> 17 % 3 # 取余 
2
>>> 2 ** 5 # 乘方
32

注意:

  • Python3中int类型不存在最大范围限制,也就是说你可以使用超过[-2417483648,2417483647]范围的整数
  • 在一般场景下复数使用较少,这里就不过多介绍了

04. String - 字符串

字符串很常用,可以通过单引号或者双引号来赋值

>>>a, b, c, d='Hello', "World", """Test""",'''Python'''
>>>print(type(a), type(b), type(c), type(d))
<class 'str'> <class 'str'> <class 'str'> <class 'str'>

有时候,你可以把它当数组来进行切片:

>>>a="HelloWorld"
>>>print(a[3:6])
loW

也许你要需要进行拼接:

>>>a, b="Hello", "World"
>>>print(a+b)
HelloWorld

关于字符串拼接性能,有这样一个结论,参考Stackoverflow.com

  • If you using 2.6, use the + operator.
  • if you're using 2.7 use the '%' operator.
  • if you're using 3.x use ''.join().

当然字符串是不能改变的,也就是说

>>>a="Test"
>>>a[1]='a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

05. List - 列表

理解为数组比较合适,可以索引和进行切片

>>>a=[1, 2, 3]
>>>a[1]='a'
>>>print(a)
[1, 'a', 3]

list内置方法:

  • append: 往数组尾添加一个元素
  • clear: 清空数组
  • copy: 创建一个本数组的拷贝
  • count: 返回值匹配值的个数
  • extend: 数组拼接
  • index: 返回匹配值的索引位置
  • insert: 在指定索引位置插入值
  • pop: 在指定索引位置删除值,并返回被删除的值
  • remove: 将数组中第一个匹配的值进行删除
  • reverse: 倒排数组
  • sort: 可以按照自定义方法进行排序
>>> a=[1,2,3]
>>> a.append(4)
>>> print(a)
[1, 2, 3, 4]

>>> a.clear()
>>> print(a)
[]

>>> b=[4,5,6]
>>> a=b.copy()
>>> a[1]=2
>>> print(a,b)
[4, 2, 6] [4, 5, 6]

>>> a=[1,2,2,3,5,2]
>>> print(a.count(2))
3

>>>b=['a', 'b', 'c']
>>>a.extend(b)
>>>print(a,b)
[1, 2, 2, 3, 5, 2, 'a', 'b', 'c'] ['a', 'b', 'c']

>>>print(a.index('a'), a.index(2, 3,10))
6 5

>>>b.insert(4, 'd')
>>>print(b)
['a', 'b', 'c', 'd']


>>>b.pop(2)
>>>print(b)
['a', 'b', 'd']

>>>a.remove(2)
>>>print(a)
[1, 2, 3, 5, 2, 'a', 'b', 'c']

>>>a.reverse()
>>>print(a)
['c', 'b', 'a', 2, 5, 3, 2, 1]

>>>b.sort()
>>>print(b)
['a', 'b', 'd']

06. Tuple - 元组

元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号 () 里,元素之间用逗号隔开。

注意:

  • 与字符串一样,元组的元素不能修改
  • 元组也可以被索引和切片,方法一样
  • 元组也可以使用+操作符进行拼接

07. Set - 集合

集合(Set), 主要用来进行集合计算和排重

>>> s={'a', 'b', 'c', 'c'}
>>> print(s)
{'b', 'a', 'c'} #重复的元素被自动去掉

>>> b = set('cdef')
>>> print(b)
{'f', 'e', 'd', 'c'}
>>> print(s-b)                     #求差集
{'b', 'a'}
>>> print(s|b)                     #求并集
{'b', 'e', 'c', 'f', 'a', 'd'}
>>> print(s&b)                     #求交集
{'c'}
>>> print(s^b)                     #s和b中不同时存在的元素
{'e', 'a', 'b', 'f', 'd'}

08. Dict - 字典

字典是一个很常用的数据类型,类似于hashmap,性能很好,可以满足各种key, value形式的数据存取

>>> s={'name': 'Willie', 'age':18, 'female': False}
>>> s.update({'job': 'student'})
>>>print(s)
{'age': 18, 'female': False, 'job': 'student', 'name': 'Willie'}

注意:

  • 字典是一种映射类型,它的元素是键值对
  • 字典的关键字必须为不可变类型,且不能重复

09 更多内容

原文来自兔子先生网站:https://www.xtuz.net/detail-12.html

查看原文 >>> Python基础数据类型

如果你对Python语言感兴趣,可以关注我,或者关注我的微信公众号:xtuz666

posted @ 2020-03-10 20:26  兔子先生wt  阅读(247)  评论(0编辑  收藏  举报