We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Introduction to Programming for Business Analytics - Lecture 1: Fundamentals

00:00

Formal Metadata

Title
Introduction to Programming for Business Analytics - Lecture 1: Fundamentals
Title of Series
Number of Parts
22
Author
License
CC Attribution 4.0 International:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language
Producer
Computer animationMeeting/Interview
Computer animationMeeting/Interview
Computer animationMeeting/Interview
Computer animationMeeting/Interview
Meeting/InterviewComputer animation
Meeting/InterviewComputer animation
Computer animation
Computer animation
Computer animation
Computer animation
Computer animation
Computer animation
Computer animation
Computer animationMeeting/Interview
Computer animation
Transcript: English(auto-generated)
Hi, welcome everyone to the first lecture of the Introduction to Programming for Business Analytics class. In today's lecture we're going to talk about the fundamentals of programming and in particular we're going to do this using the Julia programming language.
First we're going to have a general definition of what is a programming language and then we're going to look at what makes a programming language a language to begin with. We're going to do this, as I mentioned, using the Julia programming language and specifically we're going to look at how we can perform some basic mathematical operation
using the Julia language and then we're going to look at some assignment and assignment statements. These are situations where we assign some values into variable names and then we're going to move on to talk about some elementary functions. Elementary functions can be either user-defined, meaning that we define them ourselves,
or built-in, meaning that they are given by the Julia language itself and then we're going to bring everything home by looking at everything we've discussed in the context of a language. Some of these might seem abstract to you at first, but as we progress through the lecture
I'm sure they're going to become clear. Alright, so let's go ahead and get started. So what is programming? In generic terms, programming is the process of designing or building an executable program. So let's parse this definition a little bit further. So what is a program? A program is a sequence of instructions that specify how to perform computations.
And in general, we can say that every program, no matter how complicated it is, can be broken into an input. This is something that we tell the computer, either by typing on the keyboard or by using the mouse and so on. And then there is the output, which is what the computer provides to us as information
through the screen, through the speakers and so on. And then there is maths, which is going to be the focus of today's lecture, meaning that we're going to perform some maths operations. And then there's conditional execution and repetition.
Conditional execution is where we check for certain conditions to be satisfied before we implement something. And then there's repetition where we repeat performing a task multiple times. As I mentioned in today's lecture, we're going to focus on the maths part and in future lectures we're going to look at conditional execution and repetition.
Alright, so we've just established the definition for what programming means and to complete the definition, we're going to look at why programming languages are usually referred to as a language in the first place. So a language is a structured system of communication and by that we mean a system typically has a component.
These are usually the vocabulary and the system has a structure and this is usually the grammar of that language. So whenever you think of any language of any sort, you have two components, which makes this system of communication, these are the vocabulary,
which is the elements that you use and then the grammar, which is the structure in which you arrange those elements. Languages have traditionally fallen into two camps. One of them is called natural and the other one is called formal. Natural languages are languages people speak,
for example English, Spanish, Mandarin, etc. And then there are formal languages and these are languages designed for specific applications. So they're designed by people to do something specific like math notations and chemistry or chemical symbols and so on.
Typically, whenever we talk about natural languages versus formal languages, we distinguish them by certain features. So natural languages tend to be ambiguous, redundant and expressiveness. Why that we mean is not everything is necessarily directly understood
by just looking at the word. So sometimes you have to infer something by the context. This is what we mean by ambiguity and redundancy. And then there is also the element of expressiveness whenever you think of a natural language. So by expressiveness we mean words sometimes are used
just for their poetic nature and so on. And then on the other hand, when you look at formal languages, formal languages tend to be precise, concise and prosaic. By that we mean all we care about whenever we think of formal languages is the exact meaning of the words that they are trying to convey.
So just a big picture is that formal languages tend to be more precise when it comes to its comparison with natural languages. This is important because programming is a formal language. So as we mentioned or as we've just established, programming is a language and programming is a formal language to be specific.
And that formal language is designed to communicate instructions to a machine and in particular a computer. All right, so let's talk about the Julia programming language specifically. So Julia is a high-level programming language characterized by its efficiency and high performance.
For the purpose of illustration or for the purpose of explaining what a high-level programming language is, I'm just going to employ the abbreviation HLPL here. So loosely speaking, a high-level programming language allow programmers to communicate with computers in a user-friendly context.
What does that mean? So whenever we think of the instructions that we give to the computer, these instructions are typically written in something called machine code. Machine code, which typically is written in a sequence or is a bunch of zeros and ones, is not very easy to understand.
In fact, it can be difficult to work with. So high-level programming language provide a vocabulary or a syntax that is close to that of natural languages. By natural language, if you remember from the previous slide, we said that these are languages that are similar to the one we speak in our daily lives, such as English and so on.
And the goal of high-level programming language is to provide a vocabulary or a syntax that is close to that of natural language. And whenever we have a high-level programming language and syntax that is similar to that of natural language, this makes it easier to us to communicate instructions to a computer instead of just writing a bunch of zeros and ones.
So the first step to write a program in Julia is to download the language. So here's a hyperlink that, if you click on it, will redirect you to that page. I'm not going to do that because I already have installed the Julia language, and you can refer to the installation guide on the course website as my first step, my step guide on how to install the programming language.
All right, so after installing the software, the first step to work on Julia is to open the Julia REPL, and I'll tell you what REPL means in a second. But what I want to point out is that on the left side here I have this notebook, and then on the right side I have the REPL.
And as you can see, the screenshot here might look slightly different. This is because the screenshot is taken from a Mac OS, whereas here I'm using a Windows. So the actual REPL that I'm using here where we have the cursor blinking is using a Windows. And this is just to say that the first lines might be different for you, but that is not a problem.
So let's explain what REPL means. So the phrase REPL stands for Redeveloped Print Loop, which refers to an interactive programming interface that does four things. So it reads, that's where read comes from. It reads the user input in the form of an expression.
We'll explain what an expression means in a few slides, but let's just focus on the fact that it reads the user input. And if you remember from the first few slides, we said that any program can be broken down into five components, and one of them was an input through the keyboard or a mouse.
So in this example, we're going to type in this guy, which is called an expression, which is 40 plus 2. So let's do that. So here, as you can see here, I have the cursor blinking, so I can just type in 40 plus 2. And in this case, the REPL reads this expression,
and then it evaluates the red expression when you press the Return key or you hit Enter. So when I hit Enter, this guy gets evaluated, and by evaluated, we mean that it calls the plus operator on the 40 and the 2, yielding the value 42.
This brings me to the third point, or the third thing that this REPL does, which prints out the user evaluated value. And as you remember, we also said that any program also has output. In this case, that's going to be 42, which is the value of adding 40 to 2. And last but not least, it loops back to the red state.
So here, as you can see, the cursor is blinking again, meaning that it's in the state where I can type in new expression, and that are going to be read, evaluated, printed, and then we loop back again to the starting point. This is explained here in this plot or this kind of flow chart
that explains that the expression gets evaluated, which gives us a value, in this case 42, and then it gets printed as the output, and then we loop back to the red state. So before we move forward, as you can see here, we've changed the setup slightly where I have the notebook on the left side
and the REPL on the right side. And what I want to do is actually just to highlight or to indicate that there is a difference between the REPL and the notebook, or to be more precise, to point out the fact that one mistake that beginners often make is that
they confuse the notebook for a programming language. And what I want to point out is that the notebook is not a programming language. It's what is usually referred to as IDE, which stands for Integrated Development Environment, where we can type code, but that's not only Julia,
so we can type code using other programming language. We're actually going to do this in the second half of this course. But we can also type text. As you can see here, we're just typing text. However, the REPL, the Julia REPL, is exclusively for writing Julia code as well as exclusively for writing code,
so we can't write text on it. All right, so just to kind of show you that we can do the same here on the notebook, although we will not do this for the rest of this first class, is that we can execute this cell. You can either do this by hitting this Run button,
and as you can see here, it shows us the value 42. And there's also a shortcut where you press and hold the Shift and then you hit the Return key, which also does the same thing. As you can see here, when I did that on the keyboard, it also gave the value 42.
I've used a couple of terminologies in the previous slides. For example, I said the word expression and operator without explaining what they actually mean, so let's explain what they both mean. An expression is a syntactic entity in a programming language. In this case, that's going to be 40 plus 2.
This syntactic entity could consist of something called literals or constants or operators. So what is a constant? A constant is a value that cannot be altered during the program execution. For example, 40 and 2, for Julia, they will always be 40 and 2.
This is something that we cannot change. We cannot change their value. And then we have operator, and an operator is a construct defined to perform a specific task. So for example here, this symbol here, the addition symbol, is designed or defined to perform addition by adding two values to each other.
So the addition operator is actually not the only operator that we can use in Julia. There are several types of operators that programmers use. So for example, the arithmetic operator, such as the addition that we've just discussed, but we also have subtraction and multiplication. These are for performing a standard or the arithmetic
that we're used to from maths classes or mathematic classes. And then there are also something called comparison operator as well as logical operator. As I mentioned, in this lecture, we're going to mostly focus on the arithmetic operators, so plus the subtraction and the multiplication, but it's useful to just have the big picture in mind.
So comparison operator compare two values. So for example, strictly greater than, which has this strictly greater than symbol, and then there is also strictly less than, which has the same symbol as in maths. These, as I mentioned, are not the only one. There are other ones which we will cover in future lecture. There are also logical operators such as the and, the or,
and the negation symbol, which are indicated by and the symbol between parentheses respectively. These are not the only way in which, this is not the only way in which we can classify operators. So operators are also distinguished by the number of operands they operate on.
So what do I mean by that? So we have, in the previous example, looked at the addition operator. This is actually called a binary operator. So I'll start from the second one because this is the one that we've already covered. So we have the binary operator, and a binary operator usually operate on two operands. So for example, the plus symbol has one element to its left
or something to its left and something to its right. These are called operands, and because it has two operands, this is called a binary operator. With the same token, there is a unary operator which operate on a single operand. So for example, we have the subtraction symbol.
In the example of e, when you raise e to the power of minus x, as you can see here, the minus operates on the x only. So it only has one element to its right. All right, so we've said that the focus on today's lecture is going to be basic math operations. We've looked at an example for addition, but now we're going to look at other examples
for subtraction and multiplication. So one thing we can do is we can, for example, subtract 43 minus 1. This is also going to give us 42. We can also do 6 multiplied by 7. That's going to be equal to 42 as well.
So the subtraction is performed using the minus symbol, and then the multiplication is performed using the asterisk symbol. I earlier briefly also used the word literal,
and to explain what literal means, so numbers such as 1, 2, and so on are referred to as literal or numeric literals to be specific. So what is a literal? So a literal is a notation for representing a constant value in a computer program. One useful feature of Julia is that we can ask the REPL
or the notebook actually for help by using the question mark symbol followed by the operator, for example, that we are trying to figure out its job. So in this example here, we're going to type in the question symbol followed by the caret symbol
and then hit the return key and see what happens. So let's do that. So when I first type in the question mark without actually seeing the question mark, you'll see that Julia switched to this mode, which is called the help mode, and then once I type in the caret symbol,
and then I hit the return key, you'll see that it gives me a lot of information here. A lot of it is not going to be understood for us right now or not as important for us at this moment. However, we can just infer the basic meaning
of what this caret symbol does, and it's an exponentiation operator. So here it also gives you an example. So when you raise 3 to the power of 5, you get the value of 2, 4, 3. As I mentioned, you can also do this in the Julia or the Jupyter notebook,
but we're not going to do this for this class. All right, so let's look at an example where we can use this exponentiation operator or the caret symbol in a more practical context, and here we have an example for calculating a compound interest rate or a calculation using a compound interest rate.
So suppose you took a loan from the bank of 15,000 euros with an annual interest rate of 4%. So how much you will owe the bank after five years. So if you break down actually the calculation, so you start with 15,000, and then what you're going to owe the bank after one year
is the amount that you took as a loan multiplied by the interest rate, and then you add that to the entire amount. So in this case, that's going to be 15,600. And then in the next year, what you're going to owe the bank after two years is going to be the amount that you start with, in this case, the amount that you were carrying over
from the previous period, plus the interest rate multiplied by that same amount. So in this case, that's going to be 16,224, okay? So this guy actually keeps repeating all the way until the end, and then the amount that you owe after five years, if you perform the same calculation,
it's going to be 18249.8 approximately, okay? So if you break down this, one thing you can do is that you can actually factor out the amount that you pay at the beginning or the amount that you have here corresponding to the money that you were carrying over from the previous period.
So here in this case, that's going to be 15,000. If you factor out this guy from each and every component, what you're going to end up with is one, because here we have one plus R because that's the interest rate. And the same thing actually multiplies by itself for five times.
And five times, as we know, is the amount of years that I'm going to count before I pay the bank the money that I owe. In this case, we can write everything down in one compact formula, where we say the amount or the debt after T years, T here being the period or the number of periods, equals to A, which is the amount
that we owe at the beginning of every year that we factor out here in this case, multiplied by one plus the interest rate and then it's raised to the power of T, okay? So as you can see here, we can write down this guy explicitly, which we're going to do together in the form of an expression that we're going to evaluate using the REPL.
So I'll type 15000 and then the asterisk symbol and then I have an open parenthesis and then I type in one plus 0.04 and then I close the parenthesis and then I type in the character symbol and then I raise that to the power of five.
So this is actually a direct copy paste from the formula that we've extracted here. So we have 15000 multiplied by the amount that we start with one plus 0.04 and raised to the power of five. And then I hit the return key and as you can see here, the money that we get after five years
is 18249.8 approximately. So if you round this number, you'll get the same. One of the most powerful features of a programming language is the ability to manipulate variables. So what is a variable?
So a variable is a name that refers or is bound to a value. So if we look at the previous example that we had, we have this formula for the compound interest calculation or the debt that we pay after T years. So we use the variable name D or use the notation D to indicate
to the amount that we're going to pay after five years or after T years, A to the amount that we took as a loan and then R for the interest rate and T for the number of periods after which we're going to pay the bank back. So what I'm going to do here is I'm actually going to define a variable for each one of these guys. And I'll start with the A
because D is the final thing that we will calculate. So A is going to be 15000 and then R is going to be 0.04, that's the interest rate and T is going to be five. And then I'm going to define something here which I'm going to call D equals to A and then I just multiply by one plus R raised to the power of T exactly as it is the case in the formula.
So let's do that together. So the first thing I will type in here A equals to 15000 and then I hit the return key and then I say R equals to 0.04 and then I say T equals to five and then I hit the return key
and then I have all of these variables already defined and what I'm going to do is I'm going to assign the value of calculating A multiplied by one plus R and then I close the parenthesis and then I raise everything to the T and what I expect is a number that looks the same
as if I were to type everything down explicitly. And what I did here is I assigned the amount that I'm calculating to a new variable called D. So when I hit the return key here you'll see here I will get a number that looks exactly the same as the one that we've calculated here.
So before I explain why we're using these variables or what's the purpose behind using these variables one thing I want to point out is that Julia also has built in constants which are variable names with a pre-specified value. So one of them for example is the value of pi
which if you type here in the RAPL if you type in pi and then you hit the return key you'll see here it tells you the value of pi but here it uses the Greek notation is equal to 3.14 and so on. So this is an irrational number we don't have to worry about that now
nevertheless we just need to keep that in mind as well is that Julia already has some built in constants that we can use directly for calculation. So let's go back to the assignment statement that we've just introduced. So I said that we're going to assign the value of the calculation to a variable called D.
But what does assigning mean or what is an assignment statement? An assignment statement creates or reuses a variable and gives it a value. So an assignment statement for example T equal 5 consists of the following. It consists of actually three things a variable or a variable name where the value is stored
in this case it's going to be T or in this example and then there is an expression that can be something that contains operator or some arithmetic operations or it can just be a value like 5. And then lastly we have this operator which is in this case the equal symbol it's called an assignment operator
and what it does is that it binds the name T of the variable to the value of the expression. So here we're binding the value of the expression 5 or of evaluating the expression I should say 5 to the name T. But now to the important question
which is what is the benefit of using variables instead of writing down everything explicitly. So here I perform the same operation one time I wrote down everything explicitly I wrote down the numbers explicitly and then here I wrote down only variable names. So the first actually part is called hardcoding this is whenever you write things down explicitly.
And to see why hardcoding is not always the best strategy for writing code we have an example or we have a hypothetical situation. So suppose the bank offers you an interest rate of 3% if you repay the loan in four years
so we have the same example that we're working from before but now we've just added this hypothetical situation where we are assuming that the bank is offering us a lower interest rate I should say of 3% if we repay the loan back in three years. And what we want to do is to calculate the amount that we will save if we take the offer.
So if I were to proceed with the way that I originally wrote the calculation by hardcoding meaning that I type in 15,000 multiplied by one plus zero four raised to the power of five this is what I will do. So let's actually do that together so I'll type in one five zero zero and then I have the asterisk symbol
and then I open the parentheses one plus point zero four and then I close the parentheses and then I raise this guy to the power of five and then what I want to do is I want to actually subtract one five zero zero multiplied by one plus point zero three that's the new interest rate
and then I raise the same thing to four. So remember the condition here is that I have to pay the loan back in four years instead of five years and then in return I will get a cheaper interest rate of 3%. And to calculate the amount that we will save if I retake the offer I calculate the original amount that I have here
minus the new calculation that I've just wrote down. So once we've done that here I hit the return key I get the value one three six seven point one six and so on. So now let's look at how we can benefit from
defining variables or for performing the calculation using variable names and assignment statements instead of hardcoding things. So one thing that is familiar to us from this calculation here is the first half. This is actually something that we've already done. So we don't need to write down everything again.
So instead of having to write down everything again what I'm going to do is I'm just going to start by subtracting this guy here from the variable where I stored the value of this guy. And remember when we performed the calculation for the first half we stored everything here in a variable called D.
So if you go back to the Julia REPL and then you type D and then hit the return key you'll see that Julia still remember from the first calculation that D equals eighteen two four nine point eight approximately. And what I'm going to do instead of actually writing down everything explicitly from scratch
I'm just going to subtract the amount of the calculation based on the discounted rate or the new interest rate and I'm going to subtract that from the D variable that I have. And just to sort of be consistent I'm going to assign the value of evaluating this guy to a new variable name which I'm going to call saving in this case.
So what we have here is a new variable called saving this is just where I want to assign the new variable name or the new value and I'm going to type in D that's the old variable minus A that's the variable where I stored the amount of money
remember I also had that here which is fifteen thousand and then what I'm going to multiply that by is one plus R so instead of writing down point zero three I'm just going to subtract point point zero one because point zero point zero four minus point zero one
is what's going to give me point zero three and then I'm going to close the parenthesis here and then I raise everything to the power of T minus one so T minus one here and then I close the parenthesis and as you can see here
what we have is the original amount that we already stored in the variable minus A multiplied by one plus R minus point zero one raised to the power of T minus one you hit the return key and indeed you see here that we have one three six seven point one six one etc. which is very similar to writing down everything explicitly
nevertheless if you contrast or if you compare the two you will see that writing this down is actually less easier than writing down the first statement because in this part here I've actually repeated a calculation or something that I've already done I don't need to do that I could just use one letter here
where I stored the original value or the value of calculating this guy here and then I only type the new part here in which case I've reused the value that I've used before so to summarize the benefit of using the variable instead of writing down everything explicitly is the fact that
we're actually able to reuse the variables as place holder or places where we store values or certain values alright, so before we move on to the next part one thing that I want to indicate is that what we did here whenever we performed the calculation was that we said D minus A multiplied by one plus R
minus point zero one and then raised to the power of T minus one clearly I could have written something like instead of R minus one I would just say this guy here and then I just subtract or add point zero three that's the new interest rate and then instead of typing
raised to the power of T I just type in four that's the number of periods in which after which I'm able to get the discount and if I hit the return key I also get the same value so the first thing to note by this observation or from this observation is that there are multiple ways in which we can perform
the same operation this leads me to the next point which is something called reassignment, so one thing we can do is that we can actually reassign the values R and T completely to new values by letting R equals point zero three and T equals four, so instead of
having to write down A multiplied by one plus R minus point zero one raised to the power of T minus one I can directly say R has a new value, in this case it's going to be point zero three or that's the modified R and then T also has a new value in this case that's gonna be four, that's the new
period and then if I go back to the original calculation that we performed here for the saving and then I just say use T directly and then use R directly, that's very similar to the original way we calculated D and then I hit the return key, you'll see that
I have one three six seven that is the same number indeed this is called reassignment because we've reassigned the values of R and T to new values this is not the only way that we can perform reassignment, alternatively we can use the current value of R and T to define their new values by letting R equals R
minus point zero one so if I say for example let's actually write down R as it used to be so R is point zero four but now I wanna make R equals to point zero three, so I can say R equals to its old value, the old R minus point zero one
and then I hit the return key and you'll see here R is now equal to point zero three okay, similarly we can do the same thing for T this type of reassignment is referred to as an update reassignment we say an update reassignment because we're actually updating the old values
of R, we're updating the values of R and T using their old values and there's actually a short hand for the update reassignment where instead of writing down R equals R minus point zero one we can just say R directly we just say minus equals
point zero one so essentially what we do is we ignore or we don't write the R here because we know that R is gonna be subtracted from itself, so I'm just gonna say subtract from R this number here, which when you hit the return key you'll see that R is going to equal to
point zero three and the same thing for T as well, okay? and just to bring everything in one picture if you have other operators such as the plus, the multiplication you can do the same thing for them so you can reassign values by saying for example T multiplied by two and so on, and if T has
a value, let's say four, and then we say T multiplied equals by two, that's gonna be equal to eight and so on okay, so here just a summary of doing the same thing using the same calculation, I'm not gonna execute the cell as I mentioned because we wanna use the Julia REPL exclusively for this lecture but you can try that at home
as well, or yourself this is the last point I mentioned about using other operators or other arithmetic operators using the shorthand for the update reassignment alright, so let's level up suppose you want to calculate how much you're going to owe the bank if you postpone
repaying the loan for an additional one three or five more years remember that the original deal we had with the bank is that we're gonna repay the loan after five years, so to say that I'm gonna postpone repaying the loan by one year means that I'm gonna repay the loan in six years and to say that I'm gonna postpone it for three or five years, meaning that means that
I'm gonna repay the loan in eight or ten years respectively one thing we can perform one way to perform this calculation is that we can write down, we can hard code everything as we did in the very first time we attempted to calculate the amount that we're gonna pay the bank
after three years, so you just write down fifteen thousand multiplied by one plus point zero four and raised to the power of six, you do the same thing again, but now you change the exponent value to eight and then you do the same thing again and you change the exponent value to ten, okay so for the interest of time I will not do this here, but you can do this as an exercise at home
and you might want to do that and keep track of how long it will take you to do that, and when you do that, you will notice that it's not necessarily the shortest thing that you will do all day but when you reflect on this fact, one thing you can notice
is that there is some repetition here between all three expressions that you're trying to write down, so if you look at the first, the second and the third expression, you will notice that everything from fifteen thousand all the way up to the current symbol is exactly the same in the first expression, in the second expression and in the third expression the only thing that is changing
is the value of the exponent, okay and in order to avoid this kind of repetition one thing you can do is you can define a function that calculates the value of fifteen thousand multiplied by one plus point zero four raised to some variable name
which is here in this case it's gonna be t, and this function is going to tell you what the value or how much you're going to owe the bank after any amount of years that you put here, in fact you could even put fractional values but this is not necessarily what we're interested in in this example, okay, nevertheless
the point is that instead of writing this whole part over and over again or everything from fifteen thousand to the current symbol over and over again, what we're gonna do is we're gonna write down a function that captures this part that repeats and we're gonna parameterize that function by a value or by a variable name
called t and that variable name is gonna act as a way to change the value of the time at which we want to repay the loan back to the bank, okay so in mathematics a function is a rule that defines a relationship from an input
to a possible output, so for example if you look at fx equals x square this is a function the input here is going to be a value of x the input goes here, so let's say for example we give it two, the output is going to be two squared, that's gonna be four and so on and to define a function as Julia or the process of defining a function
in Julia is similar to defining variables so what I mean by that is that we specify the input and the output by a name, so when we define variable for example we said t equals four, we said r equals 0.03 and so on and we do something quite similar to this whenever we define variable or whenever we define function, so we
specify the name of that function and then later on we call that function by its name whenever we want to use it so in this example when we define a variable name we use that variable in this expression by using its name so instead of saying I want to use the value four, I just said I want
to use t which we know that represents the value four so let's actually do that together in the Julia REPL so we can say fx that is defining a function, we type f, we're going to give it the name f and then we type in parentheses or open parentheses and then we type x here, that's going to be
the argument or the parameter of that function and then we close the parentheses and then we hit or we type in the equality or the equal operator and then we say x is going to be raised to the power of two and then you hit enter so far nothing happens, it just Julia says f is a generic function
with one method, let's not worry too much about that for now but what is important is that we don't have any value right now that tell us whether this function actually does what we want it to do or not this is because we have not yet used a function we've just defined a function and as I mentioned if you want
to use the function or as I mentioned if we want to use the function we have to call it by its name and we have to tell it what the value of x is going to be, so in this example here we're going to give it the value two to evaluate that to evaluate two squared, so we're going to say f and then we open the parentheses and then we type two, we close the parentheses
and then we hit the return key as you can see here we have four just to test it using another value, let's say five for example open the parentheses, close the parentheses and then hit the return key and you see that it indeed does the job that we want it to do which is give us the value of x squared
and before we go back to the original example, the compound interest example for other values of t, let's break down the definition of a function so the definition of a function consists of the following, f that is its name, that's the name we use to call that function and then we have these parentheses
they are called delimiters and then x is referred to as a parameter or input of the function but there is a subtle distinction here so whenever we're defining a function we call that x a parameter so that function is parameterized by the value or by the variable named x, however when calling the function the value
of x is assigned to or the value assigned to x is referred to as an argument of the function so here two is an argument of the function and five is an argument of the function so just to summarize the distinction is that x is called a parameter when defining a function and it's called an argument when
calling the function so let's go back to our original example and remember that what we're trying to do is we're essentially trying to rewrite down the original compound interest rate formula but now in the form of a function where it's going to be parameterized by the variable t so we can do that directly and we're going to give that function
the name debit just to stand for the fact that this is the amount of money that we owe the bank after t years and we're going to specify or parameterize that function by a variable named t and then we're going to set it equal to
1500 that's the number of years multiplied by we open the parenthesis 1.04 we close the parenthesis we raise that to the power of the variable t so everything here is in numbers except the only variable by which we are trying to parameterize that function and now if we hit the return key
we say that we have debit with a generic function with one method as I mentioned we will not worry about that for now but we can call that function now by calculating the values that we want to evaluate so let's just make sure that the function works as we want it to do so if you remember from the first example
when we evaluated the expression for 5 years we had 18249.8 approximately so if you go back here and then we just type 5 we close the parenthesis and then we hit enter you'll see that indeed we get the same number and now we can use that function
and now with a different value so in this case postponing the repayment of the loan for 1 year we hit the return key we get a specific number corresponding to that value and then we do the same thing here but now we type 8 and then we hit the return key and then we get this value and then we do the same thing
but now with a value of 10 and then we get these values so if you go back as a sanity check and evaluate these cells or cells in the julia or in the jupiter notebook you'll see that the first guy here indeed corresponds to this number the second guy here indeed
corresponds to this number and the second the third guy here indeed corresponds to evaluating this expression alright so just a quick note as you can see here in all three expressions here we used we wrote down the argument of the function as a numerical literal so a value like 6, 8 and 10 but
this does not have to always be the case so we can pass an algebraic expression so in this example here we have instead of typing down debit 6 we can type debit and then we open the parenthesis and we type 3 plus 3 close the parenthesis and then you hit the return key you'll see that indeed gives us the value corresponding
to 6 and then we can do the same thing here by passing a variable name so we can say something like t equals 8 in this second example and then in the next line we say debit and then we pass to this guy the value of t and then we close the parenthesis and then you hit the return key
you'll see that this guy here indeed corresponds to evaluating the function or passing 8 as an argument of that function and another thing we can do is for example we can use a compound expression by a compound expression what I mean is that we can pass to the function
and value that gets evaluated as or the output that we get by evaluating another function so in this example we have a function called double x which essentially simply just multiplies the value of the parameter by 2 or the argument that we pass here when we call the function
2 and then we're going to evaluate that as 5 so when we multiply by 2 it's going to be 10 and then this should give us hopefully the value that corresponds to evaluating debit equals 10 directly so let's do that together so I have this new value or this new function which I give it the name double and it takes one argument
in this case x and then it's equal to 2 multiplied by x and then here I just hit enter to define to complete the definition of the function and then here I go back to this guy, debit here and instead of writing down 10 explicitly I will just say double 5 here
and then I close the parenthesis and then I hit return key here and as you can see here is I have the number corresponding to evaluating the value of 10 directly. So at first glance it might not seem so obvious how powerful functions are or how powerful functions could
be by just looking at the examples that we just did where we parameterized the function by one parameter which is in this case was t or the number of periods after which we pay the loan back to the bank and to see how powerful functions can be let's take a look at another example or another hypothetical situation where we
suppose that another bank is giving us 4 different offers each offer differs from the other by the value of the interest rate and by the loan period or by the time after which we have to pay the loan back to the bank. So you see here cheaper interest rate
3% for example correspond to longer periods and so on. And we're going to use the same value which is 15,000 to evaluate the amount that we're going to repay the bank after the loan period. So to evaluate the best offer, one thing we can do is that we can define a function that takes multiple arguments.
But why do we need to do that? So if you go back to the previous example, the only thing that was changing between different expression was the value of t, was the amount of time after which we're going to repay the loan back to the bank. This is indeed the case in this second hypothetical situation but what is also changing now is
the value of the interest rate. So instead of having to define a function for every new interest rate, for every different interest rate what we can do is we can define or we can use the same function that we had from last time except now instead of passing the value of the interest
rate explicitly as we did here by saying 0.04 we're going to also pass another parameter in this case that's going to be r to just indicate that it corresponds to the interest rate and then call that function using the different values for which we want to evaluate the interest rate. And the way to do this is very similar
to the function that we had except now we're just going to give it a different name, we're going to call it debit modified just to make a distinction between the one that we had from last time and then we're going to pass in the r parameter as well. And to indicate that r is different from t or rts for example is not one variable name, we're going
to insert this comma here to separate the two. And then the rest is very similar except as I mentioned we're just going to use the value of r instead of writing down the number explicitly. So let's do that together. So I'll just say debit underscore modified here and then I have
it takes now two parameters in this case r comma t, I close the parenthesis I type equal and then I say one five zero zero and then multiplied by open parenthesis one point or one plus r and then
close the parenthesis and then I just say that this guy is raised to the power of t. So everything here to the right hand side is known to that function. So what I mean by that is fifteen thousand is a constant value that Julia recognizes directly one is the same as well for one. Our
anti r variable names however Julia know that whenever I call that function I'm going to tell it what r is and what t is. So everything on the right hand side here is actually recognized to Julia with respect to that function. And then we can just hit the return key and then I have completed the definition of that function. And now we can directly
evaluate the function by just calling it using its name. So we'll just say debit modified and now in the first example we're going to use point zero three and then in the same way we defined it whenever we call it we pass the argument by separating them using the comma.
So in the first example we have point zero three eleven and then we close the parenthesis we hit the return key it's the corresponding value we do the same thing now instead of point zero three we're just going to say point six and then here we evaluate eight and then hit the return key we get this value and then in the
third example we have point zero nine and then the amount of over the period is going to be five hit the return key same for the last one except now we have twelve percent and then the amount of years or the loan period is two and then we hit the return key now you can
clearly see that doing this is much much easier than writing down the same expression or hard coding everything by writing down all the numbers explicitly okay? And just to give you sort of a terminology for what this comma is called so the comma separating the two parameters r and t
is also called a delimiter as is the case for the parenthesis. Alright so if we look back at all of the functions that we've used so far whether it was the debit function and the double function or the debit modified function all of them they share one characteristic
which is that or one of the characteristics they share I should say is that they were defined by us we call these user defined function in the next few minutes we're going to look at another class of function which is called built in and in particular we're going to look at some of the functions such as the print and the print lin function. So what are the built in functions? The built
in functions are functions that are defined by the Julia language itself. So Julia has many built in functions which are essential for writing a program and a very useful built in function is called print which creates a print statement so a print statement is an instruction that causes Julia to display
a value on the screen so in this example here we have we signed the value of debit modified when we pass 0.04 and 0.05 to it as argument to a variable called d and then we're going to just say print the value of d and this
might not be very obvious to see why we need to display the value on the screen and since the repo already does that but their usefulness is going to come in handy in future lectures or it's going to become clearer I should say in future lectures. But let's look at the example
where we say d equals debit underscore modified and then we open the parenthesis and we type in 0.04 and then comma and then 5 and then we close the parenthesis. And what I'm going to do here is I'm going to add a semicolon at the
end and what the semicolon is going to do is actually it's going to suppress the output so when I hit the return key you'll see that I no longer see the output the julia displays whenever we hit enter. However when I type in print and then pass to that function
the variable name d and then I hit the return key you'll see that I can indeed see the value of evaluating the debit modified function without suppressing the output using the semicolon. So these guys are both indeed the same. And here in this example we
also add a new variable to d so without for example calling or without changing the argument that we pass to debit modified I'm just going to say d plus equals 1000 so I use an update reoperator and then I
hit the return key and then I do the same thing by typing or evaluating the expression print and d and then you'll see here I indeed get the same number. So a very similar function to print is called printlin which is short for print line and unlike the print function printlin adds
a new line at the end of the displayed value. So let's look at an example where the distinctions between the two become clear. So here we're going to have two variable names. One of them is named d1 and the other one is named d2 and we're going to use the debit modified function for one of them. We're going to
pass in 0.04 the other one we're going to pass in 0.04 6 and the other one we're going to pass in 0.08 and 3. And whenever we use the print function to display their value at first we're going to say print d1 and print d2 and then at second time we're going to say printlin d1
and then printlin d2 and then hopefully their distinction will become clear. So let's do that together. So the first step here is to say d1 modified and d1 equals debit modified and we pass in 0.04 6 here and like I just mentioned I'll suppress the output so we don't see it already
and then here I have d2 and then I pass to this guy here 0.08 and 3 and then we hit the return key. So I have already defined the two variables but now one thing I'm going to do I'm also going to say d1 here and without going
to a new line I'm going to type directly in the same line by adding the semicolon so I add the semicolon without hitting the return key I'll just say print d1 and then I do the same thing and then print d2 and then I hit or type in the semicolon
and then I hit the return key so as you can see here typing these two guys created, there was no space between print d1 and then print d2 so the two numbers were sort of mushed together but let's do the same thing again but now instead of using print I'll just use printlin that's the other function
that we want to take a look at so here we have printlin d1 and then I press the semicolon or type in the semicolon then I do the same thing printlin but now d2 I close the parenthesis and then I type the semicolon again then I hit the return key and as you can see here
on the one hand looking at the expression or the output display by using the printlin function is cleaner to see because I have a space after this five here so after the five that corresponded the calculation or the value of d1 and the second thing is that there is a line there is a new line so the
second expression gets evaluated in a new line so as the definition states here unlike the print function printlin adds a new line at the end of the displayed value just a quick note on the use of the semicolon so the semicolon is used stylistically to indicate
the end of a line or suppress the default output displayed by evaluating the expression so in the first time we use that here we use that to suppress the output that's displayed by default but another use for that is actually we can just indicate the end of the line so typically whenever
we wanted to say this line or this expression ends at some point we hit the return key so we evaluate the expressions but if you don't want to do that if you don't want to evaluate the expression directly and you want to type something else and you want the same or the two expressions to be evaluated at the same time you can type in
the semicolon and then you type in the second expression that you want to evaluate and so nevertheless these are stylistic uses of the semicolon and in future lecture we will discuss formal use of semicolons as well alright perhaps the print and the print
lin functions are some of the most basic built-in Julia functions but there are also other built-in functions that does things like math or algebraic operations so many of the familiar math functions are actually directly available in Julia so for example the following cell here shows how we can
and find the square root of x using a function called sqrt, which as you might expect, just stands for the square root. So if you just type in sqrt, and then you pass here four, and then you close the parentheses, you hit the return key, you'll see that it tells you it's two. If I say, for example, 46, and then I hit
the return key, you'll see here, it tells me that the value corresponded to eight, and so on, okay? So without going into a lot of the details, the list below shows all the built-in power logs and root functions in Julia, and there's also the description and the example for each one. So we've just taken a look at the square root function,
which computes the square root of x, and it's used in this manner, so you just type in sqrt, and this should give you two. And then there's also the cubic root function, which finds the cubic root of x, and you can use it by typing c, b, r, t, 27, and so on.
Julia also provides all standard trigonometric functions, including the sine, cosine, and tan, and the following example finds the sine over radians, so we have a variable named degrees, we assign it to the value 45, and then we calculate radians, which equals to degrees,
divided by 100 multiplied by pi, and remember, pi is the built-in constant that is specified by Julia, so we don't need to define that. And here we pass in the radians to this function, so you can try this example at home, I will not do that for the interest of time. And there's also a list of other functions,
such as the absolute function, which gives a positive value with a magnitude of x, so for example, you pass in, say, abs minus three, and then you close the parentheses and you hit the return, it tells you that the absolute value of minus three is three, and so on, and then this function, for example,
tells you the sine of x, which returns either negative one, zero, or plus one, and this function copies the sine of one value to another value, and so on. All right, so to put everything into perspective, I would like to go back to where we started,
which is the fact that every programming language is referred to as a language. We wanna justify that claim, we wanna justify that claim at least for the Julia language by at least by showing how the Julia language can be thought of as a language indeed. If you remember from the definition, we said that a language is a structured system
for communication, that system has two components, the vocabulary and the grammar. In this sense, we can think of the vocabulary as the syntax, the stuff we write down here to write a Julia expression, or an expression that gets evaluated. And the one that we want to drill more on, and to be specific in this next few minutes,
is the grammar, or Julia grammar, okay? If you remember from the previous examples that we used whenever we defined a function, or a variable, or we wrote down any assignment statement, we used variable names such as A, T, and R,
these consist of single letters, but we also used things like double, debit, and then we also used something like debit underscore modified even, okay? So this brings us to the question of can anything be used as a variable or a function name? And the short answer is no. A variable and function's name should follow
two rules whenever we want to define them. They should start with letters, they can be uppercase or lowercase, or a few other characters, so for example, the underscore letter, but they cannot start with digits. And the second rule is that they can continue with letters, again uppercase or lowercase letters, and they can also continue with digits in this case,
underscore, or the explanation mark, and other characters, okay? So it's okay to define a variable that we call D1 equals, for example, debit here, and then we just pass in four for example to it, then we hit the return D, that is completely fine.
But what is not fine is to write something like 1D equals debit, or, and then you close the parentheses. That is immediately a problem, okay? So there's a follow-up question. So can we use the word function as a variable name? So let's see if the word function satisfies
the two rules that we have here. So does it start with letter? Indeed does, it starts with the letter, lowercase f. Does it continue with letters, digits, underscore, or the explanation mark? Indeed it continues all the way through with lowercase letters. So it indeed satisfies the definition that we have. So let's do that. So I'll type in function x, just to test the hypothesis,
and then C, and then set that equal to x. So in this case, I'm just defining a function, I'm giving it a name function, just to see whether it works to use function as a variable, or as a function name, and then I'm just gonna assign it to x,
or even just to be consistent with the notebook here, I'll just say it's equal to x squared, okay? But when I hit the return enter, you'll see here that I'm getting an error, and it says syntax unexpected equals sign. So although the word function only consists of letters, Julie does not accept that as a variable name. This is because Julie has keywords which are illegal
to use as variable or function's name, and function happens to be one of those keywords, and therefore we get that error. Below is a list of these keywords, or Julia reserved keywords, which are illegals to use as variable names. Now, this might not be as straightforward,
but you'll see here in the Jupyter notebook at least, things like function is, or something like function is actually written down in a different color, in this case, green. And this is essentially Julia telling you that this is, or the notebook is telling you that this is actually a keyword that you cannot use as a variable name. But if I write down something else,
like for example, function, and I just add the number two, just to make a distinction between the keyword and the one that I want to use as a variable name, you'll see here immediately it has no highlight, just to tell me which indicates essentially that it can be used as a variable name indeed,
or a function name, okay? So here's a list of all the words, you don't really need to worry about them too much, because as I mentioned, if you're using an IDE like the Jupyter notebook, you'll immediately know by typing them in the cell here. Just a quick note, although Julia does not care about
the semantic meaning of names, it's generally a good practice to use mnemonic names. So mnemonic names are names that are meaningful or relevant. In the following example, we used interest, the word interest to represent the interest rate, which is in this case, we say interest equals 0.04, and then we say period equals five,
just to represent the loan period. In the previous example, we use T on R, as long as you remember what R means and T means, both names are acceptable, and if you have more complicated notation or more complicated code, you might also consider writing down explicitly
what the variable stands for. This just makes it easier to remember whenever you're programming, but Julia does not care about the semantic meaning of that name. So you can really use whatever you like here as a variable name, of course, as long as it's satisfying the two rules that we've mentioned, it's not a key word.
All right, so the second thing you wanna talk about when it comes to the Julia vocabulary and grammar is something called operator precedence. So operator precedence is the order of evaluation when an expression contains more than one operator. So if you remember from previous example,
we had, let's say, the expression that contained the 15,000 multiplied by parentheses, one plus 0.04, we close the parentheses, we say raised to the power of five or four or whatever value we use, right? So this expression actually contains multiple,
more than one operators. It contained the multiplication operator, the addition operator, and the exponentiation operator, right? So Julia follows the mathematical convention when it comes to mathematical operators or when it comes to an expression that contains more than one mathematical operator.
And the acronym PIMDAS is a useful way to remember the rule of this mathematical convention. So what does this PIMDAS guy stand for? So it stands for parentheses having the highest precedence and can be used to force an expression to evaluate in the order you want. So in this example here, we have an expression
that says two multiplied by three minus one. If I, to evaluate this expression directly without writing any parentheses, what I'm gonna end up with is five. This is because two is multiplied by three first, which gives us six, and then when you subtract one, you get five, right? But if I do the same thing again, but now instead of adding,
or instead of writing without the parentheses, or if I add the parentheses, I should say, between the multiplication sign or the asterisk symbol and the three minus one, so you have three minus one here and close the parentheses, and I evaluate this expression, I get a four. So what happens here is that the three minus one
gets evaluated first because it's being forced by the parentheses, and then the output is multiplied by two. In this case, it's gonna be two multiplied by two, which gives us four. The second thing, or the second part of this PIMDAS is this letter E, which means that the exponentiation has the next highest precedence after the parentheses.
So if you have an expression that says one plus two to the power of three, this is going to be nine. So meaning that when you have this, instead of having to add one plus two first and then raise them to the power of three, what you get immediately is two to the power of three,
that's eight, and then you add that to one, and then you get nine. And then following that in the PIMDAS expression, we have M and D, which means that multiplication and division have a higher precedence than addition and subtraction, which is the last two letters of the acronym. Okay, so here in this example,
we have two multiplied by three minus one is five, not four. So if addition and subtraction had a higher priority compared to multiplication and subtraction, what would have happened was, what would have been three minus one would give us two, and then we multiply by two, we get four. But this is not the case because, as I mentioned,
multiplication and division have higher priority, so we get two multiplied by three, that's six, minus one, that's five. Similarly, here we have six plus four divided by two, these guys are gonna be divided first, so we get two, and then we add that to six, we get eight. Had it been the other way around, what would have happened was six plus four,
that gets 10, and then we divide that by two, and then we get five. But that is not the case because, as I mentioned, multiplication and division have a higher priority. Just a quick note, so operators with the same presence are evaluated from left to right, as you've seen here. So in the expression degrees divided by two multiplied by pi,
the division degrees divided by two happens first, and then the result is multiplied by pi. So notice here, in this expression here, it has a multiplication, a division and a multiplication in the same expression, so we don't have an addition or subtraction. And because these two operators
have the same order of priority, the expression gets evaluated from left to right, so what happens is I first evaluate the division because it comes first, and then the outputs, or the result of evaluating the first part gets multiplied by pi. And if I want to divide, if we want to divide by two, multiply by pi,
so this guy, multiply by the whole thing, meaning that I want to evaluate the multiplication first, you can just use the parentheses, or you can just say degrees divided by two, divided by pi in order to multiply by the reciprocal of pi, which is going to be one over pi.
All right, so just to summarize, programming is a formal language, and the acronym REPL stands for read, evaluate, print loop. Expressions are a syntactic entity that consists of constants, variables, functions, and operators. Assignment statements create and assign values to variables using the equal operator, and we can use Julia to perform basic math operations
using either arithmetic operators, define our own function, or use one of the Julia built-in functions, and in variable and function names, whenever we define them, we must not start with digits and continue with letters and digits or a few other characters, such as the underscore symbol,
and Julia has keywords that are illegal to use as variable or function names. And last but not least, we also looked at how Julia follows the mathematical convention PIMDAS for operator precedence. So what we should look forward to is, can Julia handle data types other than numbers,
and if so, what are they, and how do we work with them? Thank you for watching, and I'll see you in the next video.