from http://docs.python.org/tutorial/introduction.html
Interactive moed: python
quit()
help()
float(), int() and long() abs ()
In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
42.6 * 58
_ + 1
String literals can span multiple lines in several ways. Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:
If we make the string literal a “raw” string, however, the \n sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data. Thus, the example:
c = r"this \n will ignore, for now the \n"
print c
triple quotes allow continued lines and will retain formating with carriage returns """ """ or ''' '''
Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> word = 'Help' + 'A' >>> word 'HelpA' >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>'
Strings can be subscripted (indexed); like in C, the first character of a string has subscript (index) 0. There is no separate character type; a character is simply a string of size one. Like in Icon, substrings can be specified with the slice notation: two indices separated by a colon.
word = "house" word[4] word[1:2]
Unlike a C string, Python strings cannot be changed. Assigning to an indexed position in the string results in an error:
However, creating a new string with the combined content is easy and efficient: 'x' + word[2:]
>>> 'x' + word[1:] 'xelpA' >>> 'Splat' + word[4] 'SplatA'
Here’s a useful invariant of slice operations: s[:i] + s[i:] equals s.
>>> word[:2] + word[2:] 'HelpA' >>> word[:3] + word[3:] 'HelpA'
Indices may be negative numbers, to start counting from the right. For example:
len(string)
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.
>>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234]
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
>>> a[0] 'spam' >>> a[3] 1234 >>> a[-2] 100 >>> a[1:-1] ['eggs', 100] >>> a[:2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] >>> 3*a[:3] + ['Boo!'] ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
Unlike strings, which are immutable, it is possible to change individual elements of a list:
>>> a ['spam', 'eggs', 100, 1234] >>> a[2] = a[2] + 23 >>> a ['spam', 'eggs', 123, 1234]
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:
>>> # Replace some items: ... a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] >>> # Remove some: ... a[0:2] = [] >>> a [123, 1234] >>> # Insert some: ... a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234] >>> # Insert (a copy of) itself at the beginning >>> a[:0] = a >>> a [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] >>> # Clear the list: replace all items with an empty list >>> a[:] = [] >>> a []
The built-in function len() also applies to lists:
>>> a = ['a', 'b', 'c', 'd'] >>> len(a) 4
It is possible to nest lists (create lists containing other lists), for example:
>>> q = [2, 3] >>> p = [1, q, 4] >>> len(p) 3 >>> p[1] [2, 3] >>> p[1][0] 2 >>> p[1].append('xtra') # See section 5.1 >>> p [1, [2, 3, 'xtra'], 4] >>> q [2, 3, 'xtra']
Note that in the last example, p[1] and q really refer to the same object! We’ll come back to object semantics later.