Getting Started with Julia for Pythonista

It's a story that feels like it's decocted ... [Faster] than Python (http://julialang.org/benchmarks/), it's written closer to Python, and it's the most noticeable language in my life these days.

This time, I will introduce the difference in syntax that Pythonista needs to remember when using Julia by comparing Python and Julia.

Click here to build Julia's environment (http://julialang.org/downloads/)

Basic knowledge

The code is indented for clarity, but the indentation is not related to behavior.

julia


for n = 1:10
println(n)
end

If you write in one line, you need ;.

julia


for n = 1:10;println(n);end

Also, the index of list is from ** 1 **, not from 0.

julia


julia> List = [0, 1, 2]
3-element Array{Int64,1}:
 0
 1
 2

julia> println(List[1])
0

Hello World!

python


print "Hello World!"

julia


println("Hello World!")

for Loop statements

python


for i in range(10):
	print i

julia


for n = 0:9
	println(n)
end

If statements

python


hoge = "hoge"
if hoge[0] == 'h' and hoge[1:] == "oge":
	print "hoge is hoge"
elif hoge == "huge" or hoge == "hige":
	print "hoge is huge or hige"
else:
	print "hoge is", hoge

In JuliaandIs&&orIs||is.

julia


hoge = "hoge"
if hoge[1] == 'h' && hoge[2:4] == "oge"
	println("hoge is hoge")
elseif hoge == "huge" || hoge == "hige"
	println("hoge is huge or hige")
else
	println("hoge is ", hoge)
end

It works with hoge [2:], but * WARNING: deprecated syntax "x [i:]". * Appears, so I write hoge [2: 4].

Exception Handling (Try-Except)

python


List = [0, 1, 2]
try:
	print List[3]
except IndexError as e:
	print e

julia


List = [0, 1, 2]
try
	println(List[4])
catch e
	if isa(e, BoundsError) # "BoundsError"Says in python"IndexError"is
		println(e)
	end
end

Functions

python


def huge(x, y):
	z = x + y
	return z

julia


function huge(x, y)
	x + y
end

Or

julia


function huge(x, y)
	z = x + y
	return z
end

Anonymous Functions

python


z = lambda x, y : x + y 
z(1, 2)

julia


z = (x, y) -> x + y
z(1, 2)

Working with Files

Read

python


with open("output.txt") as f:
	for line in f:
		print line.rstrip() #Removed line feed code to match Julia output

julia


open("output.txt") do f
	lines = readlines(f)
	for line in lines
		print(line) #Julia print()Does not contain a line feed code
	end
end

Or

julia


open("output.txt") do f
	for line in eachline(f)
		print(line)
	end
end

Write

python


with open("output.txt", "w") as f:
	for i in range(10):
		f.write(str(i) + "\n")

julia


open("output.txt", "w") do f
	for n = 0:9
		write(f, string(n)"\n")
	end
end

List complemention

python


[[n**i for n in range(10)] for i in range(5)]

julia


[[n^i for n = 0:9] for i = 0:4]

Create a list of each element plus 10

python


[n+10 for n in range(10)]

julia


10+[0:9]

Detailed story

printf

python


Integer = 2
String  = "hoge"
print("Integer: %d String: %s" % (Integer, String))

julia


Integer = 2
String  = "hoge"
@printf "Integer: %d String: %s" Integer String

strip()

python


>>> "hoge\n".rstrip()
'hoge'

julia


julia> rstrip("hoge\n", '\n')
"hoge"

Other good points of Julia

infix form

julia


julia> +(10, 20, 30)
60

julia> *(10, 20, 30)
6000

Or

julia


julia> f = +
+ (generic function with 117 methods)

julia> f(10, 20, 30)
60

julia> f = *
* (generic function with 115 methods)

julia> f(10, 20, 30)
6000

inverse divide

julia


julia> 1/3
0.3333333333333333

julia> 3\1
0.3333333333333333

Function chaining

julia


julia> [0:5] |> x -> x.^x |> sum
3414

reference

Julia Documentation

Recommended Posts

Getting Started with Julia for Pythonista
Getting Started with Lisp for Pythonista: Supplement
Getting Started with Python for PHPer-Classes
Getting Started with Python for PHPer-Functions
Getting Started with Python for PHPer-Super Basics
1.1 Getting Started with Python
Getting Started with Golang 2
Getting started with apache2
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Settings for getting started with MongoDB in python
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Google App Engine for Python & PHP
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Python Basics of Python
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with CPU Steal Time
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Getting Started with Heroku, Deploying Flask App
Getting Started with TDD with Cyber-dojo at MobPro
Grails getting started
Getting started with Python with 100 knocks on language processing
MongoDB Basics: Getting Started with CRUD in JAVA
Getting Started with Drawing with matplotlib: Writing Simple Functions
Getting started with Keras Sequential model Japanese translation
~ Tips for Python beginners from Pythonista with love ① ~
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Getting started with AWS IoT easily in Python
Getting Started with Python's ast Module (Using NodeVisitor)
~ Tips for Python beginners from Pythonista with love ② ~
Materials to read when getting started with Python
Getting Started with Processing and p5.js (for those who have done other languages) 02