@Channelchan
2017-11-21T19:09:22.000000Z
字数 1790
阅读 137393
最常用的数字类型包括整型数(int),浮点型(float),正负数皆可。
a=666
type(a)
int
b=-6.66
type(b)
float
主要用于判断真假,True代表真,False代表假。
i=1
i>0
True
i<0
False
字符串或串(String)是由数字、字母、下划线组成的一串字符。用引号限制起来。
string='大鱼金融是你身边No_1的Quantitative Trader'
string[0:4]
'大鱼金融'
提醒:Python默认索引的起始值为0,不为1。
元组是一系列Python数据类型安装顺序组成的序列。用小括号表征。报错由于元组内容不可变。
t=(6,'abc',0.1)
t[0]
6
t[1]
'abc'
t[0]=666
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-b98c09a85d62> in <module>()
----> 1 t[0]=666
TypeError: 'tuple' object does not support item assignment
列表和元组功能上类似,但表达方法不同,列表用[]表示。列表内容可修改。
l = [6,'abc',0.1]
l[0]
6
l[0]=666
l
[666, 'abc', 0.1]
字典是非常实用的数据结构,它包括了多组键(Key):值(Value)。字典用{}表示。
d={}
d['microsoft']=100
d['apple']= 150
d['google']=200
d
{'apple': 150, 'google': 200, 'microsoft': 100}
d['apple']
150
d.keys()
dict_keys(['microsoft', 'apple', 'google'])
d.values()
dict_values([100, 150, 200])
d.get('apple')
150
# 修改
d['apple'] = 50
d
{'apple': 50, 'google': 200, 'microsoft': 100}
# 删除
del d['apple']
d
{'google': 200, 'microsoft': 100}
# 添加字典
d.update({'apple':50})
d
{'apple': 50, 'google': 200, 'microsoft': 100}
# pop删除
d.pop('apple')
50
d
{'google': 200, 'microsoft': 100}
for k,v in d.items():
print (k)
microsoft
google
函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
x = set('apple')
y = set('google')
z = set([0,0,0,1,1,1,2,1,3,4,5,6,5,6,6])
#无序去重
x
{'a', 'e', 'l', 'p'}
y
{'e', 'g', 'l', 'o'}
z
{0, 1, 2, 3, 4, 5, 6}
# 交集
x&y
{'e', 'l'}
# 并集
x|z
{0, 1, 2, 3, 4, 'l', 5, 6, 'a', 'e', 'p'}
# 差集
x-y
{'a', 'p'}
t = (1,2,3,4,5,6,6,6,6)
list(t)
[1, 2, 3, 4, 5, 6, 6, 6, 6]
tuple(t)
(1, 2, 3, 4, 5, 6, 6, 6, 6)
set(t)
{1, 2, 3, 4, 5, 6}
keys = list(set(t))
values = ['a','b','c','d','e','f', 'g']
函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
list(zip(keys, values))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')]
dict(zip(keys, values))
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'}