Mastering Julia: Variables, Conditionals, Loops, and Practical Uses

avatar

Mastering Julia: Variables, Conditionals, Loops, and Practical Uses

The Julia programming language is specifically designed to work in areas related to data visualization, machine learning, scientific computing, and more. For this reason, it is mostly used in specialized fields. Julia programming language is designed and produced with the aim of combining the advantages of C and C++ languages ​​and Python and R programming languages.

What is the Julia programming language?

Julia is a high-level, dynamic and multipurpose programming language with many features. The main applications of Julia are in computer science, scientific computing and artificial intelligence applications. The Julia language is designed with the aim of being efficient and simple to use, and has a rich set of libraries and various tools to perform various projects in areas such as visualization and data analysis.

The development of Julia has been done using a new approach to perform technical calculations and combining the simplicity of using Python and R language, along with the efficiency of C++ and Fortran programming language. In addition to the above, Julia code provides a very simple and fluent syntax that helps us implement new mathematical ideas with simpler expression.

Another positive point to mention about Julia is that it is open source. With the help of this feature, anyone can start coding with it without any problems and for free. Because Julia is designed to perform high-quality calculations, it has become an excellent choice for large-scale data analysis projects. So if you are looking for a powerful programming language that is open source and easy to use, Julia is one of the best choices.

History of Julia

The Julia programming language was first introduced in 2012 by a team of developers led by Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and Alan Edelman. Since its inception, Julia has been used in many diverse fields such as machine learning, data science, and scientific computing.

It was named Julia for no particular reason just because one of its designers - Mr. Jeff Bezanson - called the language Julia in an interview. Unlike other technical programming languages ​​that were used to perform calculations at that time, Julia was designed with the goal of being an independent programming language, with various applications and capable of managing various tasks.

The design of Julia started in 2009 with the aim of creating an open source language, with high performance and suitable for numerical and scientific calculations.

The design philosophy of this language emphasizes better programming performance and more code readability. In addition to object-oriented programming and traditional commands, Julia also supports functional programming and metaprogramming. The main goal in all versions of the Julia language is to increase the quality, to be closer to the design philosophy and to reduce errors.

Coding with Julia programming language

Julia is one of the high-level and dynamic languages ​​designed with the aim of providing the processing speed of C and C++ languages ​​along with the simplicity of using Python language. It means that developers can solve problems with higher speed and higher quality.

Julia is an excellent choice for solving complex computational problems. Many Julia users are focused on working in scientific fields such as chemistry, biology, and machine learning.

Although Julia was designed with the aim of performing scientific calculations, it is a general purpose language. Julia programming language can also handle a variety of tasks such as web application development, game development, etc. Many experts are touting Julia as the next generation of languages ​​used in the fields of machine learning and data science, for the reasons listed below.

  • high speed
  • Flexibility
  • The growing trend of the ecosystem of specialized libraries for working on massive amounts of data

How to download Julia programming language?

There are two main ways to run Julia code, which I've listed below.

  • Run Julia code files with .jl extension in IDEs like VSCode
  • Execution of Julia language commands using REPL specific to this language

The word REPL stands for "Read Evaluate Print Loop". This word refers to a native environment for coding this language. In some other languages, this environment is called REPL.

To download Julia language, it is enough to go to the official website of this programming language and download the desired version. Of course, you can also download the latest version of Julia from the Microsoft Store by entering the following code in the "Command Prompt" of Windows.

winget install julia -s msstore

Along with the installation of Julia, the REPL is also installed on the computer.

Basics of coding the Julia programming language

Before using Julia to do all sorts of exciting projects, the language was built with the goal of working on projects like machine learning or data science. However, to implement any type of project, the first task is to familiarize yourself with the basic principles of coding in the Julia programming language.

Variables and types in Julia

In Julia, variables are typed dynamically. This means that programmers do not need to specify its type when defining a variable.

In the box below, I have implemented the variable definition example in the Julia language with a simple method and direct value assignment to the variable.

julia> a = 10
10
julia> a + 10
20

Note that in the code snippet used, when the expression julia>  is displayed, it means writing the Julia codes inside the REPL.

As can be seen in the code above, I have defined a variable and assigned a value of type Integer to it. The Integer type in programming represents a whole number or an integer without a decimal part.

The example I did above can be done in the same way for string type variables in programming or other types of variables.

julia> my_string = "Hello albro Blog" #string
"Hello albro Blog"
julia> balance = 238.19 #float
238.19

When defining a variable in Julia language, the name of the variable is always placed on the left side of the equal sign and the value of the variable must be placed on the right side of this sign. One of the other tasks is to create a new variable using the values ​​stored in other variables.

julia> new_balance = balance + a
248.19

In the box above, we can see that the value of the new_balance variable is now equal to the sum of 238.19 and 10. That is, I defined and initialized a new variable using two other variables. Pay attention to the fact that the variable type new_balance is of the Float type in programming - a number including a decimal part. Because when we add a Float type variable with an Int variable in programming, the answer automatically takes the higher precision type. In this particular case, the precision of the Float type is higher.

