ImageVerifierCode 换一换
格式:DOCX , 页数:125 ,大小:761.75KB ,
资源ID:6863815      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6863815.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(python学习笔记.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

python学习笔记.docx

1、python学习笔记python学习笔记(一)入门 1python学习笔记(二)数据类型 9python学习笔记(三)字符串 20python学习笔记(四)数据字典 25python学习笔记(五)语句 35python学习笔记(六)函数 45python学习笔记(七)文件和目录操作 57初识Redis及Redis在Windows下的安装和使用 64python小专题time模块 69python一些小知识 76python小专题JSON 79python小专题optparse模块 82python小专题urllib2模块 88python调用powershell、远程执行bat 92pytho

2、n对MySQL的CRUD 102python检测服务器是否ping通 107python操作excel表格(xlrd/xlwt) 110python学习笔记(一)入门 python很多人都非常熟悉,而我作为后知后觉者,虽然慢人一步,但是学习永远不会晚。其实作为shell,不管是perl还是ruby、powershell等,语法很相似的,我以前没接触过python,现在从最基础的学起,当然对于非常简单的并没有详细记录,简单的准备记录下应该注意的地方。虽然python3.X的shell工具已经出来了,但是相关教程好像没找到,而且与python2.x语法好多不兼容。所以我的学习环境是python s

3、hell2.7,也是目前最稳定和常用的版本吧。 娱乐阶段: 学习python之前,先来看看python的设计哲学,我觉得Guido van Rossum一定是个有趣的人,能将设计思想展现在python解释器中,呵呵。输入import this 命令:Python 2.7.3 (default, Apr 10 2012, 23:31:26) MSC v.1500 32 bit (Intel) on win32Type copyright, credits or license() for more information. import thisThe Zen of Python, by Tim

4、 PetersBeautiful is better than ugly. 优美胜于丑陋Explicit is better than implicit. 明了胜于晦涩Simple is better than complex. 简单胜过复杂Complex is better than complicated. 复杂胜过凌乱Flat is better than nested. 扁平胜于嵌套Sparse is better than dense. 间隔胜于紧凑Readability counts. 可读性很重要Special cases arent special enough to brea

5、k the rules. 即使假借特例的实用性之名,也不违背这些规则Although practicality beats purity. 虽然实用性次于纯度Errors should never pass silently. 错误不应该被无声的忽略Unless explicitly silenced. 除非明确的沉默 In the face of ambiguity, refuse the temptation to guess. 当存在多种可能时,不要尝试去猜测There should be one- and preferably only one -obvious way to do i

6、t. 应该有一个,最好只有一个,明显能做到这一点Although that way may not be obvious at first unless youre Dutch.虽然这种 方式可能不容易,除非你是python之父Now is better than never. 现在做总比不做好Although never is often better than *right* now. 虽然过去从未比现在好If the implementation is hard to explain, its a bad idea. 如果这个实现不容易解释,那么它肯定是坏主意If the impleme

7、ntation is easy to explain, it may be a good idea. 如果这个实现容易解释,那么它很可能是个好主意Namespaces are one honking great idea - lets do more of those! 命名空间是一种绝妙的理念,应当多加利用 哈哈,和一般的语言不同,在“hello world”程序开始之前,它还有一番人生哲学啊。 初步入门: 第一个python程序:(和其他脚本一样,可以按tab键快速选择) print hello world =print是一个函数hello world 这个在python3.0的解释器中运

8、行是错误的,应该写成:print(hello world),不管这些,以下均是2.X版本下。 基础知识: 交互式python解释器可以当非常强大的计算器使用 1+12 1/2 =和其他语言一样,不做任何处理的情况下,这个是取整0 1.0/2 =将任意一个数写成浮点形式,则结果会与精度最大的保持一致0.5 1./2 =单独写个小数点也行0.5 1/2 = /这个符号是专门取整的0 假如不想每次都要这么费劲一下,我就想在python里执行普通的除法,有办法: from _future_ import division =注意future左右是两个下划线 1/20.5 1/2 =在这种情况下你反而想

9、取整数部分了,使用/ 01./20.0 长整型和进制: 958346283662845 =2.2版本以前是不能处理长整型的,范围是-21474836482147483647958346283662845L =L表示长整型,而且是大写 0xAF =16进制175 010 =8进制 (010首数字是0,表8进制)8 获取用户输入: x=input(x=)x=3 y=input(y=)y=4 x*y12 函数: pow(2,3) =求幂函数8 2*38 abs(-10) =取绝对值10 round(5/2) =这里是先算5/2,即2,所以round(2)=2.02.0 round(2.5) =把浮点

10、数四舍五入为最接近的整数值3.0 floor(32.9) =取为不大于该数的最大整数32 模块: floor(23.5)=出错了Traceback (most recent call last): File , line 1, in floor(23.5)NameError: name floor is not defined import math =引入模块math math.floor(23.5)23.0 int(math.floor(23.5) =转换为整数23 那我不想每次都输入math.来调用相应函数,如下写法: from math import floor floor(3.2)3

11、.0 cmath和复数: 在高中的时候有复数的预算,比如对一个复数取平方根,python提供了对复数处理的机制,但是要引入cmath模块。 sqrt(-9)Traceback (most recent call last): File , line 1, in sqrt(-9)NameError: name sqrt is not defined import math =引入math模块也无效 math.sqrt(-9)Traceback (most recent call last): File , line 1, in math.sqrt(-9)ValueError: math doma

12、in error import cmath =引入cmath模块 cmath.sqrt(-9)3j (1+3j)*(9-2j) =还可以进行计算(15+25j) (_future_这个模块比较特别,它可以导入那些在未来会成为标准python组成的新特性) 保存并执行程序: 现在建一个python文件,扩展名是.py,把 print hello python!写入,双击,我们看到的是一闪而过的一个黑框,我记得在执行C#窗体程序的时候也会出现这种情况,只要在主程序结尾加上Console.ReadKey()就行了,这里也是,要给控制台一个提醒输入的机会,不然运行完就退出了,在结尾加一个语句:raw_

13、input()。再双击即可弹出“hello python!”执行这个文件hello.pyname=raw_input (what is you name:)print hello +name +!raw_input() 字符串: hello python! =双引号会把原字符串按原样显示hello python! well go shopping!well go shopping! well go shopping!SyntaxError: invalid syntax well go shopping! =转义单引号well go shopping! hello,python! she sa

14、id =字符串本身有双引号的情况hello,python! she saidhello + python! =字符串拼接hello python! str & repr: 您可能发现了,不用print打印出的字符串显示出来的时候会被单引号括起来。所有通过python打印的字符串是被引号括起来的,这是因为python打印值的时候会保持该值在代码中的状态,而不是你希望用户看到的状态。 print hello worldhello world print hello worldhello world hello worldhello world hello worldhello world prin

15、t 10000L10000 10000L10000L 这里讨论的实际是值转换成字符串的两种机制, str函数:会把值转换为较理性的字符串,以便用户可以理解 repr函数:会创建一个字符串,以合法的python表达式的形式表示值。 print repr(hello python!)hello python! print repr(100000L)100000L print str(hello python!) =显然,用户更希望看到的是str函数处理后的结果hello python! print str(10000L)10000 其实python有时候也没有这么智能,在高级语言中,好像数字有时候

16、可以自动转换为字符串型 age=22 print my age is + ageTraceback (most recent call last): File , line 1, in print my age is + ageTypeError: cannot concatenate str and int objects print my age is + str(age)my age is 22 input & raw_input 这样,我们先运行两个脚本,代码如下:name=input(please input your name:)print hello,+name另一个脚本是将上述

17、input改为raw_input 运行会发现,第一个出错。其实不运行脚本也行,我么直接在解释器里运行命令吧! name =input(please input name:)please input name:JayTraceback (most recent call last): File , line 1, in name =input (please input name:) File , line 1, in NameError: name Jay is not defined name =raw_input (please input name:)please input name:

18、Jay 其实你只要在input那个下面输入的字符串加上引号就行了,这就是原因。input会假设用户输入的是python的合法表达式,当然以字符串作为输入的名字肯定没有问题,但是要求用户每次输入一个东西还需要加引号,这不太友好。 想反,raw_input函数会把所有的输入当做原始数据,然后自动将其放入字符串中,所以不会出错,所以我们应尽可能使用 raw_input() 长字符串 如果需要写一个很长的字符串,它需要跨多行,可以使用三个单引号,强制换行用反斜杠 asdghagjawagasdghnagjawnag 1+5+ 4+616 python中对多个反斜杠(路径中常用)转义除了加,可以在整个字

19、串的前面加r print c:temptest.txt c: emp est.txt =t是制表符,所以会将t作为一个制表符显示 print c:temptest.txt =用传统的转义c:temptest.txt print rc:temptest.txt =在前面加r转义c:temptest.txt 常用的函数:abs(number)、cmath.sqrt(number)、float(object)、help()、input(prompt)、int(object)、long(object)、math.ceil(number)(返回上入整数)、math.floor(number)(返回下舍整

20、数)、pow(x,y)、raw_input(prompt)、repr(object)、str(object)、round(number.ndigits)python学习笔记(二)数据类型 python数据结构 学一门语言,最基础和重要的就是数据结构了,而在python中最基本的数据结构是序列,也可以理解为数组,但貌似比数组强大。 jason=jason,42 james=james,45 database=jason,james databasejason, 42, james, 45 索引: greeting=hello greeting0h greeting-1 =反着的时候从-1而不是0

21、开始开始o digital=raw_input (year:)3year:2013 digital3 索引示例: months=January,February,March,April, May,June,July,August,September,October November,December #根据指定的年月日以数字形式打印出日期 endings=st,nd,rd+17*th+st,nd,rd+7*th+st #以1-31的数字作为结尾的列表 year=raw_input (Year:)Year:2013 month=raw_input(Month(1-12):)Month(1-12)

22、:3 day=raw_input(Day(1-31):)Day(1-31):30 month_num=int(month) day_num=int(day) month_name=monthsmonth_num-1 =注意这里索引要减1 ordinal=day+endingsday_num-1 print month_name + +ordinal + , + yearMarch 30th, 2013 分片: 使用索引能访问单个元素,使用分片能访问一定范围的元素,分片通过冒号相隔的两个索引来实现。 tag=Python web site tag9:30http:/www.python.org

23、tag32:-4Python web site numbers=1,2,3,4,5,6,7,8,9,10 numbers3:64, 5, 6 numbers-3:-18, 9 numbers-3:0 #分片中的最左边的索引比它右边索引的晚出现在序列中,结果就是一个空序列 numbers-3: #默认到最后8, 9, 10 numbers:3 #默认从第一个开始1, 2, 3 numbers: #默认全部1, 2, 3, 4, 5, 6, 7, 8, 9, 10 很显然,分片操作的实现需要提供两个索引作为边界,第一个索引的元素包含在分片内,而第二个不包含在分片内。 分片步长:默认步长没有写,是1

24、,分片格式:上边界:下边界:步长 numbers0:10:1 #默认步长是11, 2, 3, 4, 5, 6, 7, 8, 9, 10 numbers0:10:2 #步长设为21, 3, 5, 7, 9 numbers3:6:3 #步长设为34 numbers:4 1, 5, 9 numbers8:3:-1 #步长不能为0,因为不会向下执行,可以为负数,向前执行9, 8, 7, 6, 5 numbers10:0:-2 #当步长为负数时,开始索引必须大于结束索引10, 8, 6, 4, 2 numbers0:10:-2 numbers:-210, 8, 6, 4, 2 numbers 5:-26

25、, 4, 2 numbers:5:-210, 8 序列相加: 1,2,3+4,5,61, 2, 3, 4, 5, 6 1,2,3+world #列表和字符串都是序列,但是不能连在一起,两种同类型的序列才能进行连接操作Traceback (most recent call last): File , line 1, in 1,2,3+worldTypeError: can only concatenate list (not str) to list 序列乘法: python*5pythonpythonpythonpythonpython 25*1025, 25, 25, 25, 25, 25,

26、 25, 25, 25, 25 空列表可以简单的通过 表示,但若想要创建一个占用十个元素空间,却不包括任何有用的有用的内容列表。这时需要使用None,None是Python的内建值,初始化一个长度为10的列表如下: sequence=None*10 sequenceNone, None, None, None, None, None, None, None, None, None 序列乘法示例:(存在脚本中运行)sentence=raw_input (Sentence:)screen_width=60text_width=len(sentence)box_width=text_width+6l

27、eft_margin=(screen_width-box_width)/2printprint * left_margin + + + - * (box_width-2) + +print * left_margin + | + * text_width + |print * left_margin + | + sentence + |print * left_margin + | + * text_width + |print * left_margin + + + - * (box_width-2) + +printraw_input()结果如下: in运算符:检查一个值是否在序列中 permission=rwx #有木有觉得这个像判断Linux中某个文件的执行权限,确实可以这么判断

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1