background preloader

Instant Python

Instant Python
This is a minimal crash-course in the programming language 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. 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. To begin with, think of Python as pseudo-code. x,y,z = 1,2,3 first, second = second, first a = b = 123 Blocks are indicated through indentation, and only through indentation. The first two examples are equivalent. Notice that the end is non-inclusive. print a or b

Python入门教程 超详细1小时学会Python_python_脚本之家 本文适合有经验的程序员尽快进入Python世界.特别地,如果你掌握Java和Javascript,不用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脚本来运行. 同样的工作用Python实现如下: import subprocess cmd="cmd.exe"begin=101end=200while begin<end: p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) p.stdin.write("ping 192.168.1." p.stdin.close() p.wait() print "execution result: %s"%p.stdout.read()

BeginnersGuide/Programmers This is a Wiki page. Users with edit rights can edit it. You are, therefore, free to (in fact, encouraged to) add details of material that other Python users will find useful. A beginner-friendly Python tutorial that starts with the absolute basics but also covers more advanced stuff like Python software deployment. The Python Standard Library This document is for an old version of Python that is no longer supported. You should upgrade and read the Python documentation for the current stable release. Navigation 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. Python’s standard library is very extensive, offering a wide range of facilities as indicated by the long table of contents listed below. The Python installers for the Windows platform usually include the entire standard library and often also include many additional components. In addition to the standard library, there is a growing collection of several thousand components (from individual programs and modules to packages and entire application development frameworks), available from the Python Package Index. Previous topic 9. Next topic 1. This Page Show Source Quick search

Tutorial - py2exe.org py2exe turns Python programs into packages that can be run on other Windows computers without needing to install Python on those computers. You must run py2exe on a Windows computer. Python is needed on the computer where py2exe itself is run because py2exe is a Python program and it includes parts of Python in the package that is built. To successfully complete this tutorial you'll need to know the basics of Python (you can get started at python.org's getting started page). You'll also need to know how to run Python programs from the command prompt. Install py2exe on your Windows computer using pip: pip install py2exe There are a few simple steps needed to use py2exe once you've installed it: 1. The biggest step is almost always the first one. It's important that you make sure everything is working before you use py2exe. The first example we'll use here is our old friend... hello.py We need to make sure it's working... C:\Tutorial>python hello.py Hello World! Looks good! 2. setup.py 3. 4. 5.

Python正则表达式指南 - AstralWind 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例。本文的内容不包括如何编写高效的正则表达式、如何优化正则表达式,这些主题请查看其他教程。 注意:本文基于Python2.4完成;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 1. 1.1. 正则表达式并不是Python的一部分。 下图展示了使用正则表达式进行匹配的流程: 正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功;一旦有匹配不成功的字符则匹配失败。 下图列出了Python支持的正则表达式元字符和语法: 1.2. 正则表达式通常用于在文本中查找匹配的字符串。 1.3. 与大多数编程语言相同,正则表达式里使用"\"作为转义字符,这就可能造成反斜杠困扰。 1.4. 正则表达式提供了一些可用的匹配模式,比如忽略大小写、多行匹配等,这部分内容将在Pattern类的工厂方法re.compile(pattern[, flags])中一起介绍。 2. re模块 2.1. Python通过re模块提供对正则表达式的支持。 re.compile(strPattern[, flag]): 这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象。 re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同) M(MULTILINE): 多行模式,改变'^'和'$'的行为(参见上图) S(DOTALL): 点任意匹配模式,改变'.' re提供了众多模块方法用于完成正则表达式的功能。 re模块还提供了一个方法escape(string),用于将string中的正则表达式元字符如*/+/? 2.2. Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息。 属性: string: 匹配时使用的文本。 方法: group([group1, …]): 获得一个或多个分组截获的字符串;指定多个参数时将以元组形式返回。 2.3. Pattern对象是一个编译好的正则表达式,通过Pattern提供的一系列方法可以对文本进行匹配查找。 Pattern不能直接实例化,必须使用re.compile()进行构造。 实例方法[ | re模块方法]:

python - IRC bot functionalities Notepad++编辑Pyhton文件的自动缩进的问题(图文)(转) - ksharp_dabu的日志 Ps:notepad++还有很多功能不知道,自己也没安装什么插件,以后看来要和它多打交道了。 原文链接: 这个问题一直困扰我很久,Python对缩进很敏感,一般建议缩进用空格,而Notepad++的自动缩进是用的TAB,google过,baidu过, 都提到在首选项中有个将TAB用4个空格代替的选项,可我一直找不到这个选项,经过N个版本更新后依然如初,甚至还下载过一些插件希望能解决,但无果。 今天终于在帮助文档中找到答案了(HELP很重要啊!!!) 如果你想打开自动缩进,可以在 设置-》首选项-》其他 中进行设置 勾选了这个后,你换行是就会自动缩进了,下面还要设置将TAB更换成4个空格 设置-》首选项-》语言-》标签设置 不要改“Default”,现在做得很灵活了,可以对不同的语言进行设置,我们可以选上Pyhton,将默认去掉,选“以空格取代” 现在你的Notepad++可以很好的编辑Pyhton文件了 随便说说运行的设置,在运行(F5)中输入 cmd /k C:\Python26\python.exe "$(FULL_CURRENT_PATH)" & PAUSE & EXIT , 注意:上面的输入相当于在cmd运行python.exe的绝对路径,所以你要根据自己的python的路径修改就性了。 点击保存,自己起个名字,以后在“运行”菜单下就可以运行你的Python程序了

