Introduction to Programming for Business Analytics - Exercise 9: Python Fundamentals and Elementary Data Types
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
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 | 10.5446/64049 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Producer |
Content Metadata
Subject Area | ||
Genre | ||
Keywords |
00:00
Computer animationLecture/Conference
01:52
Computer animation
09:20
Computer animation
12:33
Computer animation
19:07
Computer animation
25:42
Computer animation
28:24
Computer animation
30:27
Computer animation
36:39
Computer animation
44:40
Computer animation
Transcript: English(auto-generated)
00:10
Hi, welcome to the ninth exercise video for the introduction to programming for business analytics class. This video marks a very important
00:21
shift in the topic of the exercises because from now on, we're going to talk about Python. You might notice that the Jupyter notebook is now says Python three instead of Julia, in the upper right corner. And as always, please attempt to
00:42
solve the exercise by yourself before you watch the respective part of the video where I explain the task. Because if you will just watch me solve the tasks, without having attempted to solve them yourself, you will not really learn anything. And with that said, let's get started with task one, which is to list down all the differences between Julia and Python in the context of the topics
01:11
discussed in the ninth lecture. And this list is very long, has 15 bullet points in my solution. And I will just go through them one by one.
01:27
You can, to solve the task, you can compile a similar list by just going through the lecture, and writing down all the differences that are discussed in the lecture. And if this is not exactly 15 bullet points, or if you if you merge or split up some bullet
01:45
points, that's not really a problem. The point of this exercise is really that you end up understand all of the differences. All right, so the first item in the list is that Julia's exponentiation operator is this caret symbol, while pythons operator is the two asterisks. And the second
02:08
point is that Julia has functions which are called print and print lm. While Python only has the function called print, and print always adds a new line. So it kind of behaves like print lm in Julia. And another difference is that when we give multiple
02:26
arguments to print in Python, they are printed out of the space between the items. Whereas in Julia, if we give multiple arguments to print lm or print, they are printed without a space separating them. And in Python, you can actually configure the behavior of the print function by
02:44
giving extra arguments, like you can you can change the space to something else. And you can also change whether there is a new line or not. And now, item number three, is that Julia makes most math functions directly available. An example for us is that you can just
03:04
type sqrt in Julia, and you have the square root function. But in Python, the math functions are all loaded via this module called math. And so you have to first import the module math, and then you can type math dot sqrt to get the
03:23
square root function, for example. Fourth item is that Python has no numeric data types, which specify the bit width. So Python has three, three numeric data types, int, float and complex. Whereas Julia has things like int64, float64, and then data types with other bit
03:43
widths. And in Python, we just have the three. And also the int data type is really like Julia's big int. So the there is no overflow in Python int and you can store numbers that are arbitrarily large as long as your computer's memory is large enough
04:01
for that. For the fifth item is that Python's Boolean literals are spelled with a capital letter, while Julia's are spelled with a lower case letter. And six bullet point is that there is no character data type in Python. So there is no data type that can just hold one
04:22
individual character of a string. But there is such a data type in Julia. In Python, we define string literals differently, we can use the single quotes, also called apostrophes, or we can use double quotes. And we
04:40
can also use three of the quotes instead of just one quote at the end of each end of the string literal. And in Julia, you know that we can only make string literals with the double quotes. And in Python, if you use the triple quotes, then you can actually have new lines in the string without creating a syntax error. And
05:07
Python, which is the eighth item in the list, has functions which we call, which are called str, int, float and bool. And these functions, you can use to convert any value to a string with the str function to an integer
05:25
with the int function to a floating word number with the float function and to a bool with the bool function. I said any value, however, this is there are some values which still which just cannot be converted. But as long as there is some way to convert the value in a meaningful way into the respective data type, then it is possible.
05:47
Item number nine is that instead of Julia's arrays and vectors, Python only has list. And list is a little different from arrays and vectors. And one of the most important differences is that it cannot be multidimensional. So you cannot have a matrix stored in just one
06:07
list. Instead, you would need a nested list to store a matrix. So that is you have a list of columns, for example, in your matrix, then for each column in the matrix, you have another list inside of your list, which then stores the items in the different rows of
06:24
the column. Item 10 is Python uses zero based indexing instead of Julia, which uses one based indexing. 11, when we index or slice with the bracket operator in Python, then there is a difference regarding the end of the list. So you know that in Julia, we could type something like
06:44
and minus one to get the second last item in a list or string. And in Python, we would just use negative numbers for that. So the if we use minus one as the index, then it's the last item minus two is the second last item minus three is the third
07:05
last item and so on. And item 12 is when we slice a sequence, like like this here, my underscore sequence, and then the bracket operator a colon b, then in Python, the b item of the sequence is not included in the result is excluded, and it would be
07:23
included in Julia. So for example, if you if you select one colon, two in Python, then you only get one item, while in Python, you would get two items. Item 13, the string concatenation operator in Julia is the asterisk, while in Python it's the plus. Item 14, in
07:46
Julia string interpolation is done with these with the dollar character as the escape symbol. And then you can use this parentheses to surround a more complicated expression if it's not just one identifier or variable that you
08:03
are putting into your string. And in Python, you put the letter F before your strings or before the first limiter. So it would be F double quote, for example, and then your string literal. And then inside of the string literal, you can use these curly braces here. And whatever is inside of these curly braces interpreted as a piece of
08:28
code, and is put into the string. We will see examples for all of this later. And in item 15 is concerns how we append to a vector or a list respectively. So in Julia, you know that they we had the
08:42
methods, sorry, the functions push and append to push one item at the end of the vector or to append an entire vector to another vector respectively. And in Python, we have the method, the method not the function append, and the method extend. And we call these on our list. So if we have a list, which is assigned to the
09:08
variable l, we can call l dot append e. And then we have appended an element e to our list l. And similarly, we can do it with two lists with the method extend. Okay, so for task two, we actually have to write some Python code. First, we need to give
09:27
two examples for a Python literal, which has a value of type int. One example would be the number 15. That's an int. Another example would be zero, that's also an int. And we can actually check if we're correct by running by calling the function type. And type of
09:50
zero will give us the answer which is int. And the next sub task is to give two examples for a Python literal, which has the value, which has a value of the type float. So for example, this would be 1.5 or 0.0.
10:08
So anything where you include this decimal point, you get a float literal. Third sub task is to give examples for a Python literal, which has a value of the type string. And here we can
10:20
use any of the delimiters, which we discussed in the first talk. So we can, we can actually just put a Julia string literal like this. And it will be a string. And you can see that Python actually internally converts it to double to single quotes.
10:40
And you can see that the type is now str, which is Python's way of saying that the type is string. And we can also use the single quotes to make a string. And what we can also do is we can use the triple quotes. So for example, this can also be a string. And there we have a string literal, which is then printed like this. So you can see that Python adds a new line before the first
11:13
test, and then another new line before this word test, then we have another new line at the end. And we can also assign this
11:23
to a variable. And then we can print the variable with the print function. And then the it's printed just like that, without the escaped new lines. And now the next sub task is to give two examples for a Python literal, which has a value of the type list.
11:45
And the simplest example would just be the empty list, which we can input to Python like this. And a more interesting example would be to put some items into our list. So for example, we can put numbers in the list, and we separate each of them
12:03
with a comma. And we can also put different things into the list. So for example, a string, another list, or also a Boolean literal. And the next sub task is to give two examples for a Python literal, which has a value of the type bool. And here
12:24
there is actually only just two answers. And these answers are true and false, because that's the only Boolean literals that exist. Now after task three, in task three, we have to take some Julia code and translate it to semantically equivalent Python code. And we will also try to
12:45
write Python code that doesn't just give the same output, but also creates this output in a similar way. Or sometimes we also make use of features which are available in Python only, but not in Julia. So for example, we could actually write if, if we can, if we compute
13:07
the square root of nine and Julia, we could just write the result immediately, which is three, that's very boring. Instead, we want to know how to calculate the square root of nine in Python as well. So we first import the math module. And then we can use the function math dot sqrt. And then we
13:27
get the same result, which is three. The next Julia code snippet is big int of two to the
13:42
type int just behaves like big int behaves in Julia, so there is no overflow. That is right, we can just write two to the power of 8192. And we get a correct result. Whereas in Julia, if we executed this with the different exponentiation operator, which Julia has,
14:04
we would get a very different result. The next Julia code snippet is type of 100. And in Python, we can achieve the same result by just calling the function type. Then we again,
14:22
put the literal 100 there, and we get the answer which is int. And the next code snippet concatenates these strings here. So I, P, B and A, and they are concatenated in Julia with the asterisk, which is the string concatenation operator. And we can do the same in Python with
14:45
the plus operator. And we can also make use of all the different ways there are to define strings in Python. Sorry to define to create string literals in Python. So this should give
15:01
us the same answer, which is just a string I PBA. Now here, we have a very, very, very long string in Julia. And we can define this in Python. By putting it like this, I am a very, now there is a new line. Very. Now another new line. Then we have the third very,
15:27
and then another new line. And then we have long string. Okay. Now there is now there is our very, very, very long string. Here we have these two print lin statements in Julia,
15:49
which both slice something out of this string here. And to print something in Python, we call the function print. So we call this two times, then we put the string literal.
16:10
And now we want to slice from this string. And we have to make sure that we translate these indices correctly. So in Julia, indexing starts at one. So we first have to subtract
16:29
one from the first part of the slice. So that would be four. And also in Julia, the second number of the slice is interpreted inclusively. So the seventh character of the
16:44
string will be included in the result in Julia. Whereas in Python, the second part of the slice is interpreted exclusively. So that is why we, if we translate number seven to the zero waste
17:06
indexing, we would get six, but because it is exclusive in Python, we still need to use seven to get the same answer. And now for the second line, we have to use the negative indices that
17:24
Python uses to slice strings beginning from the end of the string. And now let's have a look what happens here in Julia. So Julia, in Julia, this would be the character we get when we index
17:43
just with end, this would be the D would be end minus one, the L would be end minus two, and minus three, and minus four, and minus five, and minus six. So in Python, to get the same U, we go minus one, this is the character we get when we just put minus one here, then minus two,
18:05
minus three, minus four, minus five, minus, minus six, and minus seven. So we start with minus seven here. And then in Julia, again, it would be end minus one, minus two, minus three,
18:23
minus four. And in Python, it's minus one, minus two, minus three, minus four, minus five. So let's have a look. And again, we have the exclusive string indexing in Python. So again, we
18:42
need to move the last index one step more to the end of the string. So instead of minus five here, we would use minus four. So we get the same answer as we get in Julia. And now we have
19:01
these two lines of Julia code here. In the first line, we create a Julia vector. And we can do the same in Python, it's actually with the exactly same, exactly the same syntax, just in Python, we receive a list instead of a vector. And then in Julia, the function each index gives us each index into the vector, this would be another, this would be a sequence with the
19:25
numbers 123 and four. And then if we call the function vector and Julia, the sequence will be again, translated into a vector. And in Python, we can do much of the same by using the function range, which can give us the indices between two, between two indices, which are at the
19:48
beginning at the end of the sequence. And if we provide the arguments one, and the length of V, so that would be le m v, we could also we could also have just typed four here,
20:06
then we will get this iterable sequence. And to convert to a list, we can call the function list, which basically has the same role here as the function vector in Julia. And then we get this answer, which is 123. So not what we would have expected, because in Julia,
20:27
this prints 1234. And this again, is because in Python, ranges are exclusive with respect to the last index. So if we want to receive 1234, then we have to add one to the second argument,
20:44
which we pass to range. And now it is 1234. And just as quick advice, if you want to iterate over a string or a vector, for example, in a list, for example, in Python, with the index,
21:01
and you can just use a range of le m of the thing you want to iterate. And then you will get all of the indices that you must that you must use in the iteration if you want to have every single element. Okay, now, the next Julia code snippet first creates this vector here,
21:29
and then it pushes two elements into the vector. And then it appends another vector to the vector. So let's do the same thing in Python, maybe we can call it even the variable exactly
21:43
the same. So first of all, we create the empty list. And then we use the method append to append one single item to the list. So this is the same function which is taken by the method, sorry, the function push exclamation mark in Julia. And we do the same
22:05
thing again for the number seven. So if we do this, our result looks like this. And to also extend the the list, the Python list with a another list, we can use the function extend
22:27
where we pass a list as the argument. And notice that in Python, we often can call a function with this dot notation on some value. So we say that we sorry, a method, we say that
22:48
we call the method append on the value ra. And we call the method extend on the value ra. This is not possible in Julia. But in Python, it is possible. And to if we do this, we modify
23:04
the list ra in the process. Now the next Julia code snippet, we first define three variables i, j and k, which have the values one, two and three respectively. So we can do exactly the
23:21
same in Python, the syntax is just the same. And then we include them into this string with the Julia string interpolation syntax. Now let's do the same thing with the Python string interpolation syntax. So first, we start by having i equals in the string, then we put the
23:40
curly braces in place of the dollar symbol. And we just put i inside of the curly braces. Then we continue with the string comma space. Now there's j equals and then again, we have the curly braces instead of the dollar symbol. Inside of the curly braces, we put j. And again,
24:01
we continue with the string so comma space k plus equals. And then again, we put the curly braces and inside of the curly braces, we just put k plus one, which is the last last thing we include in this string in Julia. And so you can see that the result looks like this, i equals
24:23
to one. So this curly brace i is replaced with the value of i, j equals to two. So these curly braces with j are replaced with the value of j, and k plus one equals to four, where these curly braces are replaced with the value of the expression k plus one. Now in Julia, if we call
24:44
the function parse, and give it an numeric data type as its first argument, it converts the second argument, which is a string to this data type. And in Julia, we can do the same by calling the function int if the argument was an int numeric data type. And here we pass the string 01234 as
25:11
the argument, and we receive the number, oops, I did a typo, we receive the number 1234 as an int.
25:21
And in Julia, we can convert the number 1234 to a string. When we call the function string, and we can do a similar thing in Python, if we just call the function str, and we put in the numeric int literal 1234, we receive the string 1234. Task four says you are given three
25:44
pairs of variables, see below. For each pair, write a Python expression that evaluates to a boolean value, either true or false, which indicates whether the values of the two variables are equal to each other, then there's the hint, the results should all be true. So for
26:01
the first variables, a one, and a two, this is very straightforward, we just, which we just execute the cell which defines the variables first, and then we can compare them using the equals equals operator. So the same operator that we use in Julia, to compare numbers for equality,
26:22
sorry, int numbers for equality. And the same is true for strings. So let's first actually define the strings. And then we can again use the equals equals operator to compare the strings for equality. So this is also true. And now, with the float numbers, it gets tricky,
26:42
because if we just try to compare them for equality, when we buy by using the equals equals operator, we get the result false, which at first might seem unintuitive. But as you know, again, already from the Julia lecture on numeric data types, if we use equals equals on float
27:06
numbers, we actually compare their represent how they are represented, and not what they represent. So here, both of these variables represent the value 0.3. But 0.3 is represented in different
27:25
ways, we can see this if we just look at the variable. So the first one just gives 0.3. But the second one gives 0.3, then there's a lot of zeros, and then there's this four here at the end. So this is a result of different ways to represent the same number. And we can
27:45
actually compare float numbers for equality with respect to what is represented with them, not what they represent in Python when we use the function is close. I think this is from the math package. So c1, c2. Yes, and there we have it.
28:06
The result is true. So here, the functions, the function method compares the two variables for equality in a way that is intuitive, and makes sense if we are interested in what
28:22
they represent. In task five, we have to give three examples for an assignment statement that is illegal in Python because it involves a keyword. Now, you might think that maybe a keyword could be int. So let's just try what happens if we assign to int. Okay, it just works.
28:47
So now if we evaluate int, which is the type of integer literals, Python, and also the function which we have previously used to compare to convert, for example, strings to integers,
29:06
then now int just has the value of one. And in fact, if we try to use the function again to convert a string to an integer, it doesn't work. And Python now tells us int object is not callable, because we've just tried to execute a function call on an on int, which is now
29:25
just a variable with the value one. Yes, so that is a fact about Python, you can just assign to many things to which it is not very useful to assign to. But
29:42
there are still some assignment statements that are illegal. And one example for that is we cannot assign to the keyword import. So we cannot have a variable that is called import, we also cannot have a variable that is called if we cannot have a variable that is called
30:03
while all of these are not possible. And there are many, many more examples. So if you came up with different examples, don't don't worry about it. So yes, protected keywords in Python, we cannot assign to them, but we can still assign to types like int, float, str, and so on.
30:28
Now in task six, we introduce the concept of lambda functions, which you haven't seen in lecture. And in Python, if you use the keyword lambda, you can create a function. And one example for this is we look at the mathematical function f of x equals to x to
30:44
the power of two. And we can create this in Python like this. So we have the keyword lambda, then we have the parameter of the function. In this case, this is x, then a column, and then the functions body, so x to the power of two. And this creates the function and we can
31:02
assign the result of this function creation to a variable, which is f in this case, and then we can call this variable like this. So first of all, we have created the function now. And if we just look at f, it will tell us something like this is a function. Yeah, so that looks like this. But if we call f now, and then we get the result of
31:28
four to the power of two, which is 16. And now the task is to for each of the following mathematical functions given equivalent Python function.
31:41
And first of all, we start with the name of the function, which is what we assigned to, and then we use the keyword lambda, then the function parameter, so x in this case, and then the functions body so that in this case is just m times x plus b, plus plus b. Yes. Okay.
32:03
And if we try to call the function now, we receive an error message because m and b are not defined. So if we define them first, then we just get the answer. In this case, it is three. Next mathematical function binomial of a and b equals to a to the power of two minus two
32:26
times a times b plus b to the power of two. So let's go, we create a variable which called binomial. And to this variable, we assign a function, which we create with the lambda keyword. And then we list the two parameters a and b separated with a comma. And after that,
32:45
we get the function body. So a to the power of two plus two times a times b plus, or I wrote minus here. Okay, yes, sorry, minus two times a times b plus b to the power of two.
33:05
And if we call this function now, it's binomial of, let's say, three and four. And the result is one. The next mathematical function is growth of x and m. So let's start by
33:25
defining a variable which we call growth. To this we assign a function which we again create with the lambda keyword. We have two arguments, sorry, two parameters here x and m. And um, now the body of this function is this fraction here. So I type one over
33:44
one plus math.exp, which is the exponentiation function in Python, 0.05 times x. Again, we can call this function by typing growth of
34:06
five and one, for example, the argument one is just discarded because this parameter m doesn't actually in doesn't isn't actually part of the functions body, but that doesn't really matter.
34:20
We still get this result 0.56217 and so on. The next mathematical function is called gauss. So again, we define this variable gauss to which we assign the result of a function definition with the lambda keyword. Here we have three arguments, so x mu and sigma.
34:44
And the body of the function is this complicated mathematical expression in which we first have a fraction, one over something, which we then multiply with an exponentiation. And the
35:02
the denominator of the fraction is sigma sigma times the square root of two times pi. So sigma times math.sqrt two times pi, and then the in the exponent we have, we have minus one over two multiplied by
35:30
x minus mu over sigma. And this entire second fraction is then raised to the power of two.
36:00
Okay, there we have it, our Gaussian function. Now we can call this, for example, with mu of zero and sigma of one and we get some error message. Let's see, pi is not defined, right?
36:20
In Python we have to do it math as math.pi instead of just pi. And there we have it 0.398 and so on. It's the value of the Gaussian function at zero if mu and sigma are zero and one. In task seven, we now apply functions to solve a business analytics task. And the task is,
36:47
suppose you have the opportunity to buy a popcorn card and start a popcorn selling business. You can use Python in your analysis of whether this is a good investment. First task is write a Python function that expresses your popcorn cards earnings after tax
37:03
as a function of the earnings before taxes and interest, the interest expenses and the taxes to be paid. So let's do this. Let's call our function EIT because it calculates the earnings after tax, use the lambda keyword to define the function. And it is a function of three
37:24
parameters, which is the earnings before taxes and interest, the interest expenses, so why don't we call this interest expenses and the taxes to be paid. So why don't we call this taxes.
37:41
Maybe we just shorten this to interest. Again, we put the colon to divide, to separate the list of parameters from the functions body. And now we put the functions bunny which is just we subtract the interest from the
38:05
earnings before interest and taxes and then we can subtract the taxes from that. This gives us a result. Let's try if it works by calling the function. So what is a popcorn cards earnings before taxes interest? I really have no idea. But let's just use 15,000 here.
38:26
And let's say that maybe interest is 3000, taxes is 4000. And then the earnings after interest and taxes would be 8000. Now, second sub task is for the purpose of this exercise,
38:42
let the breakeven point be the minimum number of bags of popcorn you need to sell such that your revenue is equal to your costs. Write a Python function that expresses your popcorn cards breakeven point as a function of the price of one back of popcorn, one back of popcorn P, the cost of one bag of popcorn V and the fixed costs C. Make sure that your function
39:04
returns an integer. Now, again, we define a function. Maybe we call it break even. And to this name, we assign the result of the function definition where we can put the lambda
39:20
keyword. And then we have the three parameters of our function P, V and C. And after that, we put a column. And then we have to calculate the breakeven point.
39:40
So the minimum number of bags of popcorn you need to sell such that your revenue is equal to your cost. So what we want here is that the revenue is equal to the cost. So cost is a variable that we directly have available, it's called C. And the revenue is just a multiplication
40:00
of the price of one bag of popcorn. And the and the cost of one bag of popcorn, we, we subtract those to get the to get the profit we make. And then we multiply this with the number
40:21
of bags of popcorn that we sell. So that would be P, which is the price minus the cost. So that's our surplus per bag of popcorn, right? Then we multiply this with x. And now, what we
40:41
are actually interested in is the minimum number of bags of popcorn you need to sell. And this is the x in our equation here. Now, we need to manipulate this equation such that it reads x equals to something. And we can do this by just dividing by P minus V, right? If we just divide
41:05
by P minus V, we get an equivalent equation, which reads C over P minus V equals to x. This is exactly what we will put as the body of our function called break even. And what we
41:24
haven't done is to make sure that the function returns an integer. And for this, we can use the value math dot seal, because the function will probably return something like will return in some some decimal number, like for example, 1.5. And if the break even point is at 1.5,
41:47
we actually need to sell two bags of popcorn because our business does not sell half bags. So that's why we want to always round up to the next integer, which is exactly what the function math point seal accomplishes for us. And now if we call the function break even,
42:06
we need to come up with some numbers. So P, the price of one bag of popcorn, let's say, let's say, let's say we sell one bag for 99 cents. And then the cost of one
42:20
bag of popcorn, maybe that's 20 cents or something. And the fixed costs, maybe the fixed cost is 5000 euro. And now we get the result. So we break even if we sell 6330 bags of popcorn. And
42:48
now the third sub task is suppose that as an alternative to buying the popcorn card, you consider putting the money into a new savings account, write a Python function that expresses the accounts debit after T years as a function of the cards price a,
43:04
the accounts annual interest rate are and the number of years T to solve this sub task, you need the formula for the interest rate, which you can look up if you don't know it. For example, we discussed this in the first lecture. And so to to write a Python function,
43:26
maybe we can call our function debit. And we can use the lambda keyword to define the function and then we put the functions parameters which are A, R and T. And now the functions body is
43:40
the debit of the account after T years, which is the the cards price a multiplied by one plus the interest and annual interest rate are raised to the power of T. And to test the function again, we have to come up with some numbers. Let's say that our
44:02
popcorn card costs 12,000 euro. And let's say that our interest rate is maybe a very generous 5%. And we want to make the investment for 10 years, then we get some
44:23
right I have to do a multiplication sign here. Now it should work. And there we have it. After 10 years, our bank account has 19,546 euros and 73 cents. With this we are done with the
44:42
first Python exercise video. As always, I hope I could answer all of your questions about how to solve the tasks in the ninth exercise. If this is not the case, then please ask for help on Moolah. And in any case, have a nice day and see you next time.