Julia programming language | Starting with Julia

Contents

This article was published as part of the Data Science Blogathon.

Introduction

Hello readers!

Walk like a python. Run like C

The previous line explains a lot why I decided to write this article🤩. I reviewed Julia some time before, even though it was in its early stages, all the time I was making increases in the math log space.

Julia is developed by MIT, a high-level language that has a syntax that is beginner-friendly like Python and fast like C. This is not everything, provides distributed parallel execution, a sophisticated compiler, mathematical precision and extensive numerical function Library.

In this blog, we will learn the basics of Julia programming language with the help of code snippets. This article covers the basics of Julia, which helps you provide an overview on how to declare variables, how to use functions and many more.

If you are interested in machine learning in Julia, make the payment. Start machine learning with Julia: Julia's Top Libraries for Machine Learning🙂

49287intro-1769582

Variables and type

In this section, we will learn variables and their types and how to perform simple mathematical operations using Julia.

64970variable-2929259

Variables

In programming, when we want to store the data we want a container to store it, the name of this container is called variable. When we want to store the data in a variable, we do it by writing the following code:

name = "Akshay"
number = 23
pie_value = 3.141

Here the Name is a string containing a text value, a number contains an integer and pie_value is a floating number. Unlike C ++, we don't need to specify the variable type before the variable name.

Print the variable

If you want to print the value of the variable, we use the Print keyword. Check the following code to print the variable value:

>>> print(name)
   Akshay

Mathematical operations

And Julia, many mathematical operations are present that we can perform with variables. For instance, sum, multiplication, subtraction and many more.

a = 2
b = 3
sum = a + b
difference = a - b
product = a * b
quotient = b / a
power = a^3
modulus = b % a

See the image below to see all the math operations that are available in Julia:

31717mathematical20operation-4068359

For complete information on mathematical operations, mark this Link

Type of data

And Julia, each variable has its data type. There are many data types in Julia like Float, Int, String. See the image below to see all the data types that are available in Julia:

33652data20type-3006028

If we want to determine the type of variable, we use kind of function:

>>>typeof(0.2)
Float64
>>>typeof(50)
Int64
>>>typeof("Akshay")
String

To learn more about variables and their types, see this Link.

Features

These are Julia's building blocks. All the operations that we carry out on variables are carried out with the help of functions. They are also used to perform mathematical operations with variables. In a nutshell, a function is a kind of box that takes input to perform some operations and finally returns an output. In this section, we will study the function in great detail.

39722fucntion-6645724

Defining functions

And Julia, the function begins with the function keyword and ends with end.

function plus_two_number(x)
      #perform addition operation
        return x + 2
end

Note: Bleeding is essential in Julia. As you can see in the above code in the number lines 2 Y 3. Note that there is no fixed number of indentation spaces, but overall, four spaces they are the favorites.

An online version of a function

We can also create an online version of the function

plus_two_number(x) = x+2

Anonymous functions

We use the lamdas function in Python, similarly, and Julia, we can create anonymous functions using the following code:

plus_two_number = x -> x+2

Null functions

A function that accepts no arguments and returns no value is called a null function. Check the following code that does not take any arguments and print the Hello there message.

function say_hello()
      println("Hello")
      return
end

Note: We can use println function also to print the output value but add a new line at the end of the output.

Optional positional arguments

We can also take a value from the user and pass it to the function. Check the following code which converts weight (in kg) on Earth to some other value on a different planet

function Weight_calc(weight_Earth, g=9.81)
      return weight_Earth*g/9.81
end

Check the result below:

>>>Weight_calc(60)
60
>>>Weight_calc(60, 3.72)
22.75

For more information on the functions, see this Link

Data structures

In this section, we will discuss how we can store data in memory using different data structures. We will discuss vectors, tuples, named tuples, matrices and dictionaries.

82747data20structures-7497361

Cartoon vector

It is a list of ordered data that has a common type (for instance, Int, Any o Float) y is a one-dimensional array. Check the following syntax to create a vector in Julia:

a = [1,2,3,4,5,6,7]
b = [1.2, 3,4,5,6]
c = ["Akshay", "Gupta", "xyz"]

Matrices

They are two-dimensional matrices. Check the following syntax to create an array in Julia

Matri_1 = [4,5,7; 8,1,3]

Tuplas

It is a group of variables of fixed size and can contain a common data type, but it is not mandatory. Once the tuple is created, can not increase the size. Tuples are created using the following syntax:

a = (1,2,3,4,5)
b = 1, 2, 3, 4, 5

Then, these can be created using parentheses or without using them.

Tuple_1 = (1, 2, 3, 4)
a, b, c, d= Tuple_1
>>>print("$a $b $c $d")
1 2 3 4

Named tuples

They are the same as a tuple with a name identifier for a single value. Check the following example for the named tuple:

>>> named_tuple = (a = 1, b = "hello")
(a = 1, b = "hello")
>>>named_tuple[:a]
1

Dictionaries

It is a collection of keys and values. They are messy, which means that the order of the keys is not fixed. Check the following syntax to create a dictionary in Julia using the Dictate keyword

Person_1 = Dict("Name" => "Akshay", "Phone" => 83659108, "Pant-size" => 40)
Person_2 = Dict("Name" => "Raju", "Phone" => 75973937, "Pant-size" => 36)
>>>print(Person_1[“Name”])
   Akshay

To learn more about data structures in Julia, check this Link

Control flow

In this section of the blog, we will see control statements in Julia. First, we will start with conditional blocks, for instance and … but and after that, We will see how to loop using the For and While loops.

52377control20flow-1468531

And … but

Hope you have studied the if else condition in C / C ++. Then, used to make decisions according to certain conditions.

For instance, we want an absolute value of a number. It means that if the number passed is positive, we will return the number itself. Secondly, if it is negative, we will refund otherwise if it is a number. Check the following code on how if-else statements work.

function absolute_value(x)
    if x >= 0
        return x
    else
        return -x
    end
end

Note: You can see that the if-else block ends with end, as a function

Check more than one condition

If you want to check more than two conditions, we can use

  • Y (written as &)
  • O (written as ||)

Check the following code on how to use Y condition

if 1 < 4 & 3 < 5
    print("Welcome!")
end

For loops

If we want to iterate over a list of values ​​and perform some operation, we can use it by loop in Julia. Check the following code to print the square of the values ​​of the 1 al 10.

for j in 1:10
    println(j^2)
end

Production

1
4
9
16
25
36
49
64
81
100

While looping

Check the following code for Weather loop in julia

function while_loop()
    j=1
    while(j<20)
        println(j)
        j += 1
    than
than

Break

If we want to exit the loop after a certain condition is met, use un Break keyword. Check the following code on how to use Break in Julia

for j in 1:10
    if j>3
        break
    else
        println(j^2)
    than
than

Continue

Please refer to the following code on how to use the Continue keyword in Julia

for j in 1:20
    if j %2 == 0
        continue
    else
        println(j)
    than
than

To learn more about iterators and control flow, check this link

Conclution

Then, in this article, we had a great detailed discussion on the basics of the Julia programming language. I hope you learn something from this blog and it turns out better for your project. Thanks for reading and your patience. Good luck!

For more Julia related blogs, see the following links:

Start machine learning with Julia: Julia's Top Libraries for Machine Learning

Data visualization in Julia using Plots.jl: with practical implementation

You can check my articles here: Articles

Email identification: [email protected]

Connect with me on LinkedIn: LinkedIn

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.