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/)
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 Juliaand
Is&&
、or
Is||
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]
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"
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
Recommended Posts