Comments

Python Julia
Code
#This is a single line comment.
"""
This is a multiline comment.
Enclose comments between triple 
single quotes ''' ''' or 
double quotes """ """. 
""" 
#This is a single line comment.
#=
This is a multiline comment.
Enclose comments between "#=" and "=#". 
=#

Casting

Python Julia
Code
float(9)
convert(Float64,9)
Output 9.0 9.0

Null

Python Julia
Code
None
nothing

Tuples

Python Julia
Code
tup = (4.5,"python",7e-6,None)
tup
tup[2]
tup[:3]
tup = (4.5,"julia",7e-6,nothing)
tup
tup[2]
tup[:3]
Output
(4.5, 'python', 7e-06, None)
7e-06
(4.5, 'python', 7e-06)
(4.5, "julia", 7.0e-6, nothing)
"julia"
7.0e-6

Functions

Function

Python Julia
Code
def f(x,y): 
    return x+y
def g(x,y):
    return x**3%y
f(-1,2)
g(7,3)
f(x,y) = x+y
function g(x,y)
    return x^3%y
end
f(-1,2)
g(7,3)
Output
1
1
1
1

Lambda functions/Anonymous functions

Python Julia
Code
list(map(lambda x:x**2-3, [1,2,3]))
map(x -> x^2-3, [1,2,3])
Output
[-2, 1, 6]
3-element Vector{Int64}: 
-2
1 
6

Function arguments - Default

Python Julia
Code
def f(x,*,y=6,z=-2):
    return x+y+z 
def g(x,y=6,z=-2):
    return x+y+z
f(5)
f(5,y=7,z=3)
g(5,y=7,z=3)
g(2)
g(2,4,6)
f(x;y=6,z=-2)=x+y+z
g(x,y=2,z=2)=x+y+z
f(5) 
f(5,y=7,z=3)
f(5;y=7,z=3)
g(2)
g(2,4,6)
Output
9
15
15
6
12
9
15
15
6
12

Arrays

Array/Vector

Python Julia
Code
np.array([1,2,3])
[1,2,3]
Output
np.array([1,2,3])
3-element Vector{Int64}:
1
2
3

Array - append

Python Julia
Code
np.append(np.array([23,5]),3)
push!([23,5],3)
Output
array([23,  5,  3])
3-element Vector{Int64}:
23
5
3

Array - concatenation

Python Julia
Code
np.concatenate([[2,4],[35,3543,3,54]])
1. vcat
vcat([2,4],[35,3543,3,54])
2. reduce and vcat
reduce(vcat,([[2,4],[35,3543,3,54]]))
3. append!
append!([2,4],[35,3543,3,54])
Output
array([2,4,35,3543,3,54])
6-element Vector{Int64}:
2
4
35
3543
3
34

Array - Slicing

Python Julia
Code
[1, 5, 6, 7][1:4]
[1 5 6 7][2:4]
Output
[5, 6, 7]
3-element Vector{Int64}:
 5
 6
 7
Code
#slice index [start:stop:step]
np.array(range(1,6))[0::2]
#slice index [start:step:stop]
Array(1:5)[1:2:end]
Output
array([1, 3, 5])
3-element Vector{Int64}:
 1
 3
 5
Code
np.array(range(1,6))[-1]
Array(1:5)[end]
Output
5
 5
Code
np.array(range(1,7))[1:-1:2]
Array(1:6)[2:2:end-1]
Output
array([2, 4])
 2-element Vector{Int64}:
 2
 4

Array - Reshape

Python Julia
Code
x = np.array([1,2,3]) #1d array,
                      # shape: (3,)
x = x.reshape(-1,1) #3x1 2d array, 
                    #shape: (3,1)
x
x = [1,2,3]   #3 element Vector,
              #shape: (3,)
x = reshape(x,(:,1))  #3x1 Matrix,
                      #shape: (3,1)
x
Output
array([[1],
       [2],
       [3]])
3×1 Matrix{Int64}:
 1
 2
 3

Matrices

Python Julia
Code
np.array([[1,2,3]])
[1 2 3]
Output
array([[1, 2, 3]])
1×3 Matrix{Int64}:
1  2  3
Code
np.array([[1,2,3],[4,5,6]])
[1 2 3;4 5 6]
Output
array([[1, 2, 3],       
       [4, 5, 6]])
2×3 Matrix{Int64}:
1  2  3
4  5  6

Matrices - Transpose/Adjoint

Python Julia
Code
np.array([[1,2,3]]).T
[1 2 3]'
Output
array([[1],
       [2],
       [3]])
3×1 adjoint(::Matrix{Int64}) with eltype Int64:
1
2
3

Matrix muliplication

Python Julia
Code
np.array([[1,2],[3,4]])@np.array([[4,5],[6,7]])
[1 2;3 4]*[4 5;6 7]
Output
array([[16, 19],
       [36, 43]])
2×2 Matrix{Int64}:
16  19
36  43

Broadcasting

Python Julia
Code
import numpy as np
np.array([[1,2,3],[3,4,5]])-np.array([6,7,8])
[1 2 3; 3 4 5] .- [6 7 8]
Output
array([[-5, -5, -5],
       [-3, -3, -3]])
2×3 Matrix{Int64}:
 -5  -5  -5
 -3  -3  -3

Dictionaries

Python Julia
Code
{'a':2,'b':3}
Dict('a'=>2,'b'=>3)
Output
{'a': 2, 'b': 3}
Dict{Char, Int64} with 2 entries:
'a' => 2
'b' => 3

Strings

Strings or Chars?

Python Julia
Code
'a' == "a"
type(''),type("") 
'a' == "a"
typeof('a'),typeof("")
Output
True
(<class 'str'>, <class 'str'>)
false
(Char, String)

