一.基本数据类型

1.整形 

a=10
type(a)
Out[75]: int a.bit_length() #字节长度
Out[76]: 4 整形相除会返回整形,想要返回浮点数,需要用浮点数相除
1./4
Out[79]: 0.25

  

2.浮点型

b=0.25
type(b)
Out[80]: float 浮点数表示成有理分式

 b.as_integer_ratio()
  Out[81]: (1, 4)

那么怎么确定精度呢? 需要引入decimal模块

import decimal
 from decimal import Decimal

decimal.getcontext()
 Out[82]: Context(prec=8, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])

可以看到 精度是28,还有最大值、最小值
可以改变精度

decimal.getcontext().prec=4 # 改变精度为4

e=Decimal(1)/Decimal(11)  #计算1/11
  print(e)

输出结果:
 0.09091

确定精度为10:

decimal.getcontext().prec=10

e=Decimal(1)/Decimal(11)
 print(e)

0.09090909091

3.字符串

字符串表示文本

t='this is a string'

t.capitalize()
Out[88]: 'This is a string'

按tab键可以有自动补全功能,可以查看内建方法。比如:图中有字符串的一些内建方法,capitalize等。

t.split()  #字符串的分割
Out[23]: ['this', 'is', 'a', 'string'] #结果分割为列表 查找

 t.find('a')  #查找元素‘a’,返回元素位置,注意:python位置下标从0开始
 Out[25]: 8

 替换

t.replace(' ','|')  #把空格替换为‘|’
 Out[27]: 'this|is|a|string'

去除操作

s='http://wwww.python.org'
 s.strip('htp:/') #去除'htp:/'
 Out[29]: 'wwww.python.org'

正则表达式 re的简单应用

import re

series="""
'01/18/2014 13:00:00',100,'1st',   #三引号表示可以换行
'01/18/2014 13:00:00',110,'2nd',
'01/18/2014 13:00:00',120,'3rd',
"""

dt=re.compile(r"'[0-9/:\s]+'")  #匹配规则:反斜杠表示转译
result=dt.findall(series)
print(result)

["'01/18/2014 13:00:00'", "'01/18/2014 13:00:00'", "'01/18/2014 13:00:00'"] #返回值是一个列表

接着把时间序列转化为datetime对象

from datetime import datetime

pydt=datetime.strptime(result[0].replace("'",""),'%m/%d/%Y %H:%M:%S')
pydt

Out[102]: datetime.datetime(2014, 1, 18, 13, 0)

type(pydt)

Out[103]: datetime.datetime

小注:

datetime模块的时间与字符串转化:

import datetime
from datetime import datetime

datetime.datetime.strftime():由日期格式转化为字符串格式

datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')

Out[105]: '05-02-2018 18:27:58'

datetime.datetime.strptime():由字符串格式转化为日期格式

datetime.datetime.strptime('05-16-2017 21:01:35', '%m-%d-%Y %H:%M:%S')
Out[107]: datetime.datetime(2018, 5, 2, 18, 27, 58)

具体参考:https://www.cnblogs.com/cindy-cindy/p/6720196.html

二.基本数据结构

1.元组:tpule

t=(1,2.5,'date')

t1=1,2.5,'date'
type(t1)
Out[3]: tuple t1[2]
Out[4]: 'date'

只有两个方法 : 计数与下表返回索引
t.count(para)
t.index(para)

t.count(1)
Out[6]: 1 t.index(1)
Out[7]: 0

2.列表

列表的构建

l=[1,2,1.5]
l1=[x for x in range(10)]
l2=list(t)
print(l,'\n',l1,'\n',l2)
[1, 2, 1.5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2.5, 'date']

操作方法:

print('abc\ndef')  #\n  换行
abc
def type(l1)
Out[10]: list l.append([4,3]) #添加列表
l
Out[11]: [1, 2, 1.5, [4, 3]] l.extend([1,3,6]) # 添加元素
l
Out[12]: [1, 2, 1.5, [4, 3], 1, 3, 6] l.insert(1,'date')
l
Out[13]: [1, 'date', 2, 1.5, [4, 3], 1, 3, 6] l.remove('date')
l Out[14]: [1, 2, 1.5, [4, 3], 1, 3, 6] p=l.pop(2) # pop 方法 可用返回值接受
print(l,'\n',p) [1, 2, [4, 3], 1, 3, 6]
1.5 l[:2]
Out[16]: [1, 2]

