First codes#
Variables and types#
A variable, in Julia, is a name associated (or bound) to a value. It’s useful when you want to store a value (that you obtained after some math, for example) for later use. For example:
# Assign the value 10 to the variable x
x = 10
# Doing math with x's value
x + 1
# Reassign x's value
x = 1 + 1
# You can assign values of other types, like strings of text
x = "Hello World!"
"Hello World!"
println(x)
Hello World!
Allowed Variable Names#
Para los nombres de variables pueden ocupar letras, número y símbolos. Deben tener cuidado con algunas excepciones que están reservadas, por ejemplo el número pi, o funciones específicas que no se pueden modificar o que no queremos modificar, por ejemplo if, else, while, sum, mean, etc.
🍎 = 10
α = 0.1
Δ = 5
🌵 = 20
println((π, 🍎, α, Δ, 🌵))
(π, 10, 0.1, 5, 20)
Stylistic Conventions#
As suggested by Julia’s recommendations:
While Julia imposes few restrictions on valid names, it has become useful to adopt the following conventions:
Names of variables are in lower case.
Word separation can be indicated by underscores (‘_’), but use of underscores is discouraged unless the name would be hard to read otherwise.
Names of Types and Modules begin with a capital letter and word separation is shown with upper camel case instead of underscores.
Names of functions and macros are in lower case, without underscores.
Functions that write to their arguments have names that end in !. These are sometimes called “mutating” or “in-place” functions because they are intended to produce changes in their arguments after the function is called, not just return a value.
Types#
Types
will be a fundamental part of programming in Julia; they are flexible and will provide flexibility when programming and solving a problem.
Types
correspond to a category of values with specific characteristics. So far, we have seen two different types: integer and string.
As the course progresses, we will introduce different types
. The basic ones we will use include: integers
, floats
, strings
, arrays
, and booleans
.
To see what type a variable is, you can use the function typeof(variable)
.
a = 10
b = 10.0
c = "Hello"
d = [1,2,3]
e = true
f = [a,b]
g = [a,b,c]
println("a: ", (a, typeof(a)))
println("b: ", (b, typeof(b)))
println("c: ", (c, typeof(c)))
println("d: ", (d, typeof(d)))
println("e: ", (e, typeof(e)))
println("f: ", (f, typeof(f)))
println("g: ", (f, typeof(g)))
a:
(10, Int64)
b: (10.0, Float64)
c: ("Hello", String)
d: ([1, 2, 3], Vector{Int64})
e: (true, Bool)
f:
([10.0, 10.0], Vector{Float64})
g: ([10.0, 10.0], Vector{Any})
Mathematical Operations#
Standard mathematical operations
Arrays: Vectors and Matrices
Logical operations
Standard mathematical operations#
Basic mathematical operations:
Addition (+)
Subtraction (-)
Times (*)
Divide (/)
Inverse divide (\ )
Power (^)
Remainder (%)
a = 10
println("+: ", a+a)
println("-: ", a-a)
println("*: ", a*a)
println("/: ", a/a)
println("^: ", a^a)
println("% :", a%a)
+: 20
-: 0
*: 100
/: 1.0
^: 10000000000
% :0
Arrays: Vectors and Matrices#
https://docs.julialang.org/en/v1/manual/arrays/
x = [1,2,3]
3-element Vector{Int64}:
1
2
3
x = [1 2 3]
1×3 Matrix{Int64}:
1 2 3
x = zeros(5,5)
5×5 Matrix{Float64}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
v = ones(3,3)
w = [1,2,3]
x = v*v
y = v.*v
z = x*w
println("x: ", x)
println("y: ", y)
println("z: ", z)
println("sum: ", sum(z))
x:
[3.0 3.0 3.0; 3.0 3.0 3.0; 3.0 3.0 3.0]
y: [1.0 1.0 1.0; 1.0 1.0 1.0; 1.0 1.0 1.0]
z: [18.0, 18.0, 18.0]
sum: 54.0
Logical operations#
1 == 1
true
true || false
true
true && false
false
x = 10
y = x < 20
true
Create a variable xmin equal to 1 and a variable xmax equal to 100.
Create a variable x that contains equidistant values from xmin to xmax.
Check the type of x.
Create a variable y that indicates whether each element in x is greater than 50. Hint: It should be of type BitVector.
Create a variable z that divides each element of x by each element of y.
Determine the maximum value of the vector z and find out which function can be used to obtain it.
With the variables A = rand(3,3) and B = ones(3,3), test the operations * , ^, .* , -, inv(), det(), and the difference between / and \ .