Skip to content

Instantly share code, notes, and snippets.

@cuckookernel
Last active March 20, 2023 04:02
Show Gist options
  • Save cuckookernel/9777067 to your computer and use it in GitHub Desktop.
Save cuckookernel/9777067 to your computer and use it in GitHub Desktop.
Python to Julia Quick translation / conversion reference Guide

A quick and dirty syntax translation guide to ease the transition between Python and Julia.

Some important differences

  • Arrays in Julia are indexed starting from 1.
  • In Julia class (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 ).

Core

Python Julia
True true
False false
None nothing
type( obj ) typeof( obj )
{} Dict{KeyType,ValueType}()
elif elseif

Basic String operations

Python Julia
str1 + str2 string( str1, str2 )
len( str1 ) length( str1 )
str1.rstrip() rstrip( str1 )

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 )
@schneiderfelipe
Copy link

Now there is a startswith function in Base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment