Python

Python is a scripting language.

There are two versions, 2.x and 3.x

1. main

Use the __name__ parameter to determine if you are calling your script from the command line.

#!/usr/bin/python

__author__ = 'magicmonster'

def main():
    print __name__

if __name__ == "__main__":
    main()

2. Modules

import

Before using a module you must import it. e.g. if you want to use the sys module, add the following to the top of your python script file.

import sys

3. args

Command line arguments are accessible via sys.argv. As with C, argv[0] contains your script file.

4. Strings

Join

You can join a list of strings to make one string.

long_string = ",".join(["apple","banana","carrot"])

Casting

To convert an int into a string, use str()

String value present in a set

Use the in operator to test whether a value is present in a set.

>>> 'apple' in ('apple','pear','banana')
        True
      

trim

Use strip() instead. e.g. to trim a trailing slash, use rstrip()

>>> "dir/".rstrip("/")
        'dir'

5. Built-in functions

len

length

6. Classes

It is possible to define a class that holds state.