Logo

The Data Daily

Learn Julia by Coding 7 Projects – Hands-On Programming Tutorial

Learn Julia by Coding 7 Projects – Hands-On Programming Tutorial

Learn Julia by Coding 7 Projects – Hands-On Programming Tutorial
Logan Kilpatrick
The Julia programming language is used for a lot of really impactful and interesting challenges like Machine Learning and Data Science.
But before you can get to the complex stuff, it is worth exploring the basics to develop a solid foundation.
In this tutorial, we will go over some of the basics of Julia by building 7 small Julia projects:
Mad Libs ✍️
If you have not downloaded Julia yet, head to: https://julialang.org/downloads/ or watch this video:
Download Julia
It's also worth noting that if you are totally new to Julia and want a comprehensive introduction to the language, you can check out this freeCodeCamp article .
Beginner-Friendly July Projects
How to Build Mad Libs in Julia ✍️
In Mad Libs, the user is prompted to enter different types of words. The random words the user enters are then inserted into a sentence. This leads to some pretty wacky and funny outcomes. Let's try to program a simple version of this using Julia.
At the core of this problem, we want to concatenate (or add together) multiple strings so that we go from a sentence with some placeholders, to a sentence with the user input.
The simplest way to achieve this in Julia is with String Interpolation:
julia> name = "Logan" "Logan" julia> new_string = "Hello, my name is $name" "Hello, my name is Logan"
Here we can see that we can insert the name variable we defined into the string by using the $name syntax.
There are a bunch of other ways to do this, like using the string function:
julia> new_string = string("Hello, my name is ", name) "Hello, my name is Logan"
but string interpolation seems the most straightforward and readable in this case.
Now that we know how we are going to set up the strings, we need to prompt the user for their input.
To do this, we can use the readline function as follows:
julia> my_name = readline() Logan "Logan"
The readline function takes a single line of input from the user. This is exactly what we will want to use. Let’s put it all together into a simple example:
function play_mad_libs() print("Enter a verb (action): ") verb1 = readline() print("Enter an adjective (descriptive word): ") adj1 = readline() print("Enter a noun (person place or thing): ") noun1 = readline() print("Enter another noun (person place or thing): ") noun2 = readline() print("Enter a catchphrase (something like 'hands up!'): ") phrase1 = readline() base_sentence = "John $verb1 down the street one night, playing with his $adj1 $noun1. When all of a / sudden, a $noun2 jumped out at him and said $phrase1" print("\n\n", base_sentence) end # Link to source code: https://github.com/logankilpatrick/Julia-Projects-for-Beginners/blob/main/madlibs.jl
Possible solution to the Mad Libs project
In this example, we learned how to work with strings, define a function, use print statements, and more!
As noted before, there are lots of other ways to do the same things we did above. So if you want to find out more about working with strings, check out the Julia docs here .
How to Build a Guess the Number Game in Julia ????
In this game, we will have to generate a random number and then try to guess what it is.
To begin, we will need to generate a random number. As always, there are many ways to do something like this but the most straightforward approach is to do the following:
julia> rand(1:10) 4
The rand function takes as input the range of numbers you want to use as the bounds for the number you will generate. In this case, we set the range as 1-10, inclusive of both numbers.
The other new topic we need to cover for this example to work is while loops. The basic structure of a while loop is:
while some_condition is true do something end
This loop will continue to iterate until the condition for the while loop is no longer met. You will see how we use this soon to keep prompting the user to enter a number until they guess it right.
Lastly, to make it a little easier for us, we are going to add an if statement which tells us if we guess a number that is close to the target number. The structure of an if statement in Julia is:
if some_condition is true do something end
The big difference is that the if statement is checked once and then it is done. The initial condition is not re-checked unless the if statement is in a loop.
Now that we have the basic ideas down, let's see the actual code to build the number guesser. Make sure you try this on your own before checking the solution below. Happy coding! ????
# Number Guessing Game in Julia # Source: https://github.com/logankilpatrick/10-Julia-Projects-for-Beginners function play_number_guess_human() total_numbers = 25 # # Generate a random number within a certain range target_number = rand(1:total_numbers) guess = 0 # While the number has not been guessed, keep prompting for guesses while guess != target_number print("Please guess a number between 1 and $total_numbers: ") guess = parse(Int64, readline()) # Convert the string value input to a number # If we are within +/-2 of the target, give a hint if abs(target_number - guess) rng = MersenneTwister(1234);
Random seeds make it so that random number generators make reproducible results. Next, we need to define all possible guesses:
julia> a = collect(1:50) 50-element Vector{Int64}: 1 2 3 ⋮
We now need to use the shuffle function to make the guesses random:
julia> using Random julia> shuffle(rng, a) 50-element Vector{Int64}: 41 23 13 49 ⋮
Now that we have the random guesses set up, it's time to loop through them one at a time and see if the number is equal to the target the user inputted.
Again, give this a try before you check out the solution below:
# Computer Number Guessing Game in Julia # Source: https://github.com/logankilpatrick/10-Julia-Projects-for-Beginners using Random function play_number_guess_computer() print("Please enter a number between 1 and 50 for the computer to try and guess: ") # Take in the user input and convert it to a number target_number = parse(Int64, readline()) # Create an array of 50 numbers guess_order = collect(1:50) # Define our random seed rng = MersenneTwister(1234) # Shuffle the array randomly given our seed shuffled_guess = shuffle(rng, guess_order) # Loop through each guess and see if it's right for guess in shuffled_guess if guess == target_number print("\nThe computer cracked the code and guessed it right!") break # Stop the for loop if we get it right end print("\nComputer guessed: $guess") end end
Possible solution to the Computer Number Guesser project
How to Build Rock ????, Paper ????, Scissors ✂️ in Julia
If you have never played rock, paper, scissors, you are missing out! The basic gist is you try to beat your opponent with either rock, paper, or scissors.
In this game, rock beats scissors, scissors beat paper, and paper beats rock. If two people do the same one, you go again.
In this example, we will be playing rock, paper, scissors against the computer. We will also use the sleep function to introduce a short delay as if someone was saying the words out loud (which you would do if you played in person).
The sleep function takes in a number that represents how long you want (in seconds) to sleep. We can use this with a function or a loop to slow things down as you will see in this game.
sleep(1) # Sleep for 1 second
Let's also explore a function I found while writing this tutorial, Base.prompt , which helps us do what we were previously using readline for.
In this case, however, prompt auto-appends a : to the end of the line and allows us to avoid having two separate lines for the print and user input:
human_move = Base.prompt("Please enter ????, ????, or ✂️")
We will also need to use an elseif to make this example game work. We can chain if , elseif , and else together for completeness. Try putting together the if conditionals, prompts, and sleeps to get the desired behavior, and then check out the code below:
Gif of playing Rock Paper Scissors in the Julia REPL
# Rock ????, Paper ????, Scissors ✂️ Game in Julia function play_rock_paper_scissors() moves = ["????", "????", "✂️"] computer_move = moves[rand(1:3)] # Base.prompt is similar to readline which we used before human_move = Base.prompt("Please enter ????, ????, or ✂️") # Appends a ": " to the end of the line by default print("Rock...") sleep(0.8) print("Paper...") sleep(0.8) print("Scissors...") sleep(0.8) print("Shoot!\n") if computer_move == human_move print("You tied, please try again") elseif computer_move == "????" && human_move == "✂️" print("You lose, the computer won with ????, please try again") elseif computer_move == "????" && human_move == "????" print("You lose, the computer won with ????, please try again") elseif computer_move == "✂️" && human_move == "????" print("You lose, the computer won with ✂️, please try again") else print("You won, the computer lost with $computer_move, nice work!") end end
Possible solution to the Rock Paper Scissors project
How to Build a Password Generator in Julia ????
WARNING: Do not use this code to generate real passwords!
In the age of endless data breaches and people using the same password for every website, having a secure password is important. In this example, we will generate an arbitrary number of passwords with a variable length.
Given that this could take a long time, we will also add an external package, ProgressBars.jl , to visually show the progress of our for loop. If you have never added an external package before, consider checking out this robust tutorial on why the package manager is the most underrated feature of the Julia programming language.
To add a Julia package, open the REPL and type ] followed by add ProgressBars. After that, as we did with the Random module (note we did not need to add it since it is part of base Julia), we can say using ProgressBars to load it in.
The main new idea we will introduce here is vectors / arrays. In Julia, we can put any type into an array. To create an empty array, we do:
password_holder = []
and then to add something, we use the push! function as you will see in the below example.
As mentioned before, we will use the ProgressBars package to show progress on the screen. Note that Julia is so quick that it likely won’t show you the loading screen unless you manually slow things down with a sleep function call or a high number of passwords. Check out the README for an example of using this in practice.
As with the other example, try to put some code together before you dissect the example below:
# Generate Passwords in Julia # Source: https://github.com/logankilpatrick/10-Julia-Projects-for-Beginners using ProgressBars using Random # WARNING: Do not use this code to generate actual passwords! function generate_passwords() num_passwords = parse(Int64, Base.prompt("How many passwords do you want to generate?")) password_length = parse(Int64, Base.prompt("How long should each password be?")) # Create an empty vector / array password_holder = [] # Generate a progress bar to show how close we are to being done for i in ProgressBar(1:num_passwords) # Add the new password into the password holder push!(password_holder, randstring(password_length)) sleep(0.2) # Manually slowdown the generation of passwords end # Only show the passwords if there are less than 100 if length(password_holder)

Images Powered by Shutterstock