Having covered a few examples, let's now turn to a more systematic exposition of the essential features of the language.
Topics:
Like most languages, Julia language defines and provides functions for operating on standard data types such as
Let's learn a bit more about them.
A particularly simple data type is a Boolean value, which can be either true or false.
In [1]:
x = true
Out[1]:
In [2]:
typeof(x)
Out[2]:
In [3]:
y = 1 > 2 # Now y = false
Out[3]:
Under addition, true is converted to 1 and false is converted to 0.
In [4]:
true + false
Out[4]:
In [5]:
sum([true, false, false, true])
Out[5]:
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]:
In [7]:
typeof(1)
Out[7]:
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]:
In [9]:
x * y
Out[9]:
In [10]:
x^2
Out[10]:
In [11]:
y / x
Out[11]:
Although the * can be omitted for multiplication between a numberic literal and a variable.
In [12]:
2x - 3y
Out[12]:
Also, you can use function (instead of infix) notation if you so desire.
In [13]:
+(10, 20)
Out[13]:
In [14]:
*(10, 20)
Out[14]:
Complex numbers are another primitive data type, with the imaginary part being specified by im.
In [15]:
x = 1 + 2im
Out[15]:
In [16]:
y = 1 - 2im
Out[16]:
In [17]:
x * y # Complex multiplication
Out[17]:
In [18]:
x = "foobar"
Out[18]:
In [19]:
typeof(x)
Out[19]:
You've already seen examples of Julia's simple string formatting operations.
In [22]:
x = 10; y = 20
Out[22]:
In [23]:
"x = $x"
Out[23]:
In [24]:
"x + y = $(x + y)"
Out[24]:
To concatenate strings use *.
In [26]:
"foo" * "bar"
Out[26]:
Julia provides many functions for working with strings.
In [27]:
s = "Charlie don't surf"
Out[27]:
In [28]:
split(s)
Out[28]:
In [29]:
replace(s, "surf", "ski")
Out[29]:
In [30]:
split("fee,fi,fo", ",")
Out[30]:
In [31]:
strip(" foobar ") # Remove whitespace
Out[31]:
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]:
In [ ]: