background preloader

Python Learning

Facebook Twitter

Python字符串操作 [Python俱乐部] 使用NotePad++编辑python代码_简简单单. 以前用过emacs编辑过python代码,虽然那个Tab一键完成比较爽,但是emacs配置太麻烦(windows下),而且还会产生.file#之类的文件,看着就心烦!

使用NotePad++编辑python代码_简简单单

Python异常处理_aimee. Python的异常处理能力是很强大的,可向用户准确反馈出错信息。

Python异常处理_aimee

在Python中,异常也是对象,可对它进行操作。 所有异常都是基类Exception的成员。 所有异常都从基类Exception继承,而且都在exceptions模块中定义。 Python中的异常 - 如果你想看看某人的灵魂,只要问问他做了什么梦就行了. 当你的程序中出现异常情况时就需要异常处理。

python中的异常 - 如果你想看看某人的灵魂,只要问问他做了什么梦就行了

比如当你打开一个不存在的文件时。 当你的程序中有一些无效的语句时,Python会提示你有错误存在。 下面是一个拼写错误的例子,print写成了Print。 Python是大小写敏感的,因此Python将引发一个错误: >>> Print 'Hello World' File "", line 1 Print 'Hello World' ^ SyntaxError: invalid syntax >>> print 'Hello World' Hello World 1、try...except语句 try...except语句可以用于捕捉并处理错误。 #! 运行输出如下: $ python try_except.py Enter something --> Why did you do an EOF on me? 说明:每个try语句都必须有至少一个except语句。 2、引发异常 你可以用raise语句来引发一个异常。 #! Python 列表 字符串 之间的转换_醋根. Understanding Python's "with" statement. Fredrik Lundh | October 2006 | Originally posted to online.effbot.org Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers.

Understanding Python's "with" statement

As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource.

If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse. Def controlled_execution(callback): set things up try: callback(thing) finally: tear things down def my_function(thing): do something controlled_execution(my_function) This wasn’t very difficult, was it? Python异常处理体系简介(1)

Python转义字符 - AllenW. Notepad++编辑Pyhton文件的自动缩进的问题(图文)(转) - ksharp_dabu的日志. Ps:notepad++还有很多功能不知道,自己也没安装什么插件,以后看来要和它多打交道了。

Notepad++编辑Pyhton文件的自动缩进的问题(图文)(转) - ksharp_dabu的日志

原文链接: 这个问题一直困扰我很久,Python对缩进很敏感,一般建议缩进用空格,而Notepad++的自动缩进是用的TAB,google过,baidu过, 都提到在首选项中有个将TAB用4个空格代替的选项,可我一直找不到这个选项,经过N个版本更新后依然如初,甚至还下载过一些插件希望能解决,但无果。 今天终于在帮助文档中找到答案了(HELP很重要啊!!!) ,特记录之,免日久又忘掉。 如果你想打开自动缩进,可以在 设置-》首选项-》其他 中进行设置 勾选了这个后,你换行是就会自动缩进了,下面还要设置将TAB更换成4个空格 设置-》首选项-》语言-》标签设置 不要改“Default”,现在做得很灵活了,可以对不同的语言进行设置,我们可以选上Pyhton,将默认去掉,选“以空格取代” Python:单引号,双引号和三引号的区别 - ld_flex的专栏. Python数组的使用 - 狗死灯火阑珊处的日志. Python字符串搜索并返回子字符串位置 - 代码 - BugZone. Python 正则表达式 反斜杠(/)的麻烦和陷阱_正则表达式_脚本之家. Python正则表达式指南 - AstralWind. 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例。

Python正则表达式指南 - AstralWind

本文的内容不包括如何编写高效的正则表达式、如何优化正则表达式,这些主题请查看其他教程。 注意:本文基于Python2.4完成;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 The Python Standard Library. While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python.

The Python Standard Library

It also describes some of the optional components that are commonly included in Python distributions. Python’s standard library is very extensive, offering a wide range of facilities as indicated by the long table of contents listed below. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.

Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs. Instant Python. This is a minimal crash-course in the programming language Python.

Instant Python

To learn more, take a look at the documentation at the Python web site, www.python.org; especially the tutorial. If you wonder why you should be interested, check out the comparison page where Python is compared to other languages. This introduction has been translated into several languages, among them Portuguese, Italian, Spanish, Russian, French, Lithuanian, Japanese, German and Greek, and is currently being translated into Norwegian, Polish, and Korean. Since this document still might undergo changes, these translations may not always be up to date.

Note: To get the examples working properly, write the programs in a text file and then run that with the interpreter; do not try to run them directly in the interactive interpreter - not all of them will work. 简明 Python 教程. Python入门教程 超详细1小时学会Python_python_脚本之家. 本文适合有经验的程序员尽快进入Python世界.特别地,如果你掌握Java和Javascript,不用1小时你就可以用Python快速流畅地写有用的Python程序.

Python入门教程 超详细1小时学会Python_python_脚本之家

为什么使用Python 假设我们有这么一项任务:简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200. 思路:用shell编程. (Linux通常是bash而Windows是批处理脚本).例如,在Windows上用ping ip 的命令依次测试各个机器并得到控制台输出.由于ping通的时候控制台文本通常是"Reply from ... " 而不通的时候文本是"time out ... " ,所以,在结果中进行字符串查找,即可知道该机器是否连通. 实现:Java代码如下: String cmd="cmd.exe ping ";String ipprefix="192.168.10. " for(int i=begin;i<end;i++){ p= Runtime.getRuntime().exec(cmd+i); String line = null; BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); while((line = reader.readLine()) ! 这段代码运行得很好,问题是为了运行这段代码,你还需要做一些额外的工作.这些额外的工作包括:编写一个类文件 编写一个main方法 将之编译成字节代码 由于字节代码不能直接运行,你需要再写个小小的bat或者bash脚本来运行.