Python is a modern, general-purpose, object-oriented, high-level programming language.
IPython notebook is an HTML-based notebook environment for Python, similar to Mathematica or Maple. It is based on the IPython shell, but provides a cell-based environment with great interactivity, where calculations can be organized and documented in a structured way.
Everything about string formatting:
a Python string is immutable. In other words, it is a constant which cannot be changed in place.
All string operations create a new string.
This is strange for a C developer, but it is necessary for some properties of the language. In most cases this is not a problem.
A tuple is similar to a list but it is a fixed-size, immutable array. This means that once a tuple has been created, its elements may not be changed, removed, appended or inserted.
It is declared using parentheses and comma-separated values:
A bit more about tuples: http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-...
Blocks of code are delimited using indentation, either spaces or tabs at the beginning of lines. This is one of the main differences of Python over other languages, and that is the main reason why people love it or hate it. ;-)
a=1 while a<10: print a a += 1
Functions are first-class citizen
In Python we can also create unnamed functions, using the
Classes are the key features of object-oriented programming. A class is a structure for representing an object and the operations that can be performed on the object.
In Python a class can contain attributes (variables) and methods (functions).
A class is defined almost like a function, but using the
class keyword, and the class definition usually contains a number of class method definitions (a function in a class).
Each class method should have an argument
self as its first argument. This object is a self-reference.
Some class method names have special meaning, for example:
__init__: The name of the method that is invoked when the object is first created.
__str__: A method that is invoked when a simple string representation of the class is needed, as for example when printed.
Note that calling class methods can modifiy the state of that particular class instance, but does not effect other class instances or any global variables.
That is one of the nice things about object-oriented design: code such as functions and related variables are grouped in separate and independent entities.
In Python errors are managed with a special language construct called "Exceptions". When errors occur exceptions can be raised, which interrupts the normal program flow and fallback to somewhere else in the code where the closest try-except statement is defined.
To generate an exception we can use the
raise statement, which takes an argument that must be an instance of the class
BaseException or a class derived from it.
A typical use of exceptions is to abort functions when some error condition occurs, for example:
1 2 3 4 5 6
def my_function(arguments): if not verify(arguments): raise Exception("Invalid arguments") # rest of the code goes here
To gracefully catch errors that are generated by functions and class methods, or by the Python interpreter itself, use the
1 2 3 4 5 6
try: # normal code goes here except: # code for error handling goes here # this code is not executed unless the code # above generated an error
To get information about the error, we can access the
Exception class instance that describes the exception by using for example:
except Exception as e:
One of the most important concepts in good programming is to reuse code and avoid repetitions.
The idea is to write functions and classes with a well-defined purpose and scope, and reuse these instead of repeating similar code in different part of a program (modular programming). The result is usually that readability and maintainability of a program is greatly improved. What this means in practice is that our programs have fewer bugs, are easier to extend and debug/troubleshoot.
Python supports modular programming at different levels. Functions and classes are examples of tools for low-level modular programming. Python modules are a higher-level modular programming construct, where we can collect related variables, functions and classes in a module. A python module is defined in a python file (with file-ending
.py), and it can be made accessible to other Python modules and programs using the
Consider the following example: the file
mymodule.py contains simple example implementations of a variable, function and a class: