background preloader

Python

Facebook Twitter

2. Built-in Functions. Open a file, returning an object of the file type described in section File Objects.

2. Built-in Functions

If the file cannot be opened, IOError is raised. When opening a file, it’s preferable to use open() instead of invoking the file constructor directly. The first two arguments are the same as for stdio‘s fopen(): name is the file name to be opened, and mode is a string indicating how the file is to be opened. The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). In addition to the standard fopen() values mode may be 'U' or 'rU'.

Termination - Terminating a Python script. Introduction to Python: Class 5. Page Contents A Python class is created by a class definition, has an associated name space, supports attribute reference, and is callable. class name[(expr[,expr]*)]: suite The class definition is an executable statement and as such can be used whereever an executable statement may occur.

Introduction to Python: Class 5

When executed, each expr is evaluated and must evaluate to a class; then the suite is executed in a new local name space: the assumption is that the statements in the suite will make bindings in this name space, so typically the statements in the suite are assignments and function definitions. After execution of the suite, name is bound to the new class in the outer (calling) name space, and the new name space of the class definition is associated with the class object. Classes have five predefined attributes: The simplest use of classes is as simple Cartesian product types, e.g., the records of Pascal or the structs of C.

Mappings and Dictionaries — Building Skills in Python. Many algorithms need to map a key to a data value.

Mappings and Dictionaries — Building Skills in Python

This kind of mapping is supported by the Python dictionary, dict. We’ll look at dictionaries from a number of viewpoints: semantics, literal values, operations, comparison operators, statements, built-in functions and methods. We are then in a position to look at two applications of the dictionary. We’ll look at how Python uses dictionaries along with sequences to handle arbitrary connections of parameters to functions in Advanced Parameter Handling For Functions. This is a very sophisticated set of tools that let us define functions that are very flexible and easy to use.

Debugging in Python. As a programmer, one of the first things that you need for serious program development is a debugger.

Debugging in Python

Python has a debugger, which is available as a module called pdb (for “Python DeBugger”, naturally!). Unfortunately, most discussions of pdb are not very useful to a Python newbie — most are very terse and simply rehash the description of pdb in the Python library reference manual. The discussion that I have found most accessible is in the first four pages of Chapter 27 of the Python 2.1 Bible. So here is my own personal gentle introduction to using pdb. It assumes that you are not using any IDE — that you’re coding Python with a text editor and running your Python programs from the command line. For information on the IDLE interactive debugger, see the IDLE documentationFor information on the Wing IDE debugger, see the Wing IDE documentation.

To start, I’ll show you the very simplest way to use the Python debugger. Dive Into Python. 8.5. locals and globals. Let's digress from HTML processing for a minute and talk about how Python handles variables.

8.5. locals and globals

Python has two built-in functions, locals and globals, which provide dictionary-based access to local and global variables. Remember locals? You first saw it here: def unknown_starttag(self, tag, attrs): strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) No, wait, you can't learn about locals yet. Contents. 3 Python Data Structures. Subsections Python has a few, very useful, built-in data structures that you will see used everywhere. 3.1 Strings The simplest and most familiar data structure is a string.

3 Python Data Structures

String constants can be written with either single or double quotes. Lesson 11 - Exception Handling. Introduction If you haven't seen them before, you're not trying hard enough.

Lesson 11 - Exception Handling

What are they? Errors. Exceptions. Problems. Code Example 1 - buggy program def menu(list, question): for entry in list: print 1 + list.index(entry), print ") " + entry return input(question) - 1 # running the function # remember what the backslash does answer = menu(['A','B','C','D','E','F','H','I'],\ 'Which letter is your favourite? This is just an example of the menu program we made earlier.

Bugs - Human Errors The most common problems with your code are of your own doing. Code Example 2 - error message. Lesson 6 - Tuples, Lists, and Dictionaries. Introduction.

Lesson 6 - Tuples, Lists, and Dictionaries

Building Skills in Python — Building Skills in Python. A Programmer’s Introduction to Python Legal Notice This work is licensed under a Creative Commons License.

Building Skills in Python — Building Skills in Python

Building Skills in Object-Oriented Design — Building Skills in Object-Oriented Design. Step-by-Step Construction of A Complete Application Legal Notice This work is licensed under a Creative Commons License.

Building Skills in Object-Oriented Design — Building Skills in Object-Oriented Design

Switching to OS X, Obstacle 2: Switching between windows « Lennart Regebro: Python, Plone, Web. Obstacle 1: Where the heck is that button? Obstacle 2: Switching between windowsObstacle 3: Home/End buttons (Unsolved)Obstacle 4: Python and friends In OS X, Cmd-Tab switches between applications, and not between windows as under Windows or most Linuxes. That’s OK, in fact, I think I like it (I’m note quite sure yet). However, I of course also want to switch amongst the windows within an application, using the keyboard.

And here is where it gets tricky. I tried various ways of fixing this. I downloaded Ukulele, which is a cool app, and fiddled round with it. I found a script to help change the idiotic Mac behavior of Home+End of moving to the start end end of the document, to normal sane behaviour of moving to the start and end of the line. Python from Scratch: Getting Started. Welcome to Python from Scratch, where I'm going to teach you the ins and outs of Python development... from scratch. In this first lesson, we're going to choose a version, install Python, and then create the obligatory "Hello world" script. If you're already familiar with Python, feel free to skip ahead to a later lesson in the series.

Video Tutorial Companion Article.