Python标准的数据类型

今天主要介绍Python中一些主要的数据类型:数字、字符串、列表、元组、字典等,为后续学习Pandas、Numpy、Scipy等科学计算模块做相应的铺垫。
Python 定义了一些标准类型,用于存储各种类型的数据。主要有五个标准的数据类型:

  • Numbers(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)

(一)Numbers(数字)

这里写图片描述
示例运行结果:
这里写图片描述

(二)String(字符串)

这里写图片描述
示例如下:

print("\tI'm tabbed in.")
print("I'm split\non a line.")
print("I'm \\ a \\ cat.")
a = "Hello"
print('c'+'h'+'e'+'e'+'s'+'e')
print("-"*20)
print(a[1],'/',a[1:4])
print("H" in a,'/',"M" not in a)
print("连接多个字符串示例: %s 连接多个 %s 字符串 %s " % ('eyes', 'hair', 'teeth'))
print("连接数字number用百分之%d" % 12)
print("连接百分数用%.0d%%" % 100)
print("%r %r %r %r" % ('one', 'two', 'three', 'four'))

结果如下:
这里写图片描述

(三)List(列表)

这里写图片描述
示例如下:

# 将元组转换为列表
aTuple = (123, 'xyz', 'zara', 'abc')
print(list(aTuple))

# append()/extend()方法区别:
aList = [123, 'xyz', 'zara', 'abc']
aList.append('abc'), print(aList)
aList.append([7, 8, 9]), print(aList)
aList.extend([7, 8, 9]), print(aList)
# append() 方法向列表的尾部添加一个新的元素
# extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中

# 统计某个元素在列表中出现的次数
print(aList.count(123), aList.count('abc'))
# 从列表中找出某个值第一个匹配项的索引位置
print(aList.index('zara'))
# 将对象插入列表
aList.insert(3, 2018), print(aList)
# 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
print(aList.pop(-4), aList.pop(-1))
# 移除列表中某个值的第一个匹配项
aList.remove('abc'), print(aList)
# 反向列表中元素
aList.reverse(), print(aList)

结果如下:
这里写图片描述

(四)Tuple(元组)

这里写图片描述

(五)Dictionary(字典)

这里写图片描述
Python 直接赋值、浅拷贝和深度拷贝解析:
(1)直接赋值:其实就是对象的引用(别名)。
(2)浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象。
(3)深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 。

示例如下:

a = {1: [1, 2, 3]}
b = a.copy()
print('原始:%s,%s' % (a, b))
a[1].append(4)  # 修改字典a
print('修改后:%s,%s' % (a, b))  # 浅拷贝字典b也改变

import copy
c = copy.deepcopy(a)
print('原始:%s,%s' % (a, c))
a[1].append(5)  # 修改字典a
print('修改后:%s,%s,%s' % (a, b, c))  # 深拷贝字典c不改变

结果如下:
原始:{1: [1, 2, 3]},{1: [1, 2, 3]}
修改后:{1: [1, 2, 3, 4]},{1: [1, 2, 3, 4]}
原始:{1: [1, 2, 3, 4]},{1: [1, 2, 3, 4]}
修改后:{1: [1, 2, 3, 4, 5]},{1: [1, 2, 3, 4, 5]},{1: [1, 2, 3, 4]}
参考:http://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html

其他函数示例:

# dict.fromkeys(seq[, value])函数
seq = ('Google', 'Runoob', 'Taobao')
dict = dict.fromkeys(seq)
print("新字典为 : %s" % str(dict))
dict = dict.fromkeys(seq, 10)
print("新字典为 : %s" % str(dict))

# dict.get(key, default=None)函数
dict = {'Name': 'Zara', 'Age': 27}
print("Value : %s" % dict.get('Age'))
print("Value : %s" % dict.get('Sex'))

# dict.__contains__(key)/dict.has_key(key)函数(python3/2)
print("Value : %s" % dict.__contains__('Age'))
print("Value : %s" % dict.__contains__('Sex'))

# dict.items()
print("字典值 : %s" % dict.items())

# dict.keys()/dict.values()
dict = {'Name': 'Zara', 'Age': 7}
print("Value : %s" % dict.keys())
print("Value : %s" % dict.values())

# dict.update(dict2)
dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female'}
dict.update(dict2)
print("Value : %s" % dict)

# pop(key[,default])
site = {'Name': 'Zara', 'Age': 7}
pop_obj = site.pop('Name')
print(pop_obj)

# popitem()
site= {'name': '菜鸟教程', 'alexa': 10000, 'url': 'www.runoob.com'}
pop_obj = site.popitem()
print(pop_obj)
print(site)

结果如下:
这里写图片描述

需要注意:
字典的键可以使用布尔类型的,True 默认代表 1,False 默认代表 0,如果包含 0 或 1 就无法使用布尔类型:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/KEEP_GIONG/article/details/80701691