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 ;-).
IntegratingPythonWithOtherLanguages [Hint: The idea is to create pages for the stuff, not just link it.] There a various tools which make it easier to bridge the gap between Python and C/C++: Pyrex - write your extension module on Python Cython -- Cython -- an improved version of Pyrex CXX - PyCXX - helper lib for writing Python extensions in C++ SCXX ctypes is a Python module allowing to create and manipulate C data types in Python. These can then be passed to C-functions loaded from dynamic link libraries. elmer - compile and run python code from C, as if it was written in C PicklingTools is a collection of libraries for exchanging Python Dictionaries between C++ and Python. weave - include C code lines in Python program ackward exposes parts of Python's standard library as idiomatic C++ C/C++ Binding Generators Tools to make C/C++ functions/methods accessible from Python by generating binding (Python extension or module) from header files. Articles Related See also to name a few.
Python - Quick Guide Python is a high-level, interpreted, interactive and object oriented-scripting language. Python is InterpretedPython is InteractivePython is Object-OrientedPython is Beginner's Language Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Python's feature highlights include: Easy-to-learnEasy-to-readEasy-to-maintainA broad standard libraryInteractive ModePortableExtendableDatabasesGUI ProgrammingScalable The most up-to-date and current source code, binaries, documentation, news, etc. is available at the official website of Python: Python Official Website : You can download the Python documentation from the following site. Python Documentation Website : www.python.org/doc/ Interactive Mode Programming: Invoking the interpreter without passing a script file as a parameter brings up the following prompt: >>> print "Hello, Python!" Hello, Python! #! Example: #! #!
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
Templating Templating, and in particular web templating is a way to represent data in different forms. These forms often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) and data. Template usually looks much like the final output, with placeholders instead of actual data (or example data in simplified form), bears common style and visual elements. Templating Engines There are many, many different HTML/XML templating packages and modules for Python that provide different feature sets and syntaxes. The number of templating engines is so great because the mechanisms involved are pretty easy to write in Python, at least for a fairly basic template engine; this recipe from the Python Cookbook shows how easy it is. Engines using Value Substitution The simplest form of templating engine is that which merely substitutes values into a template in order to produce the final output. HTML Shorthand Processors
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 hello.py 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
The Python Tutorial Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation. The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). This tutorial introduces the reader informally to the basic concepts and features of the Python language and system.
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)
Welcome to PyBrain’s documentation! — PyBrain v0.3 documentation The documentation is build up in the following parts: first, there is the quickstart tutorial which aims at getting you started with PyBrain as quickly as possible. This is the right place for you if you just want get a feel for the library or if you never used PyBrain before. Although the quickstart uses supervised learning with neural networks as an example, this does not mean that that’s it. PyBrain is not only about supervised learning and neural networks. While the quickstart should be read sequentially, the tutorial chapters can mostly be read independently of each other. In case this does not suffice, we also have an API reference, the Module Index. If you want to develop for PyBrain and contribute, check out our guidelines in our wiki: If at any point the documentation does not suffice, you can always get help at the pybrain Google Group at Installation Quick answer:
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 pat.search(line): print 'matched:', line else: print 'no match:', line Comments: We import module re in order to use regular expresions.re.compile() compiles a regular expression so that we can reuse the compiled regular expression without compiling it repeatedly. 2.2.3 Using regular expressions Notes:
Principal Component Analysis step by step In this article I want to explain how a Principal Component Analysis (PCA) works by implementing it in Python step by step. At the end we will compare the results to the more convenient Python PCA()classes that are available through the popular matplotlib and scipy libraries and discuss how they differ. The main purposes of a principal component analysis are the analysis of data to identify patterns and finding patterns to reduce the dimensions of the dataset with minimal loss of information. Here, our desired outcome of the principal component analysis is to project a feature space (our dataset consisting of n x d-dimensional samples) onto a smaller subspace that represents our data "well". About the notation: In the following sections, we will use a bold-face and lower-case letters for denoting column vectors vectors (e.g., e) and bold-face upper-case letters for matrices (e.g., W) Principal Component Analysis (PCA) Vs. What is a "good" subspace? Summarizing the PCA approach
Non-Programmer's Tutorial for Python 3 Authors Contributors to this book Front matter Initial remarks Intro Installing and using Python – where to get help Hello, World The famous first program – screen output – variables – numbers and calculations Who Goes There? Interactive input – strings Count to 10 while loops Decisions if statements Debugging Finding out what goes wrong Defining Functions Structuring programs with the use of functions Advanced Functions Example (Almost) mind-blowing example of how programmers can think Lists Variables containing more than one value For Loops A second kind of loop Boolean Expressions Computer logic – True and False – and and or – not Dictionaries Variables containing key/value pairs Using Modules Extensions to the standard set of functionality More on Lists Using elements or parts of lists Revenge of the Strings More advanced text manipulations File IO Reading from files and writing to files Dealing with the imperfect How to handle errors Recursion Recursive Functions Intro to Object Oriented Programming in Python 3 The End