Programming in JuliaTypes
Julia, like most programming languages, has built-in types for handling common data like numbers and text.
As discussed in the previous section, a numerical value can be either an integer or a floating point number. We can represent integers exactly, while storing a real number as a float
A number typed directly into a Julia program is stored as a float or integer according to whether it contains a decimal point, so if you want the value 6 to be stored as a
Float64, you should write it as
Numbers can be compared using the operators
==,>,<,≤,≥. Recall that an operator is a function that
What is the type of the object returned by
1 == 2?
1 == 2
x == 1 is
false according to whether
x = 1 is
Textual data is represented using a sequence of characters called a string. We can create a string object by enclosing the desired sequence of characters in quotation marks:
a = "this is a string". Such a quote-enclosed string of characters in a Julia program is called a string literal. String literals can also be delimited by triple quotes, which can be useful for multi-line strings and for strings containing quotes.
""" This is a multiline string. It can have "quotes", no problem. """ "This is an ordinary string. \"Quotes\" require a backslash."
We can find the number of characters in a string with the
We can concatenate two strings with the multiplication operator (
"Hello " * "World".
We can return the first character in a string
s using the expression
s, the second element using
s, and so on. We can get the substring from the third to the eighth character using
For which values of
b does the expression
"Hello World"[i:j] == "o Wo" return
true? i =
j is replaced with
end in the expression
s is a string), what happens? Experiment using the code block above.
Solution. Indexing with an expression involving
end is the same as replacing
end with the length of the string.
We can insert the value of a variable into a string using string interpolation:
x = 19 """ The quotient when x is divided by 3 is $(x÷3), and the remainder is $(x % 3) """
Use string interpolation to write a single line of code which prints
multiplying by 6.2 yields 12.4 if
2 is assigned to the variable
A and prints
multiplying by 6.2 yields 18.6 if
3 is assigned to
A = 2
Solution. The expression
"multiplying by 6.2 yields $(6.2*A)" works.
Bool is a special type whose only values are
false. The fundamental operators that can be used to combine boolean values are
|| (or), and
Does Julia convert types when doing equality comparison? In other words, does
1 == 1.0 return
1 == 1.0
Solution. Yes, Julia does convert types for equality comparison. So
1 == 1.0 returns
Write a one-line
true if and only if either
- Both of the first two arguments are
- The third argument is
f(a,b,c) = # add code here using Test @test f(true, true, true) @test f(false, true, false) @test !f(false, true, true)
Solution. Here's an example of a simple way to do it:
f(a,b,c) = a && b || !c
Be wary of comparisons of the form
a == true or
b == false. These are equivalent to
b are both bools. The more succinct versions are preferred.
Write some code for computing where is equal to the number of characters in the string
"The quick brown fox jumped over the lazy dog"
Solution. We store the length of the given string in a variable
a and evaluate the given expression as follows:
a = length("The quick brown fox jumped over the lazy dog") 1/(a+2/3)
1 < 3 returns
If we set
s = "Bruno", then
s[1:j] == "Bru" when