background preloader

Hands-On Python A Tutorial Introduction for Beginners

Hands-On Python A Tutorial Introduction for Beginners
Hands-On Python A Tutorial Introduction for Beginners Contents Chapter 1 Beginning With Python 1.1. You have probably used computers to do all sorts of useful and interesting things. 1.1.1. First let us place Python programming in the context of the computer hardware. z = x+y is an instruction in many high-level languages that means something like: Access the value stored at a location labeled x Calculate the sum of this value and the value stored at a location labeled y Store the result in a location labeled z. No computer understands the high-level instruction directly; it is not in machine language. Obviously high-level languages were a great advance in clarity! If you follow a broad introduction to computing, you will learn more about the layers that connect low-level digital computer circuits to high-level languages. 1.1.2. There are many high-level languages. 1.1.3. If you are not sure whether your computer already has Python, continue to Section 1.2.2 , and give it a try. Windows Linux 1.2.

Code Like a Pythonista: Idiomatic Python In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt. There are 3 versions of this presentation: ©2006-2008, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license. My credentials: I am a resident of Montreal,father of two great kids, husband of one special woman,a full-time Python programmer,author of the Docutils project and reStructuredText,an editor of the Python Enhancement Proposals (or PEPs),an organizer of PyCon 2007, and chair of PyCon 2008,a member of the Python Software Foundation,a Director of the Foundation for the past year, and its Secretary. In the tutorial I presented at PyCon 2006 (called Text & Data Processing), I was surprised at the reaction to some techniques I used that I had thought were common knowledge. Many of you will have seen some of these techniques and idioms before. These are the guiding principles of Python, but are open to interpretation. import this

labs :: Python beginner's mistakes Every Python programmer had to learn the language at one time, and started out as a beginner. Beginners make mistakes. This article highlights a few common mistakes, including some I made myself. Beginner's mistakes are not Python's fault, nor the beginner's. They're merely a result of misunderstanding the language. To put it another way, the mistakes in this article are often cases of "the wrong tool for the job", rather than coding errors or sneaky language traps. Mistake 1: trying to do low-level operations Python is sometimes described as a VHLL, a Very High-Level Language. This doesn't mean that it isn't possible to do these things with Python; but it's probably just not the right language for these jobs. Mistake 2: writing "language X" code in Python This is a mistake that is almost unavoidable. Some notorious symptoms of "language X" code, and the languages that may cause them: The point here is not to slam the language that you're used to (although that is always fun ;-).

For Beginners Welcome! Are you completely new to programming? If not then we presume you will be looking for information about why and how to get started with Python. Fortunately an experienced programmer in any programming language (whatever it may be) can pick up Python very quickly. It's also easy for beginners to use and learn, so jump in! Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. If you have a question, it's a good idea to try the FAQ, which answers the most commonly asked questions about Python. If you want to help to develop Python, take a look at the developer area for further information.

