Julia Community 🟣

Vinod V
Vinod V

Posted on

Julia Basics

Basics

Just printing hello world is not enough, is it? You want to do more than that - you want to take some input, manipulate it and get something out of it. We can achieve this in Julia using constants and variables, and we'll learn some other concepts as well in this chapter.

Comments

Comments are any text to the right of the # symbol and is mainly useful as notes for the reader of the program.

For example:

print("hello world") # Note that print is a function
Enter fullscreen mode Exit fullscreen mode

or:

# Note that print is a function
print("hello world")
Enter fullscreen mode Exit fullscreen mode

Use as many useful comments as you can in your program to:

  • explain assumptions
  • explain important decisions
  • explain important details
  • explain problems you're trying to solve
  • explain problems you're trying to overcome in your program, etc.

Code tells you how, comments should tell you why.

This is useful for readers of your program so that they can easily understand what the program is doing. Remember, that person can be yourself after six months!

Literal Constants

An example of a literal constant is a number like 5, 1.23, or a string like "This is a string" or "It's a string!".

It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.

Numbers

Numbers are mainly of two types - integers and floats.

An example of an integer is 2 which is just a whole number.

Examples of floating point numbers (or floats for short) are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10^-4^.

Strings

A string is a sequence of characters. Strings are basically just a bunch of words.

You will be using strings in almost every Julia program that you write, so pay attention to the following part.

Double Quote

You can specify strings using single quotes such as "Quote me on this".

All white space i.e. spaces and tabs, within the double quotes, are preserved as-is.

You can specify multi-line strings using double quotes. You can use single quotes freely within the double quotes. In case you want to use double quotes precede it with a "\" backslash character. An example is:

"This is a multi-line string. This is the first line.It's a text.
This is the second line.
\"What's your name?,\" I asked.
He said \"Bond, James Bond.\"
"
Enter fullscreen mode Exit fullscreen mode

Strings Are Immutable

This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.

The format method

Sometimes we may want to construct strings from other information. Julia uses the $ symbol to interpolate the variables directly into the string.

age = 20
name = "Swaroop"

println("$name was $age years old when he wrote this book")
println("Why is $name playing with that Julia?")
Enter fullscreen mode Exit fullscreen mode

Output:

Swaroop was 20 years old when he wrote this book
Why is Swaroop playing with that Julia?
Enter fullscreen mode Exit fullscreen mode

Notice that we could have achieved the same using string concatenation operator '*':

name * " is " * string(age) * " years old"
Enter fullscreen mode Exit fullscreen mode

but that is much uglier and more error-prone. Second, the conversion to string would be done automatically by the $ method instead of the explicit conversion to strings needed in this case. Third, when using the $ method, we can change the message without having to deal with the variables used and vice-versa.

What Julia does in the $ method is that it substitutes each argument value into the place of the specification. There can be more detailed specifications such as:

# decimal (.) precision of 3 for float '1.23456'
using Printf
@printf("%1.3f",1.23456)
Enter fullscreen mode Exit fullscreen mode

Output:

1.234
Enter fullscreen mode Exit fullscreen mode

Escape Sequences

Suppose, you want to have a string which contains a double quote ("), how will you specify this string? For example, the string is "This is a "special" string". You cannot specify as such because Julia will be confused as to where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string. This can be done with the help of what is called an escape sequence. You specify the double quote as \" : notice the backslash. Now, you can specify the string as "This is a \"special\" string".

Another useful escape sequence to know is the tab: \t. There are many more escape sequences but I have mentioned only the most useful ones here.

Variable

Using just literal constants can soon become boring - we need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.

Identifier Naming

Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:

Generally, variable names are written in lowercase. Underscores are used to separate different words in a variable name, but it is not advisable to use names that would require underscores. Function and macros names are in lowercase. Underscores are not used.The first character of types and modules is uppercase. The separation between words in names is done using upper camel case.The functions that modify or write to any of their arguments end with "!" symbol.

Data Types

Variables can hold values of different types called data types. The basic types are numbers and strings, which we have already discussed. In later chapters, we will see how to create our own types using struct keyword.

We will now see how to use variables along with literal constants. Save the following example and run the program.

How to write Julia programs

Henceforth, the standard procedure to save and run a Julia program is as follows:

  1. Open a text editor (notepad,notepad++,gedit etc)
  2. Create new file with the filename mentioned (file extension must be .jl).
  3. Type the program code given in the example.
  4. run the current file in julia command line as include("path to file")

Example: Using Variables And Literal Constants

Type and run the following program:

# Filename : var.jl
i = 5
print(i)
i = i + 1
print(i)

s = "This is a multi-line string.
This is the second line."
print(s)
Enter fullscreen mode Exit fullscreen mode

Output:

5
6
This is a multi-line string.
This is the second line.
Enter fullscreen mode Exit fullscreen mode

How It Works

Here's how this program works. First, we assign the literal constant value 5 to the variable i using the assignment operator (=). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5. Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen.

Then we add 1 to the value stored in i and store it back. We then print it and expectedly, we get the value 6.

Similarly, we assign the literal string to the variable s and then print it.

Note for static language programmers

Variables are used by just assigning them a value. No declaration or data type definition is needed/used.

Logical And Physical Line

A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Julia implicitly assumes that each physical line corresponds to a logical line.

An example of a logical line is a statement like print('hello world') - if this was on a line by itself (as you see it in an editor), then this also corresponds to a physical line.

Implicitly, Julia encourages the use of a single statement per line which makes code more readable.

If you want to specify more than one logical line on a single physical line, then you have to explicitly specify this using a semicolon (;) which indicates the end of a logical line/statement. For example:

i = 5
print(i)
Enter fullscreen mode Exit fullscreen mode

is effectively same as

i = 5;
print(i);
Enter fullscreen mode Exit fullscreen mode

which is also same as

i = 5; print(i);
Enter fullscreen mode Exit fullscreen mode

and same as

i = 5; print(i)
Enter fullscreen mode Exit fullscreen mode

However, I strongly recommend that you stick to writing a maximum of a single logical line on each single physical line. The idea is that you should never use the semicolon. In fact, I have never used or even seen a semicolon in a Julia program.

Indentation is not important in Julia but indented code tend to be more readable.

This blog post is an adaptation of byte-of-python
/basics.md
to Julia.

Top comments (0)