To check the type assigned to each variable, we can use the following method.

julia> typeof(new_balance)
Float64

Due to the nature of dynamic typing, all variables in the Julia language can change their type at runtime. This means that the holder_balance variable at a certain time can contain a value with a decimal number type and a little later a value with a string type may be assigned to it.

julia> holder_balance = 100.34
100.34
julia> holder_balance = "The Type has changed"
"The Type has changed"
julia> typeof(holder_balance)
String

The interesting thing about the Julia language is that there is a high degree of flexibility in the naming of variables. In fact, programmers can even use the following codes.

julia> 😀 = 10
10
julia> 🥲 = -10
-10
julia> 😀 + 🥲
0

In addition to the variable name defined as an emoji, we can also use other types of Unicode characters as variable names. Such freedom of action can be very useful when displaying mathematical symbols. Any character in Unicode can be easily used by putting \   before the desired symbol name. Finally, we must press the "Tab" button.

In the code below, I have shown how to use this technique.

julia> \sigma # press tab and it will render the symbol
julia> σ = 10

The entire variable system in Julia is flexible and provides a large set of features. So writing Julia code is simple while being clear.

How to write conditional statements in Julia

In programming, sometimes in order to execute a certain set of codes, we have to check the existence of a specific condition. For example, if we are writing a program for a bank, we should probably only allow customers to withdraw money if the amount they want to withdraw is less than or equal to their account balance.

In the box below, I have implemented a simple example of using a conditional statement in the Julia programming language.

julia> bank_balance = 4583.11
4583.11
julia> withdraw_amount = 250
250
julia> if withdraw_amount <= bank_balance
           bank_balance -= withdraw_amount
           print("Withdrew ", withdraw_amount, " from your account")
       end
Withdrew 250 from your account

Now that I've reviewed and explained the code above, I'll take a closer look at the if  conditional statement in Julia. Because in the above box, some parts may be different from other programming languages ​​we have seen before.

  • At first, I didn't use the symbol :   to indicate the end of the line. Also, I don't need to use open and close parentheses to enclose the conditional statement. Of course, the use of parentheses () for conditional expressions is not only prohibited, but also recommended due to increasing the readability of codes.
  • At the end, I have not used open and closed braces {} to show the end of the conditional block. Instead of {} in Julia, the end keyword is used.

In exactly the same way that I used the if conditional statement, I can add more conditional statements and expand it by using the else or elseif statements at the end.

julia> withdraw_amount = 4600
4600
julia> if withdraw_amount <= bank_balance
           bank_balance -= withdraw_amount
           print("Withdrew ", withdraw_amount, " from your account")
       else
           print("Insufficent balance")
       end
Insufficent balance

How to use loops in Julia language

The general structure of the loop is defined in almost the same way in different programming languages. In Julia language, like other languages, there are two main types of loops.

  • for loop: This loop counts over a specified number of elements, such as a range of data within a scrollable structure. This loop is useful when we know the number of iterations or traversal in advance.
  • while loop: This loop is usually used to execute specified codes until a certain condition is true. In particular, the use of the while loop is appropriate when the number of repetitions of the loop cannot be specified in advance. The stop time of the loop is not only not related to a certain repetition, but is determined based on the change of certain conditions.

These loops work much like their counterparts in other programming languages ​​and provide the necessary flexibility for the types of iterations needed in the program.

By convention, loops in Julia are very similar to the if control construct I discussed above. In the box below, I have implemented a simple code example on using loops.

julia> greeting = ["Hello", "world", "and", "welcome", "albro"]
6-element Vector{String}:
 "Hello"
 "world"
 "and"
 "welcome"
 "albro"
julia> for word in greeting
           print(word, " ")
       end
Hello world and welcome albro

In the above example, first of all, I defined a new type. I have created the "Vector" type, which is also known as an array in the Julia language, with the name greeting in the first line. This array is holding a number of string type values ​​that I have optionally defined. The behavior of this array is very similar to the behavior of the array type in other programming languages. But it should be noted that arrays in Julia language are "mutable". This means that after creating the array, the number of elements inside it can be changed whenever we want.

After looking at the structure of the for loop, it can be recognized that this loop scrolls over the elements inside the greeting variable. With each step in the traversal - in the case of this array - I come across a new word and assign it to a temporary variable called word. Then I print the value of this variable in the output. By observing the structure of this loop, you can notice its similarity with the conditional if statement. Especially, at the end, the work of the loop ends with the keyword end.

So far, we've looked at for loops in the Julia programming language. From here on, it's time to check the while loop. Of course, at first, I have implemented a simple example of using the while loop in the Julia programming language and displayed it in the box below.

julia> while user_input != "End"
           print("Enter some input, or End to quit: ")
           user_input = readline()
       end
Enter some input, or End to quit: hi
Enter some input, or End to quit: test
Enter some input, or End to quit: no
Enter some input, or End to quit: End

In the example above, the while loop is set to run continuously until the user types the word "End". But the structure of this loop should be familiar to you according to the codes you have seen so far.



0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000