3.字典

d={
'name':'John',
'sex':'male',
'age':18,
'country':'China'}
d['name']
Out[17]: 'John' d.keys() # 所有key组成的列表
Out[18]: dict_keys(['name', 'sex', 'age', 'country']) d.values()
Out[19]: dict_values(['John', 'male', 18, 'China']) d.items() # 字典转化为 list list中元素是元组,元组由字典的key 和balue组合
Out[20]: dict_items([('name', 'John'), ('sex', 'male'), ('age', 18), ('country', 'China')]) print('{} is {}'.format(d['name'],d['age']))
John is 18 birth=True if birth is True:
d['age']+=1 print(d['age'])
19

  

字典的遍历

for i in d:
print(i) name
sex
age
country
for item in d.items():
print(item) ('name', 'John')
('sex', 'male')
('age', 19)
('country', 'China')
for values in d.values():
print(values) John
male
19
China

4.集合

s=set(['u','i','du','du','u'])    

print(s)   

t=set(['du','u'])
t
{'i', 'u', 'du'}
Out[27]: {'du', 'u'}

交并补

s.union(t)
Out[29]: {'du', 'i', 'u'} s.intersection(t)
Out[30]: {'du', 'u'} s.difference(t)
Out[31]: {'i'}

5. 函数编程

简单的函数:

def f(x):
return x**2 f(2)
Out[32]: 4  

求偶数:

def even(x):
return x%2==0 even(2)
Out[33]: True

  

map函数:
 python3下的map()函数返回类型为iterators,不再是list

map()的使用方法形如map(f(x),Itera).对,它有两个参数,第一个参数为某个函数,第二个为可迭代对象。

list(map(even,range(4)))
Out[34]: [True, False, True, False] list(map(lambda x:x**2,range(4))) Out[35]: [0, 1, 4, 9]

filter 函数:

过滤功能,刷选出符合条件的

list(filter(even,range(15)))
Out[36]: [0, 2, 4, 6, 8, 10, 12, 14]

reduce函数:
 reduce()的使用方法形如reduce(f(x),Itera).对,它的形式和map()函数一样。不过参数f(x)必须有两个参数。reduce()函数作用是:把结果继续和序列的下一个元素做累积计算

from functools import reduce
reduce(lambda x,y:x+y,range(5)) Out[37]: 10

其实就是类似下边的函数:

def cunsum(x):
total=0
for i in range(x):
total+=i
return total cunsum(5)
Out[38]: 10

最新文章

  1. Linux虚拟机的安装(使用Centos6.3)
  2. WCF学习之旅—TcpTrace工具(二十五)
  3. Untiy3D开发小贴士 OnEnabled与OnDisabled
  4. 《OD大数据实战》Kafka入门实例
  5. uva208 - Firetruck
  6. PHP - 《高性能php应用开发》学习笔记
  7. 带A圈的秘密
  8. Windows命令行(DOS命令)教程-4(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_3.html
  9. MyEclipse建立JVM内存大小
  10. Elasticsearch 5.0 _all field的简单认识
  11. Linux和进程内存模型
  12. Windows CMD 支持ls命令
  13. DataUtils对Connection的获取、释放和关闭的操作学习
  14. BZOJ 4399 魔法少女LJJ(线段树合并)
  15. HDU1816(二分+2-SAT)
  16. 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框
  17. Innodb中自增长值的列
  18. NoSQL数据库---NoSQL数据库的分类
  19. Android 利用 gson 将 json 转成 对象object 集合list
  20. The declared package "com.dao" does not match the expected package "src.com.dao"

热门文章

  1. map集合中哪些是线程安全的
  2. ASP.NET Core 1.0: Deploy to IIS
  3. nyoj 260-数数小木块 (打表)
  4. CSS如何设置列表样式属性,看这篇文章就够用了
  5. 【Linux系列】Centos 7安装 Redis(六)
  6. Openlayers Overlay使用心得
  7. 看完这篇还不会用Git,那我就哭了!
  8. matlab-汉字unicode编码转换
  9. Python开发-实现Excel套打打印
  10. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean