From the course: Learning Julia

The Julia REPL - Julia Tutorial

From the course: Learning Julia

Start my 1-month free trial

The Julia REPL

- [Instructor] Most of the programming work you will do in Julia will take place in an IDE, but Julia does provide an interactive environment where you can try out small pieces of code. This is called a REPL, which stands for the read, evaluate, print loop. Essentially, you type in Julia commands, the REPL executes the code, and then you type in more commands. If you've done any programming in a language like Python, then maybe you've seen this before. And just to be clear, this is not the best way to write a lot of Julia code at scale. You will usually use an IDE for that. This type of interactive shell is useful for learning about different parts of Julia or just for trying out small pieces of code that you want to test. We're not going to be making heavy use of the REPL in this course, but I wanted to demonstrate it so that you can try it out on your own time. To start the environment, open up your terminal program and if you've set up the path correctly on your computer, you can type the command, Julia, and that will start up the REPL environment. And then we can start trying out some code. So let's just try a simple expression. I'll just type one plus two and you can see that the result there is three. I can also use some Julia functions. So let's try out the print function and you can see the result there. To get the result of the previous expression, I can use the ANS keywork, which stands for answer. So let's try this again. I'll type two to the fifth power. Right, and that's 32 and then I'll type ans, and you can see that that gives me the previous answer. In fact, we can even write our own functions. So let's type a function definition and we'll get more into this in the course. I don't expect you to understand all of this right now, but we will cover this later. So let's go ahead and create a function and I'm going to write, helloworld, and when I hit Enter you can see that the REPL is waiting for me to type the function body. So I'll go ahead and do that and I'll just use print and I'll just say hello world and then to close off the function, I need to use the end keyword. And you can see now that I've created a function definition where it says helloworld, generic function with one method. So, I can now call that function. I can just say, helloworld, and you can see the output there. So the REPL environment can also be used to learn more about Julia itself. If I type the question mark character, you can see that the prompt has changed to help mode and I can enter the name of a function. So let's enter the name of the sort function and you can see that that prints out the help info for using that particular function and I can scroll back up through the output and I can learn about a particular function I might want to use in my code. I can also use the shell feature, which lets me type a shell command without leaving the REPL environment. So if I type a semicolon, you can see the prompt change to the word shell and then I can use a shell command like, on Linux anyway, I can type ls. If you're on Windows you might type dir and you can see that I get a directory listing when I do this. So this is pretty useful if you're working in the REPL and you want to execute a shell command, but you don't want to have to lose all the environment context or spin up a separate terminal just to do that. So to exit the REPL, we use the exit function and you can see that I'm now back out to my shell in my terminal environment. Okay, so that's a brief introduction to the Julia REPL environment. During the course if you find yourself curious about a particular subject and you want to experiment further, I recommend just firing up a Julia REPL shell and using it to explore some of the language features.

Contents