background preloader

Python

Facebook Twitter

Python. Python Programming. CodingBat. 6. Built-in Exceptions — Python v3.1.3 documentation. In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an “associated value” indicating the detailed cause of the error.

This may be a string or a tuple containing several items of information (e.g., an error code and a string explaining the code). The associated value is usually passed to the exception class’s constructor. If the exception class is derived from the standard root class BaseException, the associated value is present as the exception instance’s args attribute. User code can raise built-in exceptions. Secant.cs.purdue.edu/_media/proghints.pdf. PEP 8 -- Style Guide for Python Code. Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such).For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting.

In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.Comparisons to singletons like None should always be done with is or is not, never the equality operators.Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context! Www.greenteapress.com/thinkpython/thinkCSpy/thinkCSpy.pdf. Overview — Python v2.7.2 documentation. Python.rhino3d.com. 3. An Informal Introduction to Python — Python v2.7.2 documentation. In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and …): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter.

Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command. Many of the examples in this manual, even those entered at the interactive prompt, include comments. Comments in Python start with the hash character, #, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. Let’s try some simple Python commands. 3.1.1. The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. The integer numbers (e.g. 2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float. 3.1.2.