A simple IRC BOT written in Python - This article shows you how to make a simple IRC bot in python. First question: why the hell not eggdrop? Answer: I don't know TCL, and I like to customize my scripts; plus it's a heavy program, and I hardly need 1% of the features it provides. I have been using python for a few months now and absoulutely love it. Creating bots in python is quite a simple job really. Importing the libraries: import sys import socket import string import os #not necassary but later on I am going to use a few features from this With that done, now we need to give a configuration: HOST='mesa.az.us.undernet.org' #The server we want to connect to PORT=6667 #The connection port which is usually 6667 NICK='pybotv000' #The bot's nickname IDENT='pybot' REALNAME='s1ash' OWNER='ne0n-' #The bot owner's nick CHANNELINIT='#test198' #The default channel for the bot readbuffer='' #Here we store all the messages from server Warning:Keep the nickname quite unique because then you'll have to use a method to change the nick if it is already taken. Connecting to the server: while 1: :nick!

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. As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. 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) with open("x.txt") as f: data = f.read() do something with data

Simple Python IRC Bot #!/usr/bin/env python# This code was written for Python 3.1.1# version 0.101# Changelog:# version 0.100# Basic framework## version 0.101# Fixed an error if an admin used a command with an argument, that wasn't an admin-only commandimport socket, sys, threading, time# Hardcoding the root admin - it seems the best way for nowroot_admin = "maslen"# Defining a class to run the server. One per connection. 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 raising.py 请输入 --> 你输入了一个结束标记EOF $ python raising.py 请输入 --> --> ab ShortInputException: 输入的长度是 2, 长度至少应是 3 $ python raising.py 请输入 --> abc 没有异常发生. 3、try...finally语句 当你正在读文件或还未关闭文件时发生了异常该怎么办呢? #! $ python finally.py Programming is fun When the work is done Cleaning up...closed the file Traceback (most recent call last): File "finally.py", line 12, in ? 说明:我们在两秒这段时间内按下了Ctrl-c,这将产生一个KeyboardInterrupt异常,我们并没有处理这个异常,那么Python将调用默认的处理器,并终止程序,在程序终止之前,finally块中的语句将执行。 本系列的文章来源是

s Python Class - Google's Python Class - Google Code Welcome to Google's Python Class -- this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google to introduce Python to people who have just a little programming experience. The first exercises work on basic Python concepts like strings and lists, building up to the later exercises which are full programs dealing with text files, processes, and http connections. To get started, the Python sections are linked at the left -- Python Set Up to get Python installed on your machine, Python Introduction for an introduction to the language, and then Python Strings starts the coding material, leading to the first exercise. This material was created by Nick Parlante working in the engEDU group at Google. Tip: Check out the Python Google Code University Forum to ask and answer questions.

Python异常处理_aimee Python的异常处理能力是很强大的,可向用户准确反馈出错信息。在Python中,异常也是对象,可对它进行操作。所有异常都是基类Exception的成员。 方式一:try语句: 1使用try和except语句来捕获异常 try: block except [exception,[data…]]: block try: block except [exception,[data...]]: block else: block 该种异常处理语法的规则是: · 执行try下的语句,如果引发异常,则执行过程会跳到第一个except语句。 · 如果第一个except中定义的异常与引发的异常匹配,则执行该except中的语句。 · 如果引发的异常不匹配第一个except,则会搜索第二个except,允许编写的except数量没有限制。 · 如果所有的except都不匹配,则异常会传递到下一个调用本代码的最高层try代码中。 · 如果没有发生异常,则执行else块代码。 例: try: f = open(“file.txt”,”r”) except IOError, e: print e 捕获到的IOError错误的详细原因会被放置在对象e中,然后运行该异常的except代码块 捕获所有的异常 try: a=b b=c except Exception,ex: print Exception,":",ex 使用except子句需要注意的事情,就是多个except子句截获异常时,如果各个异常类之间具有继承关系,则子类应该写在前面,否则父类将会直接截获子类异常。 2 使用try跟finally: 语法如下: try: block finally: block 该语句的执行规则是: · 执行try下的代码。 · 如果发生异常,在该异常传递到下一级try时,执行finally中的代码。 · 如果没有发生异常,则执行finally中的代码。 第二种try语法在无论有没有发生异常都要执行代码的情况下是很有用的。 这两种形式相互冲突,使用了一种就不允许使用另一种,而功能又各异 2. raise [exception[,data]] 在Python中,要想引发异常,最简单的形式就是输入关键字raise,后跟要引发的异常的名称。 try: raise MyError #自己抛出一个异常 except MyError: print 'a error' 3.

Related: