Sõnastik

Valige vasakul üks märksõnadest ...

Programming in JuliaTypes

Lugemise aeg: ~30 min

Julia, like most programming languages, has built-in types for handling common data like numbers and text.

Numbers

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 often requires rounding slightly. The standard integer and floating point types in Julia are called Int64 and Float64, respectively, because 64 bits are used to store an object of either type.

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 6.0.

Numbers can be compared using the operators ==,>,<,≤,≥. Recall that an operator is a function that .

Exercise
What is the type of the object returned by 1 == 2?

1 == 2

Exercise
x == 1 is which returns true or false according to whether . Meanwhile, x = 1 is that .

Strings

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 length function: length("hello") returns .

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[1], the second element using s[2], and so on. We can get the substring from the third to the eighth character using s[3:8].

Exercise
For which values of a and b does the expression "Hello World"[i:j] == "o Wo" return true? i = and j =

"Hello World"[i:j]

Exercise
If j is replaced with end in the expression s[i:j] (where 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.

String interpolation

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)
"""

Exercise
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.

A = 2

Solution. The expression "multiplying by 6.2 yields $(6.2*A)" works.

Booleans

A Bool is a special type whose only values are true and false. The fundamental operators that can be used to combine boolean values are && (and), || (or), and ! (not).

Exercise
Does Julia convert types when doing equality comparison? In other words, does 1 == 1.0 return true or false?

1 == 1.0

Solution. Yes, Julia does convert types for equality comparison. So 1 == 1.0 returns true.

Exercise
Write a one-line function which takes 3 bools as arguments and returns true if and only if either

  1. Both of the first two arguments are true , or
  2. The third argument is false
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 a and !b, respectively, assuming a and b are both bools. The more succinct versions are preferred.

Exercises

Exercise

Write some code for computing \frac{1}{a+\frac{2}{3}} where a 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)

Exercise
The expression 1 < 3 returns , which is an object of type .

Exercise
If we set s = "Bruno", then s[1:j] == "Bru" when j = .

Bruno
Bruno Bruno