这里,我将 Python 中常见的数据类型分为三大类:
- 数值(Number)—— 3种
对,这里我理解为“数值”,而不是“数字”! - 复合数据类型数据(Composite Data type) —— 5种
- 特殊数据类型
None
(空值)—— 1种
Python 中查看对象的数据类型:使用type()
函数
具体如下表所示:
Data type | example | |
---|---|---|
Number | ||
(数值) | ||
int 整型 | 10 | |
float 浮点数 | 2.0 | |
bool 布尔值 | True(1)、False(0) | |
complex 复数 | 2+3j | |
Composite Data Type | ||
(复合数据类型) | ||
String 字符串 | “hello” | |
List 列表 | [3, 2, 1] | |
Tuple 元组 | (1, 2, 3) | |
Dictionary 字典 | {‘a’:1, ‘b’:2, ‘c’:3} | |
Set 集合 | {3, 2, 2, 1, 0} | |
None(空值) |
注:这里我将布尔值也归入数值型!
1. 数值
我这里认为 Python 有四种数值型:
-
整型(int)
整数,是正数或者负数
-
浮点型(float)
包含小数的正数或者负数(也可以用带
e
的科学记数) -
复数(complex)
复数用
j
作为虚部 -
布尔型(bool)
布尔值只有两个,
True
和False
注:按普遍说法,Python 中有三种数字类型(整型、浮点型、复数)
我这里将布尔值加入上面的数字类型,变为4种数值型
附:Python 中随机数的表示
在 Python 中并没有random()
函数来创建随机数,但是可以使用内置函数模块random
来生成随机数:
整数:
random.randrange(stop)
random.randrange(start, stop[, step])
:return range(start, stop, step)random.randint(a, b)
:b <= return <= a ,实质上是Alias for randrange(a, b+1)
小数:
random.random()
:return ∈ [0.0, 1.0)random.uniform(a, b)
: b <= return <= a
2. 复合数据类型
Python 中有五种复合数据类型:
- 列表(List)
- 字符串(String)
- 元组(Tuple)
- 集合(Set)
- 词典(Dictionary)
(1)对象
#(1)String
# 字符串是数组
s = "Today is a good day !"
# 索引和切片
print(f"s[0]")
print(f"s[2:5]")
print(f"s[-5:-2]")
print(f"s[-5:]")
(2)操作
#(1)List
lst = [1, 3, 5, 7, 9]
# 查看属性
print(type(lst))
print(dir(lst))
## List自带的方法
# 增
lst.append(10) # L.append(object)
print(f'lst.append(10): {lst}')
lst.insert(1, 3) # L.insert(index, object)
print(f'lst.insert(1, 2): {lst}')
# 删
lst.remove(3)
print(f'lst.remove(2): {lst}')
lst.pop(0) # L.pop([index])
print(f'lst.pop(0): {lst}')
lst.pop() # default last
print(f'lst.pop(): {lst}')
# 改变顺序
lst.reverse()
print(f'lst.reverse(): {lst}')
# 合并
lst.extend(lst)
print(f'lst.extend(lst): {lst}')
# 元素计数
lst_count = lst.count(9)
print(f'lst.count(1): {lst_count}')
## Python 内建语句和函数
del lst[1]
print(f'del lst[1]: {lst}')
lst = sorted(lst)
print(f'sorted(lst): {lst}')
print()
#(2)Tuple
## 元组是不可变数据类型,因此只能查询,而没有增删等修改操作。(可以将元组转换为列表进行更改))
tup = (2, 4, 6, 8, 10)
# 元素计数
tuple_count = tup.count(8)
print(f'tuple_count: {tuple_count}')
# 查询索引
tuple_index = tup.index(6)
print(f'tuple_index: {tuple_index}')
print()
# String
# 字符串是不可变类型,但是注意,字符串不仅仅可以查询,它可以通过新建一个新字符串的方式来进行修改操作
s = "Today is a good day !"
## 查询
string_count = s.count('d')
print(f"s.count('d'): {string_count}")
string_find = s.find('s')
print(f"s.find('s'): {string_find}")
## 修改
# 替换:replace
(待做)
# 分隔合并:join
string_join = '-'.join(s) # S.join(iterable) 注意:是用字符串S去分隔iterable
print(f"string_join': {string_join}")
# 大小写
string_lower = s.lower() # 全部小写
print(f'string_lower: {string_lower}')
string_upper = s.upper() # 全部大写
print(f'string_upper: {string_upper}')
string_title = s.title() # 首字母大写
print(f'string_title: {string_title}')
# 居中
string_center = s.center(30, '+') # 注意:填充物的长度只能是1
print(f'string_center: {string_center}')
# 对齐
left_just = s.ljust(30, '-') # 左对齐
print(f'left_just: {left_just}')
right_just = s.rjust(30, '-') # 右对齐
print(f'right_just: {right_just}')
print()
#(3)Dictionary
dic = {'a':1, 'b':2, 'c':3}
# 访问:# dic[key]
print(f"dic['b']: {dic['b']}")
# 修改:dic[key] = value_new
dic['b'] = '5'
print(f'dic[key] = value_new: {dic}')
# 增加 # dic[new_key] = value_new
dic['d'] = '8'
print(f"dic[new_key] = value_new: {dic}")
# 删除:# del dic[key]
del dic['b']
print(f"del dic['b']: {dic}")
# 遍历读取
for k in dic.keys():
print(f'keys: {k}') # 获取 keys
print(f'values: {dic[k]}') # 间接获取 values
for v in dic.values():
print(f'values: {v}') # 间接获取 values
for k,v in dic.items(): # 同时获取 keys 和 values
print(k, v)
print()
#(4)Set
## 集合主要用于 元素去重
# 创建
blank_basket = {} # 需要注意,建立空集合需要用 set(),而不是{}
print(type(blank_basket))
basket = {'1', '2', 3, 4, 4, (5)}
print(type(basket))
# 元素去重
basket = set(basket) # 注意集合是没有顺序可以言的
print(f'set(basket): {basket}')
<class 'list'>
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
lst.append(10): [1, 3, 5, 7, 9, 10]
lst.insert(1, 2): [1, 3, 3, 5, 7, 9, 10]
lst.remove(2): [1, 3, 5, 7, 9, 10]
lst.pop(0): [3, 5, 7, 9, 10]
lst.pop(): [3, 5, 7, 9]
lst.reverse(): [9, 7, 5, 3]
lst.extend(lst): [9, 7, 5, 3, 9, 7, 5, 3]
lst.count(1): 2
del lst[1]: [9, 5, 3, 9, 7, 5, 3]
sorted(lst): [3, 3, 5, 5, 7, 9, 9]
tuple_count: 1
tuple_index: 2
s.count('d'): 3
s.find('s'): 7
string_join': T-o-d-a-y- -i-s- -a- -g-o-o-d- -d-a-y- -!
string_lower: today is a good day !
string_upper: TODAY IS A GOOD DAY !
string_title: Today Is A Good Day !
string_center: ++++Today is a good day !+++++
left_just: Today is a good day !---------
right_just: ---------Today is a good day !
dic['b']: 2
dic[key] = value_new: {'a': 1, 'b': '5', 'c': 3}
dic[new_key] = value_new: {'a': 1, 'b': '5', 'c': 3, 'd': '8'}
del dic['b']: {'a': 1, 'c': 3, 'd': '8'}
keys: a
values: 1
keys: c
values: 3
keys: d
values: 8
values: 1
values: 3
values: 8
a 1
c 3
d 8
<class 'dict'>
<class 'set'>
set(basket): {3, 4, 5, '2', '1'}
注:
1、sort()
和sorted()
函数的区别
首先,sort()是list的方法函数;sorted()是python的内建函数,
所以sort()的使用方法是句点表示法,调用的是list的方法函数,所以sort()只能接受list对象,而sorted函数可以接受各种对象。
其次,sort()方法可以设置两个参数:①指定参考对象②升序或者降序