使用python抓取网页(以人人网新鲜事和团购网信息为例) 前一段时间写的小东西,一直没工夫把他系统写出来,今天眼睛疼,就写写吧~~(原来博主不蛋疼时也会更新博客的哈~) python抓取网页基础 python自己带有很多网络应用相关的模块,如:ftplib用于FTP相关操作,smtplib和poplib用于收发电子邮件等等,利用这些 模块自己写一个FTP软件或是邮件客户端类软件完全是可能的,我就简单的试过完全用python脚本收发邮件和操作自己的FTP服务器。当然,这都不是今 天的主角,我们今天要用到的几个模块是:urllib,urllib2,cookielib,BeautifulSoup,我们先来简单介绍下。 urllib和urllib2自然都是处理URL相关的操作,urllib可以从指定的URL下载文件,或是对一些字符串进行编码解码以使他们成为特定的 URL串,而urllib2则比urllib更2一点,哦不对,是更牛逼一点。 我们先来看看最简单的网页抓取,其实网页抓取就是将所要的网页源代码文件下载下来,然后对其分析以提取对自己有用的信息。 import urllibhtml_src = urllib.urlopen(' 这样就会打印出百度首页的HTML源码了,还是很easy的。 from BeautifulSoup import BeautifulSoupparser = BeautifulSoup(html_src) 这样,后续处理HTML源码的工作交给parser变量来负责就好,我们可以简单的调用parser的prettify函数来相对美观的显示源码, 可以看到这样就能看到中文字符了,因为BeautifulSoup能自动处理字符问题,并将返回结果都转化为Unicode编码格式。 抓取人人网的新鲜事 前面讲的是最简单的抓取情形了,但通常我们需要面对更复杂的情形,拿人人网来说,需要登录自己的账号才能显示新鲜事,这样我们就只能求助于更2一点 的urllib2模块了。 首先import我们需要的所有模块,然后使用urllib2模块的HTTPCookieProcessor搭建一个处理cookie的 Handler,传入cookielib模块的CookieJar函数作为参数,这个这个函数处理HTTP的cookie,简单地说,它从HTTP请求中 提取cookie,然后将其返回给HTTP响应。

labs :: 10 Python pitfalls (or however many I'll find ;-) These are not necessarily warts or flaws; rather, they are (side effects of) language features that often trip up newbies, and sometimes experienced programmers. Incomplete understanding of some core Python behavior may cause people to get bitten by these. This document is meant as some sort of guideline to those who are new to Python. It's better to learn about the pitfalls early, than to encounter them in production code shortly before a deadline. :-} It is *not* meant to criticize the language; as said, most of these pitfalls are not due to language flaws. 1. OK, this is a cheesy one to start with. Solution: Indent consistently. 2. People coming from statically typed languages like Pascal and C often assume that Python variables and assignment work the same as in their language of choice. a = b = 3 a = 4 print a, b # 4, 3 However, then they run into trouble when using mutable objects. a = [1, 2, 3] b = a a.append(4) print b # b is now [1, 2, 3, 4] as well

Eric Walstad's crew quarters on the Starship Hello World! I'm busilly tuning the hyper drive right now. Please have a seat and enjoy my spartan quarters or have a look at my business site: or the Django Critter that helps me write code at the speed of light. links I've found useful over the years of Python programmingPythonDive Into PythonCode Like a Pythonista: Idiomatic PythonBitManipulation - PythonInfo WikiCharming Python: Using state machinesCSV module ExamplesDebugging python c extensionsEpydocGnuplot.pyPython bindings to the Xapian search enginePython SidebarPython Standard LoggingSending email with PythonSingleton Mixin - The Borg patternThe Epytext Markup LanguageUsing Mix-ins with Python | Linux JournalXapian: DocumentationLearning to programByte of Python:Main Page - Text

Python Introduction - Google's Python Class - Google Code Python is a dynamic, interpreted language. Source code does not declare the types of variables or parameters or methods. This makes the code short and flexible, and you lose the compile-time type checking in the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs. (todo: link here to the companion video segment for this section) An excellent way to see how Python code works is to run the Python interpreter and type code right into it. Python Program Python source files use the ".py" extension. Here's a very simple Python program (notice that blocks of code are delimited strictly using indentation rather than curly braces -- more on this later!) #! # import modules used here -- sys is a very standard oneimport sys # Gather our code in a main() functiondef main(): print 'Hello there', sys.argv[1] # Command line args are in sys.argv[1], sys.argv[2] ... # sys.argv[0] is the script name itself and can be ignored Python Module

LIBSVM Tools This page provides some miscellaneous tools based on LIBSVM (and LIBLINEAR). Roughly they include Disclaimer: We do not take any responsibility on damage or other problems caused by using these software and data sets. Please download the zip file. Please download the zip file. Please download the zip file. T. You can use either MATLAB or Python. Python In One Easy Lesson Nick Parlante Nov 2010 This is a one-hour introduction to Python used for Stanford's CS107. This material should work as an introduction for any experienced programmer. Python is a popular open source, cross-platform language in the "dynamic language" niche, like Javascript, Ruby, Lisp, and Perl. Unlike C/C++, Python defers almost everything until runtime. This is how Python works, in contrast to the C/C++ style of knowing the type of every variable and using that for compile time code generation. x = x + x In Python, x could be an int, or a string, or who knows what. Interpreter and Variables You can run the Python interpreter and type code directly in to it -- a good way to try little experiments. Strings Strings are delimited with ' or " or """, and are immutable. >>> a = 'hello' >>> len(a) 5 >>> a[0] 'h' >>> a + '!!!' Lists [ ], For Loop List literals are enclosed in square brackets. Program syntax, if/else, Indentation #! Dict { } Hash Table Sorting Custom sorting Lambda (optional)

Python for Fun This collection is a presentation of several small Python programs. They are aimed at intermediate programmers; people who have studied Python and are fairly comfortable with basic recursion and object oriented techniques. Each program is very short, never more than a couple of pages and accompanied with a write-up. I have found Python to be an excellent language to express algorithms clearly. From many years of programming these are some of my favorite programs. Many thanks to Paul Carduner and Jeff Elkner for their work on this page, especially for Paul's graphic of Psyltherin (apologies to Harry Potter) and to the teams behind reStructured text and Sphinx to which the web pages in this collection have been adapted. Chris Meyers

A Python Book: Beginning Python, Advanced Python, and Python Exercises 2.2 Regular Expressions For more help on regular expressions, see: 2.2.1 Defining regular expressions A regular expression pattern is a sequence of characters that will match sequences of characters in a target. The patterns or regular expressions can be defined as follows: Literal characters must match exactly. Because of the use of backslashes in patterns, you are usually better off defining regular expressions with raw strings, e.g. r"abc". 2.2.2 Compiling regular expressions When a regular expression is to be used more than once, you should consider compiling it. import sys, re pat = re.compile('aa[bc]*dd') while 1: line = raw_input('Enter a line ("q" to quit):') if line == 'q': break if print 'matched:', line else: print 'no match:', line Comments: We import module re in order to use regular compiles a regular expression so that we can reuse the compiled regular expression without compiling it repeatedly. 2.2.3 Using regular expressions Notes:

Bruce Eckel's MindView, Inc: Thinking in Python You can download the current version of Thinking in Python here. This includes the BackTalk comment collection system that I built in Zope. The page describing this project is here. The current version of the book is 0.1. The source code is in the download package. This is not an introductory Python book. However, Learning Python is not exactly a beginning programmer's book, either (although it's possible if you're dedicated). Revision History Revision 0.1.2, December 31 2001.