感谢斌哥
这是我学习运维以来看的第二套视频:
第一套是《哈佛大学公开课》:构建动态网站,真的是很棒的视频,只看了两节,后来一个是对php不太感兴趣,另一个是也太忙了,就没继续看了。第二套就是现在斌哥的这套,上周就下载了,一直有事拖到周一才看完,坐在电脑前没有快进把视频完整的看完了。好的书籍总会让你爱不释手,好的电影没有快进,斌哥出的前两个python视频真的非常棒,期待斌哥的下一集。正好最近事情不是很多,借着这个机会跟着斌哥把python好好的学习下。
第零节:热身运动
本节课程是个动员视频,没有太多实际的内容
视频录制软件:screenFlow主讲:张斌参考书籍:《Head First Python》说明: 斌哥的视频里并没有留下演示用的源码,如果有需要源码可以到github上搜索下,先列出两个别人传上去的: https://github.com/foxli180/HeadFirstPython https://github.com/CubeSugar/HeadFirstPython视频地址:http://opslinux.com/python_viode_0.html优点:1.声音很好听,这绝对是优点...2.斌哥是要将自己多年的python web开发经验无私奉献出来了,这绝对是我这种菜鸟的福利,期待斌哥的快速迭代式python学习方法。缺点:1.貌似电脑的上的qq声音没关,影响观看视频效果。2.讲python的视频,幻灯片还是c的...大神,有点懒...
第一节:人人都爱列表
我看了下《Head First Python》的中文版,斌哥确实是按照这里面的顺序来讲的,下面对本节课的学习简单的做个总结。
非技术方面:
写代码时,看斌哥说的他的习惯是””,先打出两个”,然后再往里写代码,顺序是”->”->code;我平时的习惯是”->code->”,注意到这个是因为我在sublime里写html时,老是会自动先给我打出两个”来,感觉很别扭。这个有待求证斌哥为什么要这么写,有什么好处。
技术方面:
地址:http://opslinux.com/python_viode_1.html http://opslinux.com/python_viode_2.html 本节课的内容主要是对列表的处理,简单的回顾下。 1.定义一个列表 In [163]: movies = ['The Holy Grail','The Life of Brian','The meaning of life1’] 2.打印列表第一个元素,下标从0开始 In [165]: print movies[0] The Holy Grail 3.统计列表的长度 In [164]: print len(movies) 3 In [165]: 4.向列表追加一个元素 In [166]: movies.append("one") In [167]: movies Out[167]: ['The Holy Grail', 'The Life of Brian', 'The meaning of life1', 'one’] 5.拓展列表,向列表中一次追加另一个列表的值 Out[168]: ['The Holy Grail', 'The Life of Brian', 'The meaning of life1', 'one'] In [169]: a = ["two","three"] In [170]: movies.extend(a) In [171]: movies Out[171]: ['The Holy Grail', 'The Life of Brian', 'The meaning of life1', 'one', 'two', 'three'] In [172]: a Out[172]: ['two', 'three’] 6.列表的插入 In [174]: movies Out[174]: ['The Holy Grail', 'The Life of Brian', 'The meaning of life1'] In [175]: movies.insert(1,1975) In [176]: movies.insert(3,1979) In [177]: movies.append(1983) In [178]: print movies ['The Holy Grail', 1975, 'The Life of Brian', 1979, 'The meaning of life1', 1983] 7.列表的迭代,可以使用for和while(while这个语句平时很少用,用到时都是while True什么来着…),建议用for In [178]: print movies ['The Holy Grail', 1975, 'The Life of Brian', 1979, 'The meaning of life1', 1983] In [179]: for each_item in movies: .....: print each_item .....: The Holy Grail 1975 The Life of Brian 1979 The meaning of life1 1983 In [180]: In [180]: count = 0 In [181]: while count < len(movies): .....: print movies[count] .....: count = count + 1 .....: The Holy Grail 1975 The Life of Brian 1979 The meaning of life1 1983 8.处理复杂的聊表数据,输出嵌套列表的值 In [187]: print movies ['The Holy Grail', 1975, 'Terry Jones&Terry Gilliam', 91, ['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]] In [188]: for each_item in movies: .....: if isinstance(each_item,list): .....: for each_subitem in each_item: .....: if isinstance(each_subitem,list): .....: for each_ssubitem in each_subitem: .....: print each_ssubitem .....: else: .....: print each_subitem .....: else: .....: print each_item .....: The Holy Grail 1975 Terry Jones&Terry Gilliam 91 Graham Chapman Michael Palin John Cleese Terry Gilliam Eric Idle Terry Jones 9.可以看到8有很多的for迭代,这个属于代码的冗余,可以使用一个递归函数来解决(斌哥的第一节视频还没讲到,参见《Head Firts Python》 In [189]: def print_list(the_list): .....: for each_item in the_list: .....: if isinstance(each_item,list): .....: print_list(each_item) .....: else: .....: print each_item .....: In [191]: print_list(movies) The Holy Grail 1975 Terry Jones&Terry Gilliam 91 Graham Chapman Michael Palin John Cleese Terry Gilliam Eric Idle Terry Jones 10.最后本节课的代码可以参考: https://github.com/foxli180/HeadFirstPython/blob/master/01-list-movie.py
我的补充:
其实python的列表还有很多好玩的东西,下面简单的列出几个.1.分片,打印出第一个到第三个(不包含第三个)元素,当然还有分片赋值等有意思的玩法,这里不再介绍 In [192]: numbers = [1,2,3,4,5,6] In [193]: print numbers[1:3] [2, 3]2.利用分片浅复制列表,仔细观察修改列表a和修改列表b后numbers的变化 In [194]: numbers Out[194]: [1, 2, 3, 4, 5, 6] In [195]: a = numbers[:] In [196]: a Out[196]: [1, 2, 3, 4, 5, 6] In [197]: a.append(7) In [198]: a Out[198]: [1, 2, 3, 4, 5, 6, 7] In [199]: numbers Out[199]: [1, 2, 3, 4, 5, 6] In [200]: b = numbers In [201]: b.append(8) In [202]: b Out[202]: [1, 2, 3, 4, 5, 6, 8] In [203]: numbers Out[203]: [1, 2, 3, 4, 5, 6, 8] In [204]:3.求列表的最大值、最小值 In [203]: numbers Out[203]: [1, 2, 3, 4, 5, 6, 8] In [204]: max(numbers) Out[204]: 8 In [205]: min(numbers) Out[205]: 14.删除列表中的某个元素 In [208]: numbers Out[208]: [1, 2, 3, 4, 5, 6, 8] In [209]: del numbers[6] In [210]: numbers Out[210]: [1, 2, 3, 4, 5, 6]5.常用列表方法简介 dir(list)可以看到所有的列表方法,下面对他们做个简单的介绍 In [211]: dir(list) Out[211]: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort’] (a). append这个是向列表末尾追加一个元素,如下: In [212]: numbers = [1,2,2,3,3,3] In [213]: numbers.append(4) In [214]: numbers Out[214]: [1, 2, 2, 3, 3, 3, 4] (b). count 计算某个元素在列表中出现的次数,如下: In [214]: numbers Out[214]: [1, 2, 2, 3, 3, 3, 4] In [215]: numbers.count(1) Out[215]: 1 In [216]: numbers.count(2) Out[216]: 2 In [217]: numbers.count(3) Out[217]: 3 In [218]: numbers.count(4) Out[218]: 1 (c). extend 直接向列表末尾一次性追加另一个列表,如下: In [219]: numbers Out[219]: [1, 2, 2, 3, 3, 3, 4] In [220]: a = [4,4,4] In [221]: numbers.extend(a) In [222]: num num numbers In [222]: numbers Out[222]: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] In [223]: a Out[223]: [4, 4, 4] In [224]: (d). index 从列表中找出某个值第一个匹配的索引位置 In [225]: numbers Out[225]: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] In [226]: numbers.index(1) Out[226]: 0 In [227]: numbers.index(2) Out[227]: 1 In [228]: numbers.index(3) Out[228]: 3 In [229]: numbers.index(4) Out[229]: 6 (e). insert 将对象插入列表中 In [230]: numbers Out[230]: [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] In [231]: numbers.insert(0,0) In [232]: numbers Out[232]: [0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4] (f). pop 移除列表中的最后一个元素,默认是最后一个 In [234]: numbers Out[234]: [0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4] In [235]: numbers.pop() Out[235]: 4 In [236]: numbers Out[236]: [0, 1, 2, 2, 3, 3, 3, 4, 4, 4] (g). remove 移除列表中第一个匹配的元素 In [240]: num Out[240]: [1, 2, 1, 3, 2] In [241]: num.remove(1) In [242]: num num numbers In [242]: num Out[242]: [2, 1, 3, 2] In [243]: num.remove(2) In [244]: num Out[244]: [1, 3, 2] (h). reverse 将列表中的元素反向存放 In [246]: numbers Out[246]: [0, 1, 2, 2, 3, 3, 3, 4, 4, 4] In [247]: numbers.reverse() In [248]: numbers Out[248]: [4, 4, 4, 3, 3, 3, 2, 2, 1, 0](i). sort 对列表排序,这个我比较喜欢,里面挺多好玩的东西,下面做个简单的介绍 (1) 默认排序: In [249]: L = [1,4,3,2] In [250]: L.sort() In [251]: L Out[251]: [1, 2, 3, 4] (2).自定义排序: 查看sort的使用方法 In [262]: print L.sort.__doc__ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 (a).改变默认的排序方法,搞个倒序试试 In [263]: def t(x,y): .....: if x > y: .....: return -1 .....: elif x == y: .....: return 0 .....: else: .....: return 1 .....: In [264]: L Out[264]: [1, 2, 3, 4] In [265]: L.sort(cmp) In [266]: L Out[266]: [1, 2, 3, 4] In [267]: L.sort(cmp=t) In [268]: L Out[268]: [4, 3, 2, 1] (b).根据提供的key函数为元素产生一个键,列表的元素按照这个键值来排序 In [269]: x = ['abc','a','bc','abcd'] In [270]: x.sort(key=len) In [271]: x Out[271]: ['a', 'bc', 'abc', 'abcd’] (c).反向排序 In [274]: n Out[274]: [3, 1, 2, 5] In [275]: n.sort(reverse=True) In [276]: n Out[276]: [5, 3, 2, 1]恩,暂时想到的就这么多,其他待补充......
第二节: 共享你的代码
好久没学python了,惭愧、惭愧。
说明:
本节课,《Head First Python》中的nester模块已经被其他人上传过了,我对那个模块也没太多的兴趣,就自己写了一个模块,用来爬取斌哥博客Home和Archives页面的文章,方便查询斌哥博客的更新情况。
正文:
视频地址:http://opslinux.com/python_viode_3.html1.首先到https://pypi.python.org去注册一个账号,这个不难。但是要注意有个坑,我先用的是qq邮箱注册,收不到确认邮件,可以正常登陆,但上传模块时会提示 Server response (401): basic auth failed;后来改用谷歌邮箱注册,顺利收到邮件,点击确认后,才可以正常上传模块。2.看下我的模块文件,新建的项目如下budong@budongdeMacBook-Pro:~/code_segment$ tree opslinux-project/opslinux-project/├── MANIFEST.in├── README├── opslinux│ ├── __init__.py│ ├── __init__.pyc│ ├── extract.py│ ├── extract.pyc│ ├── opslinux.py│ └── opslinux.pyc└── setup.py1 directory, 9 filessetup.py 文件内容如下:from distutils.core import setupsetup ( name = 'opslinux', version = '1.0.0', packages = ['opslinux'], author = 'bdzr', author_email = 'sias_peiqiang@foxmail.com', url = 'peiqiang.net', description = 'A spider for opslinux.com', )3.构建发布文件budong@budongdeMacBook-Pro:~/code_segment/opslinux-project$ python setup.py sdistrunning sdistrunning checkreading manifest template 'MANIFEST.in'writing manifest file 'MANIFEST'creating opslinux-1.0.0creating opslinux-1.0.0/opslinuxmaking hard links in opslinux-1.0.0...hard linking README -> opslinux-1.0.0hard linking setup.py -> opslinux-1.0.0hard linking opslinux/__init__.py -> opslinux-1.0.0/opslinuxhard linking opslinux/extract.py -> opslinux-1.0.0/opslinuxhard linking opslinux/opslinux.py -> opslinux-1.0.0/opslinuxcreating distCreating tar archiveremoving 'opslinux-1.0.0' (and everything under it)4.向pypi注册budong@budongdeMacBook-Pro:~/code_segment/opslinux-project$ python setup.py registerrunning registerrunning checkWe need to know who you are, so please choose either: 1. use your existing login, 2. register as a new user, 3. have the server generate a new password for you (and email it to you), or 4. quitYour selection [default 1]:Username: bdzrPassword:Registering opslinux to http://pypi.python.org/pypiServer response (200): OKI can store your PyPI login so future submissions will be faster.(the login will be stored in /Users/budong/.pypirc)Save your login (y/N)?y5.向pypi上传文件budong@budongdeMacBook-Pro:~/code_segment/opslinux-project$ python setup.py sdist uploadrunning sdistrunning checkreading manifest template 'MANIFEST.in'writing manifest file 'MANIFEST'creating opslinux-1.0.0creating opslinux-1.0.0/opslinuxmaking hard links in opslinux-1.0.0...hard linking README -> opslinux-1.0.0hard linking setup.py -> opslinux-1.0.0hard linking opslinux/__init__.py -> opslinux-1.0.0/opslinuxhard linking opslinux/extract.py -> opslinux-1.0.0/opslinuxhard linking opslinux/opslinux.py -> opslinux-1.0.0/opslinuxCreating tar archiveremoving 'opslinux-1.0.0' (and everything under it)running uploadSubmitting dist/opslinux-1.0.0.tar.gz to http://pypi.python.org/pypiServer response (200): OK6.安装模块[root@puppet mfs]# pip install opslinuxDownloading/unpacking opslinux Downloading opslinux-1.0.0.tar.gz Running setup.py (path:/tmp/pip_build_root/opslinux/setup.py) egg_info for package opslinuxInstalling collected packages: opslinux Running setup.py install for opslinuxSuccessfully installed opslinuxCleaning up…7.测试模块效果[root@puppet mfs]# pythonPython 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import opslinux>>> opslinux.last()标题: python开发实践(三)地址: http://opslinux.com/python_viode_3.html标题: python开发实践(二)地址: http://opslinux.com/python_viode_2.html>>> opslinux.all()标题: python开发实践(三)地址: http://opslinux.com/python_viode_3.html标题: python开发实践(二)地址: http://opslinux.com/python_viode_2.html标题: Django错误日志在命令行显示地址: http://opslinux.com/Django_Console_logging_to_STDOUT-.html标题: 配置sublime打造python编辑器地址: http://opslinux.com/sublime_python.html标题: python开发实践(一)地址: http://opslinux.com/python_viode_1.html8.本机课程结束,模块在线地址为,欢迎拍砖https://pypi.python.org/pypi/opslinux/1.0.0参考资料:1.《Head First Python》2.jkey blo: http://blog.jkey.lu/2013/04/11/create-python-egg/3.落: http://liluo.org/blog/2012/08/how-to-create-python-egg/
参考资料:
斌哥博客: http://opslinux.com/
原文地址:跟着斌哥学python, 感谢原作者分享。 大海,别为森林的渺小而沮丧,