A quick and dirty syntax translation / conversion reference guide to ease the transition between Python and Julia. This is not meant as a reference to the language. For that you should [read the manual](http://julia.readthedocs.org/en/latest/manual/). ## Some important differences * Arrays in Julia are indexed starting from 1. * In Julia classes (i.e. types) don't own methods. Methods are implementations of generic functions and are invoked in a "static style", i.e. instead of Python's str1.rstrip(), we will have rstrip( str1 ), instead of file1.close(), close( file1 ). * ## Some important similarities. ## Core | *Python* | *Julia* | *Comments* | | -------------------------|---------|----------| | `True` | `true` | | | `False` | `false` | | | `None` | `nothing` | | | `type( obj )` | `typeof( obj )` | | | `{}` | `Dict{KeyType,ValueType}()` | | | `elif` | `elseif` | | | `lambda x, y : y + x * 2` | `(x,y) -> y + x * 2` | | | `"string %s interpolation %d" % ( str1, i1)` | `"string $str1 interpolation $i1 "` | You can interpolate arbitrary expressions by enclosing them in braces, as in `"${x+y}"` | | `xrange(10,4,-2`)` | `10:-2:4` | | | `range(10,4,-2)` | `[10:-2:4]` | Do this only if you really have to, as it will consume memory proportional to the length of the range | ## Basic String operations | *Python* | *Julia* | | -------------------------|-----------| | str1 + str2 | string( str1, str2 ) | | len( str1 ) | length( str1 ) | | str1.rstrip() | rstrip( str1 ) | | str1.startswith( x ) | ??? write your own such as the one in [pytojul.jl](https://gist.github.com/cuckookernel/9777067#file-pytojul-jl) | ## Regular expressions | *Python* | *Julia* | |----------|---------| | m = re.match( r"(\d+):(\d+)", mystr ) | m = match( r"(\d+):(\d+)", mystr ) | | m is not None | m != nothing | | arr = m.groups() | arr = m.captures | ## File processing | *Python* | *Julia* | |----------|---------| | f = open( "file.txt" ) | f = open( "file.txt") | | for line in f | for line in eachline( f ) | | f.close() | close( f ) |