Julia Essentials

  • Overview
  • Common Data Types
  • Input and Output
  • Iterating
  • Comparison and Logical Operators
  • User-Defined Functions
  • Vectorized Functions
  • Exercises
  • Solutions

Having covered a few examples, let's now turn to a more systematic exposition of the essential features of the language.

Overview

Topics:

  • Common data types
  • Basic file I/O
  • Iteration
  • More on user-defined functions
  • Comparisons and logic

Common Data Types

Like most languages, Julia language defines and provides functions for operating on standard data types such as

  • integers
  • floats
  • strings
  • arrays, ets…

Let's learn a bit more about them.

Primitive Data Types

A particularly simple data type is a Boolean value, which can be either true or false.


In [1]:
x = true


Out[1]:
true

In [2]:
typeof(x)


Out[2]:
Bool

In [3]:
y = 1 > 2  # Now y = false


Out[3]:
false

Under addition, true is converted to 1 and false is converted to 0.


In [4]:
true + false


Out[4]:
1

In [5]:
sum([true, false, false, true])


Out[5]:
2

The two most common data types used to represent numbers are integers and floats.
(Computers distinguish between floats and integers because arithmetic is handled in a different way.)


In [6]:
typeof(1.0)


Out[6]:
Float64

In [7]:
typeof(1)


Out[7]:
Int64

If you're running a 32 bit system you'll still see Float64, but you will see Int32 instead of Int64 (see the section on Integer types from the Julia manual.)

Arithmetic operations are fairly standard.


In [8]:
x = 2; y = 1.0


Out[8]:
1.0

In [9]:
x * y


Out[9]:
2.0

In [10]:
x^2


Out[10]:
4

In [11]:
y / x


Out[11]:
0.5

Although the * can be omitted for multiplication between a numberic literal and a variable.


In [12]:
2x - 3y


Out[12]:
1.0

Also, you can use function (instead of infix) notation if you so desire.


In [13]:
+(10, 20)


Out[13]:
30

In [14]:
*(10, 20)


Out[14]:
200

Complex numbers are another primitive data type, with the imaginary part being specified by im.


In [15]:
x = 1 + 2im


Out[15]:
1 + 2im

In [16]:
y = 1 - 2im


Out[16]:
1 - 2im

In [17]:
x * y  # Complex multiplication


Out[17]:
5 + 0im

Strings

A string is a data type for storing sequence of characters.


In [18]:
x = "foobar"


Out[18]:
"foobar"

In [19]:
typeof(x)


Out[19]:
String

You've already seen examples of Julia's simple string formatting operations.


In [22]:
x = 10; y = 20


Out[22]:
20

In [23]:
"x = $x"


Out[23]:
"x = 10"

In [24]:
"x + y = $(x + y)"


Out[24]:
"x + y = 30"

To concatenate strings use *.


In [26]:
"foo" * "bar"


Out[26]:
"foobar"

Julia provides many functions for working with strings.


In [27]:
s = "Charlie don't surf"


Out[27]:
"Charlie don't surf"

In [28]:
split(s)


Out[28]:
3-element Array{SubString{String},1}:
 "Charlie"
 "don't"  
 "surf"   

In [29]:
replace(s, "surf", "ski")


Out[29]:
"Charlie don't ski"

In [30]:
split("fee,fi,fo", ",")


Out[30]:
3-element Array{SubString{String},1}:
 "fee"
 "fi" 
 "fo" 

In [31]:
strip(" foobar ")  # Remove whitespace


Out[31]:
"foobar"

Julia can also find and replace using regular expressions (see the documentation on regular expressions for more info).


In [32]:
match(r"(\d+)", "Top 10")  # Find digits in string


Out[32]:
RegexMatch("10", 1="10")

In [ ]: