Skip to main content

Chapter 2 Storing number and strings and using Julia syntax

Although Scientific Computing problems generally involved developing models, at the lowest level, one is manipulating data generally strings and numbers. We begin with understanding these data types and how to store values in them.
We also show some of Julia syntax, which looks like other languages (like python). If you have some basic knowledge of computing, it is helpful, but no particular language is needed. We will see many standard programming structures in this book with a emphasis on those relevant to solving scientific computing problems.

Section 2.1 Numbers

Not surprisingly, the most important data type in scientific computing--at least at the most atomic level is numbers. Numbers in computation mimic those in mathematics with some important differences. Julia, like most computing languages, have two main number types: integers and floating points. Julia’s integers and much like mathematical integers in that they store numbers like \(0, 10, -400\text{.}\) Floating-point numbers generally are approximations to real or decimal numbers, such as 3.14159 or 1.23e+08 and more details will be covered in Chapter 3.
Julia also has a rational data type for numbers commonly though of as fractions like \(-\frac{2}{3}\)or \(\frac{22}{7}\text{.}\) Recall that typically \(i\) represents \(\sqrt{-1}\text{,}\) the base imaginary number. Complex numbers like \(2+3i\) are also native to Julia, however instead of \(i\text{,}\) Julia uses im. We will explore both rational and complex numbers in greater depth in Chapter 3 and have an entire chapter on complex numbers in Chapter 45.

Section 2.2 Assignment Statement and Variables

Anything can be stored as a variable using the single equal sign like x=6. This is an assignment operator, which creates the number 6 and stores it under the name x.
And now that the variable x is stored, we can use it in calculations. For example
x+3
returns 9.
Variables in Julia, much like other languages are primarily sequences of alphanumeric characters as well as an underscore _. Primarily, a variable needs to start with a alphabetic character or _ and after the first character can contain numbers. A variable cannot have a space in the name.
Julia also allows many unicode symbols in variable names, however not everything. For example, all of the greek letters are allowed, so α=45 is valid.
To get a greek letter in Jupyter or the REPL, type \alpha, hit the TAB key and it will be turned into an α.

Subsection 2.2.1 Storing Variable in a Virtual Whiteboard

The details of storing variables in computer hardware isn’t necessary, however, thinking of storing as writing variables and values on a whiteboard is a helpful paradigm. Imagine a whiteboard with a column of variable names and a column of values. For example, if we have
x=6
y=-1
z=8.5
then you can think of the whiteboard looking like:
Table 2.1. Whiteboard analogy of the storage of variables
Variable Value
x 6
y -1
z 8.5
If we evaluate the expression x+3, then the value of x is looked up and the value 6 is substituted into the expression or 6+3.
If we change one of the values, like y=y+5, this means that to the right of the equals sign is evaluated first. y is looked up as -1, then 5 is added to it to get 4, then the 4 is placed into the whiteboard, which will now look like:
Table 2.2. Updated whiteboard of variables
Variable Value
x 6
y 4
z 8.5
If you are thinking of how a piece of code works, often you will need to get to the point of writing down a version of the whiteboard.

Section 2.3 Strings

In many Scientific Computing fields, such as Data Science, strings arise often and it is important to understand some of the basics of them. In Julia, a string is a sequence of characters surrounded by "" (double quotes). For example:
str = "This is a string"
and if you enter typeof(str) then you should see String. The individual parts of the string are called characters, which have type Char and are by default Unicode Characters (which will we see are super helpful). We will see other data types in Chapter 3, but note that types in Julia are capitalized. A few other helpful things about strings are
  • The length of a string is found using the length command. length(str) returns 16
  • To access the first element of the string, type first(str), the last is found by last(str) and the 3rd character for example is str[3]. In Julia, string indexing starts at 1.
  • To turn other data types into string, use string. For example string(3.0) returns the string "3.0".

Subsection 2.3.1 String Operations

We saw how to access elements of a string. Another helpful operation is that of concatenation, or the merging of two strings. If
str1 = "The tide is high "
str2 = "and I'm having fun."
we can concatenate in multiple ways. First, the * operator symbol with strings as both operands or the string() function. Both
str1 * str2
string(str1,str2)
returns The tide is high and I'm having fun. I find that the * is an odd choice for string concatenation. Many languages including java and ecmascript (javascript) use + instead for string concatenation. Another way of performing concatenation is shown below using string interpolation.
Another cute operation for strings is the caret ^ operation. This could be helpful and a (not so helpful) example is
"Hip, hip, hooray! "^3
returns
Hip, hip, hooray! Hip, hip, hooray! Hip, hip, hooray!

Subsection 2.3.2 String Interpolation

Mixing strings and other variables together is often needed. If you have a variable x and would like to insert it at the end of "The value of x is "
we can use concatenation as above to do this, but instead we will use string interpolation by putting a $ in front of a variable.
result = "The value of x is $x"
and the result will be "The value of x is 6".
There is actually one other way to do string concatenation if you use this method. If we have the variables stored in str1 and str2, then "$str1$str2" also will concatenate the strings.

Subsection 2.3.3 Other String Functions

There are many other string functions, which are generally used for manipulating a string. The string chapter of the Julia documentation is a good place to look.
To effectively use strings, a knowledge of regular expressions are needed. Regular expressions are ways to parse a string and extract key information. Chapter 26 goes through details on how to develop and use regular expressions. We also use these to parse points written in string form and in Chapter 47 we develop the parsing of a polynomial written as a string.

Section 2.4 Expressions

An expression is a combination of variables, data elements (like numbers and strings), operations (like + or *) and functions (like length). We’ve seen a number of expressions throughout this chapter so far like
x = 6 
x+3 
str1 * str2 
length(str) 
In short, writing things in Julia will consist of writing expressions (and slightly more complicated structures).

Section 2.5 Operator Precedence

When we type out an expression like 11+2*(4+3)^3, it is important to understand the order in which operators are performed. For mathematics, the PEMDAS mnemonic is helpful to remember in that the order is:
  • Parentheses: The expression inside the ( ) are done first. For the example above, the 4+3 is the first operation done.
  • Exponentials: The ^ is done next. Raise the 7 from above to the power of 3.
  • Multiplication and Division: In this example, the 2*(343) is done next
  • Addition and Subtraction: Lastly add 11 to the result.
In any computing language, there are other operators as well and there is order to that precedence, so we will see that there are other things to think about. For example, the assignment operator, = has the lowest precedence. That is when assigning something to a variable, all calculations are done on the right side of the = before the assignment.
Details on all this can be found on the Julia documentation page on operator precedence.

Section 2.6 Comments

A comment in computer code is sequences of characters which are ignored. The purpose of a comment is to alert a human on what is going on. You may have been told to write comments so that someone else who reads your code understands what you are doing. However, I have found that the person mostly like to read your code is you at a later date. You should add comments for yourself.
In Julia, a comment is anything to the right of a #, pound sign or hash tag. For example:
# This calculates the area of a circle
r = 3
pi*r^2 # this is the actual formula for the area
Both lines 1 and 3 have comments. On line 1, the entire line is ignore since the line starts with #. On line 3, everything after the 2 (the power) is ignored. Also, notice that there are two hash tags on line 1 and 1 on line 3. This is simply different style. Since anything after a single # is a comment, everything after the first one is ignored.