Learn Julia Basics Part:1
Who created Julia?
Julia was originally released in 2012 by Alan Edelman, Stefan Karpinski, Jeff Bezanson, and Viral Shah.
Why Julia was created?
Julia addresses the “two-language problem” by providing a high-level and fast language that allows researchers to use a single language for both prototyping and production-level code, eliminating the need for a separate, lower-level language for computationally intensive tasks. Easy as Python and fast as C.
Why the name?
That’s everybody’s favorite question. There’s no good reason, really. It just seemed like a pretty name. - Stefan Karpinski
fun fact: The julia extension actually used to be .j
. and they changed it into .jl
Printing in Julia
- Using the
print()
function to print a string or any other data type to the console.
julia> print("without new line")
without new line
julia>
- Using the
println()
function to print a string or any other data type to the console and append a newline character (\n
) at the end.
julia> println("with new line")
with new line
julia>
- Using the
printf()
function to print formatted output to the console, similar to C’s printf function.
julia> using Printf
julia> @printf "to format the printed string, like this: %i, %s" 100 "👍"
to format the printed string, like this: 100, 👍
julia>
also @sprintf("%Fmt", args...)
Return @printf
formatted output as string.
julia> @sprintf "this is a %s %15.1f" "test" 34.567
"this is a test 34.6"
-
The
15
specifies the total width of the printed field, including padding or signs, ensuring a minimum of 15 characters in the resulting string, including decimal point and leading spaces or signs. -
The
.1
specifies the floating-point number’s precision for digits to the right of the decimal point. Here, it’s 1, resulting in one digit to the right of the decimal point.
- Using the
@show
macro to print both the expression and its result, similar to Python’sprint()
function with therepr
option enabled.
julia> @show("@show macro Shows expression and result, returns final result value.")
"@show macro Shows expression and result, returns final result value." = "@show macro Shows expression and result, returns final result value."
"@show macro Shows expression and result, returns final result value."
julia>
Variables
x = 10
julia> typeof(x)
Int64
Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0
also emojis are acceptable variable name.
Arithmetic Operations
# Division
julia> 10 / 15
0.6666666666666666
# Fractions
julia> 10 // 15
2//3
# integer division, truncated to an integer
julia> 10 ÷ 15
0
julia> 15 ÷ 10
1
# Inverse division
julia> 10 \ 3
0.3
# Modulus/Remainder
julia> 11 % 4
3
Bitwise Operations
Bitwise NOT (~
):
To calculate bitwise NOT(negation), think of ~i=-i-1
julia> x = 11
11
julia> ~x
-12
Bitwise AND Operator (&
):
# Bitwise AND
julia> 5 & 3
1
Bitwise Inclusive OR (|
):
julia> 10 | 12
14
1010
1100
-------
1110
10 = 1010
and 12 = 1110
in binary.
1010 | 1100 = 1110
in binary, which is 14
in decimal.
Bitwise Exclusive OR (⊻
):
julia> 10 ⊻ 12
6
To show ⊻
symbol, in REPL do \xor
and press the tab it’ll convert into ⊻
symbol.
1010
1100
-------
0110
10 = 1010
and 12 = 1110
in binary.
1010 ⊻ 1100 = 0110
in binary, which is 6
in decimal.
Logical Shift Right (>>>
):
214 = 11010110
in binary, if we perform a logical shift right by 2 positions, then it’ll be 00110101
= 53 in decimal, the bits have been shifted two positions to the right, and zeros have been inserted from the left.
julia> 214 >>> 2
53
Arithmetic Shift Right (>>
):
julia> -42 >> 2
-11
value = -42 # Binary: 11111111_11111111_11111111_11010110 (assuming 32-bit two's complement representation)
shifted_value = value >> 2 # Arithmetic shift right by 2 positions
println(shifted_value) # Output: -11 (Binary: 11111111_11111111_11111111_11110101)
Arithmetic Shift Left (<<
):
julia> -42 << 2
-168
value = -42 # Binary: 11111111_11111111_11111111_11010110 (assuming 32-bit two's complement representation)
shifted_value = value << 2 # Arithmetic shift left by 2 positions
println(shifted_value) # Output: -168 (Binary: 11111111_11111111_11111111_10110000)
For Loop
julia> arr = [1, 2, 3, 4]
5-element Vector{Int64}:
1
2
3
4
julia> for i in arr
println(i)
end
1
2
3
4
julia> for n in 1:3
println(n)
end
1
2
3
While Loop
julia> n = 0
0
julia> while n < 3
n += 1
println(n)
end
1
2
3
Comments
# This is a single line comment
#=
This is a multiline comment
=#
String Interpolation
julia> a = 3
7
julia> b = 5
5
julia> str = "a is $a. b is $b. a*b is $(a*b)";
julia> println(str)
a is 3. b is 5. a*b is 15