62
55

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonistaのためのJulia入門

Last updated at Posted at 2014-12-30

何番煎じって感じのネタですけど...
Pythonよりも速く、書き方がPythonに近く、最近私の中で注目度No.1の言語です。

今回は、PythonistaがJuliaを使うにあたって覚える必要のある構文の違いを、PythonとJuliaで比較して紹介します。

Juliaの環境構築はこちらからどうぞ

基礎知識

コードには見やすいようにインデントを付けていますが、インデントは動作に関係しません。

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

1行で書く場合は;が必要です。

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

またlistのindexは0からではなく 1 からです。

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

Juliaにおけるand&&or||です。

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

hoge[2:]でも動くんですが、 WARNING: deprecated syntax "x[i:]". が出るので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"はPythonで言う"IndexError"です
		println(e)
	end
end

Functions

python
def huge(x, y):
	z = x + y
	return z
julia
function huge(x, y)
	x + y
end

もしくは

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() # Juliaの出力に合わせるため改行コードを削除
julia
open("output.txt") do f
	lines = readlines(f)
	for line in lines
		print(line) # Juliaのprint()には改行コードが入りません
	end
end

もしくは

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]

各要素に10を足したリストを作成する

python
[n+10 for n in range(10)]
julia
10+[0:9]

細かい話

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"

その他Juliaの良いところ

infix form

julia
julia> +(10, 20, 30)
60

julia> *(10, 20, 30)
6000

もしくは

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

参考

Julia Documentation

62
55
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
55

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?