Python 基础学习笔记
2016-07-09 23:56
337 查看
一些内容来自网络 : http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢! 一行简单的代码 ,速度提升一倍 from time import time t = time() list =['a','b','is','python','jason','helllo','hill','with','phone','test','dfsd','app','prdfe','ind','basic','onone','baecr','var','bana','dd','wrd'] list = dict.fromkeys(list,True) # 此行代码可令代码提升一倍速度 print list filter=[] for i in range(10000000) : for find in ['is','hat','new','list','old','.']: if find not in list: filter.append(find) print "total run time :" print time() -t #字典和list 的时间复杂度 字典 < list1 python 命令行模式,小程序,脚本
#!/usr/bin/env python print('Hello World!')chmod 755 hello.py
./hello.pyprint命令行模式: 运行Python,在命令行输入命令并执行。程序模式: 写一段Python程序并运行。>>>print a,type(a) print的另一个用法,也就是print后跟多个输出,以逗号分隔。2.sequence序列 ,一组有顺序的对象的集合序列有两种:tuple(定值表;也有翻译为元组) 和 list (表)>>>s1= (2, 1.3, 'love', 5.6, 9, 12, False) # s1是一个tuple>>>s2= [True, 5, 'smile'] # s2是一个listtuple和list的主要区别在于,一旦建立,tuple的各个元素不可再变更,而list的各个元素可以再变更。可变是指: 你可以对list的某个元素赋值: >>>s2[1] = 3.0序列使用方法 : 序列元素的下标从0开始 s1[i] ,2.使用上下限 s1[2:] 字符串是一种特殊的元组 tuple,因此可以执行元组的相关操作。3. 运算 pass4.选择和循环
if i == 2: 判断是否等于 2 用 双等号
5.函数
def square_sum(a,b): #def,这个关键字通知python:我在定义一个函数。square_sum是函数名。 c = a**2 + b**2 return c # 返回c的值,也就是输出的功能。Python的函数允许不返回值,也就是不用return。return可以返回多个值,以逗号分隔。相当于返回一个tuple(定值表)。return a,b,c # 相当于 return (a,b,c)
基本数据类型的参数:值传递
表作为参数:指针传递
6.类,对象,方法
class new(object): #括号中为object,说明这个类没有父类
def methond(self,,,):
继承 class old(new):特殊方法 :如果你在类中定义了__init__()这个方法,创建对象时,Python会自动调用这个方法。这个过程也叫初始化。
class happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_words summer = happyBird('Happy,Happy!')类属性和对象的性质的区别 : http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.htmlprint [1,2,3] + [5,6,9] --> [1, 2, 3, 5, 6, 9]重写 减法操作使满足list class superList(list): def __sub__(self, b): a = self[:] # 这里,self是supeList的对象。由于superList继承于list,它可以利用和list[:]相同的引用方法来表示整个对象。 b = b[:] while len(b) > 0: element_b = b.pop() if element_b in a: a.remove(element_b) return aprint superList([1,2,3]) - superList([3,4])7.字典>>>dic = {'tom':11, 'sam':57,'lily':100}
与List不同的是,词典的元素没有顺序。你不能通过下标引用元素。词典是通过键来引用。8.文本文件的输入输出创建文件 f = open (file nmae ,model) f.write(content);9 module 模块 在Python 中一个 .py 文件就是一个模块 模块的引用 模块包将功能相似的模块放在同一个文件夹中,构成一个模块包 Import this_dir.module但是文件夹中必须有一个__init__.py文件,提醒Python ,该文件夹是一个模块包 ,init 文件可以是空文件 10 参数传递 位置,关键字,参数默认值 ,包裹传递,解包裹11 跟多函数 range() enumerate()
S = 'abcdefghijk'for (index,char) in enumerate(S):print indexprint charenumerate()在每次循环中,返回的是一个包含两个元素的定值表(tuple),两个元素分别赋予index和charzip() xl = [1,3,5] yl =[9,12,13] zip(xl,yl)12 循环对象 :迭代器 ,生成器 ,(表推导 L = [x**2 for x in range(10)] L = [ x**2 for (x,y) in zip(xl,yl) if y> 10])13. 函数对象 lambda 函数 func = lambda x,y:x + yprint func(3,4)等价于def func(x,y): return x+y函数作为参数传递,传递的是函数 ,而不是函数的返回值 map() 函数 map 的第一个参数是函数对象 re = map((lambda x,y:x+y),[1,2,3],[6,7,9]) ,返回的re 可以转化为list 函数
filter()函数的第一个参数也是函数对象
reduce() 函数 ,的第一个参数也是函数,但要求这个函数必须能够接受两个参数,Reduce可以累进的将函数作用于各个参数
print reduce((lambda x,y: x+y),[1,2,5,7,9]) ---> (((1+2)+5)+7)+9 实现累加
14 异常处理
try:for i in range(100):print re.next()except StopIteration:print 'here is end ',iprint 'HaHaHaHa'抛出异常
raise StopIteration()
15 动态类型
a = 1 a 的类型 是int
a = 'Str' a 的类型是字符串
经验太浅,不知从何说起。个人的Python经验也非常有限。希望能多参与到一些项目,以便能积累经验。
更高:
时间、存储、正则表达式、http://www.cnblogs.com/vamei/archive/2012/07/23/2605345.html函数 :内置函数type(), 用以查询变量的类型。dir()用来查询一个类或者对象所有属性。 printdir(list)help()用来查询的说明文档。 help(list)c:\Python27>python one.py['a', 'b', 'is', 'python', 'jason', 'helllo', 'hill', 'with', 'phone', 'test', 'dfsd', 'app', 'prdfe', 'ind', 'basic', 'onone', 'baecr', 'var', 'bana', 'dd', 'wrd']total run time :74.0370001793c:\Python27>python one.py{'a': True, 'prdfe': True, 'wrd': True, 'b': True, 'jason': True, 'dfsd': True, 'python': True, 'is': True, 'dd': True, 'baecr': True, 'var': True, 'phone': True, 'ind': True, 'test': True, 'onone': True, 'hill': True, 'basic': True, 'helllo': True, 'bana':True, 'with': True, 'app': True}total run time :49.5829999447
相关文章推荐
- Turtle随机生成图像
- [leetcode]344. Reverse String
- [leetcode]345. Reverse Vowels of a String
- pygame实现简易音乐播放器
- python笔记-import用法
- Python介绍
- python笨办法解决zipfile解压会改变文件最后修改时间的问题
- python视频教程大全集下载
- Python JSon实例1
- python装饰器的使用
- 生成六位验证码python代码
- cpu caffe cifar10 ubuntu 14.04 python
- python爬虫
- python实现简单kNN
- Python2 之 print函数示例
- The best way to get unique elements of a list in Python
- Python学习笔记之缩进
- 好用的Python IDLE Sublime Text 3推荐
- 使用lxml的xpath读取网页中的表格并转化为pandas的DataFrame
- NumPy的详细教程