Julia Examples for beginners
Published in
2 min readMar 12, 2022
In this article, we’ll see 10 basic example programs using Julia.
For installation please check the article here.
- Variable:
var = 0
var = "Dog"print(var)
2. Function:
function changeNum()
x = "Dog"
print(x)
endchangeNum()
3. Line plot:
#Installing necessary modules
using Pkg
Pkg.add("Plots")#Import necessary modules
using Plots#data to plot
temperature = [20, 27, 26, 30, 14.5, 15]
place = ["A", "AA", "ABC", "ACD", "BD", "CDA"]#use GR module
gr();figure = plot(temperature, place, label="line")display(figure)sleep(5)
4. Scatter Plot:
#Installing necessary modules
using Pkg
Pkg.add("Plots")# Import necessary modules
using Plots#data to plot
temperature = [20, 27, 26, 30, 14.5, 15]
place = ["A", "AA", "ABC", "ACD", "BD", "CDA"]#use GR module
gr();
figure = scatter!(temperature, place, label="points")# @show figure
display(figure)sleep(5)
5. Arithmetic Operations:
a = 1
b = 2
c = 3sum = a + b
subtract = b - c
mult = b * a
divide = sum/multprintln(sum)
println(subtract)
println(mult)
println(divide)sentence = string("Hello", " Featurepreneur!")println(sentence)
mult_sent = "Hello" * " Featurepreneur!"
println(mult_sent)
6. Slicing string:
sentence = "Just few random words"println(length(sentence))
println(sentence[1])
println(sentence[end])
println(sentence[1:4])
7. Conditional evaluation:
x = 1
y = 2if x < y
println("x is lesser than y")
elseif x > y
println("x is greater than")
else
println("x is equal to y")
end#In function:
function test(x, y)
if x < y
println("x is lesser than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
endtest(1,2)
8. Bool operators:
age = 12if age >= 6 && age <= 10
println("You're in Lowhttps://docs.google.com/document/d/1OzKOuCDC6FauRYHC7PzsUfM--xVm2pZAU6_sfK-Frx0/editer Primary")elseif age >= 11 && age <= 12
println("You're in Upper Primary")elseif age >= 13 && age <= 15
println("You're in High school")elseif age >= 17 && age <= 18
println("You're in Higher Secondary")else
println("Stay Home")end
9. Get input:
#get input from the user
val = chomp(readline())
age = parse(Int32, val)if age >= 6 && age <= 10
println("You're in Lower Primary")elseif age >= 11 && age <= 12
println("You're in Upper Primary")elseif age >= 13 && age <= 15
println("You're in High school")elseif age >= 17 && age <= 18
println("You're in Higher Secondary")else
println("Stay Home")end
10. Tuple:
t1 = (1,2,3,4)
println(t1)# Get a value
println(t1[1])# Get all
for i in t1
println(i)
end