Strings - Indexing

Python Julia
Code
s = "python is 0 indexed" 
s[0],s[-1],s[1:9] 
s = "julia is 1 indexed" 
s[1],s[end],s[1:9]
Output
('p', 'd', 'ython is')
('j', 'd', "julia is ")

Strings - Concatenation

Python Julia
Code 1. .join
"".join(["concat"," 2"," strings"])
2. + operator
"concat"+" 2"+" strings"
1. string
string("concat"," 2"," strings")
2. * operator
"concat"*" 2"*" strings"
Output
'concat 2 strings'
"concat 2 strings"

Strings - Length

Python Julia
Code
len("python")
length("julia")
Output
6
5

Strings - Regex

Python Julia
Code
import re
re.findall("[a-zA-Z]p+","Apps that 
capture the spacious extent of 
the opera.")
collect(eachmatch(r"[a-zA-Z]p+","Apps 
that capture the spacious extent of 
the opera."))
Output
['App', 'ap', 'sp', 'op']
4-element Vector{RegexMatch}:
RegexMatch("App")
RegexMatch("ap")
RegexMatch("sp")
RegexMatch("op")

f strings/String interpolation

Python Julia
Code
num = 9.0
print(f"This substitutes {num}")
num = 9.0
println("This substitutes $num")
Output This substitutes 9.0 This substitutes 9.0

Conditionals

Python Julia
Code
if x>0:
    print("x greater than 0.")
elif x<0:
    print("x less than 0.")
else:
    print("x equal to 0.")
if x>0
   print("x greater than 0.")
elseif x<0
    print("x less than 0.")
else
   print("x equal to 0.")
end

Logic

Python Julia
Code
5>0 and 6>0 and 6>-9
0==0 or 7>-9
not True
5>0 && 6>0 && 6>-9
0==0 || 7>-9 
!true
Output
True
True
False
true
true
false

Loops

While loops

Python Julia
Code
x = -7
while True:
    print(x)
    if x == -5:
        break
    x += 1
x = -7
while x != -5
    println(x)
    global x += 1
end
Output
-7
-6
-5
-7
-6

For loops

Python Julia
Code
for i in range(-3,3):
    if i%2 == 0:
       print(i)
for i = -3:3
    if i%2 == 0
       println(i)
    end
end
Output
-2
0
2
-2
0
2

List comprehension

Python Julia
Code
 X = ['a','b','c']
 Y = range(0,3)
[(x,y) for x in X for y in Y]
X = ['a','b','c']
Y = 1:3
[(x,y) for x in X, y in Y]
Output
[('a', 0), ('a', 1), ('a', 2), 
('b', 0), ('b', 1), ('b', 2), 
('c', 0), ('c', 1), ('c', 2)]
3×3 Matrix{Tuple{Char, Int64}}:
 ('a', 1)  ('a', 2)  ('a', 3)
 ('b', 1)  ('b', 2)  ('b', 3)
 ('c', 1)  ('c', 2)  ('c', 3)

Exceptions

Python Julia
Code
import math
def f(x):
    try: math.sqrt(x)
    except: print("x should be non-negative.")
f(-9)
function f(x)
    try
        sqrt(x)
    catch e
        println("x should be non-negative.")
    end
end
f(-9)
Output
x should be non-negative.
x should be non-negative.

Assertions

  Python Julia
Code assert 8 == 1 @assert 8 == 1
Output AssertionError ERROR: AssertionError: 8 == 1

Counting

Python Julia
Code
from collections import Counter
Counter([0.5, 0.5, 1, 4, 3, 3, 2, 1])
using StatsBase
countmap([0.5 0.5 1 4 3 3 2 1])
Output
Counter({0.5: 2, 1: 2, 3: 2, 4: 1, 2: 1})
Dict{Float64, Int64} with 5 entries:
4.0 => 1
2.0 => 1
0.5 => 2
3.0 => 2
1.0 => 2

Ternary operator

Python Julia
Code
a, b = 3, 8
-1 if a > b else 0
a, b = 3, 8;
a < b ? -1 : 0
Output
0
-1

Scoping

Python Julia
Code
x = 10
def f():
    global x 
    x = 3
    return x
f()
x = 10
function f()
    global x
    x = 3
    return x
end
f()
Output
3
3

Iterables

map

Python Julia
Code
z = map(lambda x,y: x+y, 
    ['zero_','one_','two_'],
    ['1','2','3'])
list(z)
map((x,y)-> x*y, 
    ["zero_","one_","two_"],
    ['1','2','3'])
Output
['zero_1', 'one_2', 'two_3']
3-element Vector{String}:
 "zero_1"
 "one_2"
 "two_3"

zip

Python Julia
Code
x,y,z = zip([1,2,3],[4,5,6],[8,9,10])
x
y
z
x,y,z = zip([1 2 3],[3 4 5],[8 9 10])
x
y
z
Output
(1, 4, 8)
(2, 5, 9)
(3, 6, 10)
(1, 3, 8)
(2, 4, 9)
(3, 5, 10)

enumerate

Python Julia
Code
for (x,y) in enumerate(['a','b','c']):
    print(x,y)
for (x,y) in enumerate(['a' 'b' 'c'])
    println(x,y)
end
Output
0 a
1 b
2 c
1a
2b
3c

url requests

Python Julia
Code
import requests
#pings web page
requests.get("http://www.google.com")
#prints page html to output
requests.get("http://www.google.com").text
using Downloads
#pings web page
Downloads.request("www.google.com")
#prints page html to output
Downloads.download("www.google.com",stdout)