openSAP Python1 Week 6 Transcript
openSAP Python1 Week 6 Transcript
00:00:05 Welcome to week six, welcome to the final week of our course.
00:00:10 So what is the topic for this week? Actually, it's libraries.
00:00:15 So what are libraries? So far, your program has always been in just one file.
00:00:23 However, software and programs tend to become very big and very complex.
00:00:29 So it's better to put it in parts and to reuse functionality from one program
00:00:36 within another program, therefore you need libraries. So again, these large programs become
confusing and very hard to read.
00:00:49 If you just have one file, you would start to copy and paste parts of the program from one file to
another file.
00:00:57 However, you have very much duplication of code, of code snippets, and it is very tricky to
maintain this code afterwards.
00:01:07 You will tend to have different versions and in the end, you won't have control
00:01:12 of your programs anymore. Modularization splits up the code into pieces
00:01:20 and puts each of these pieces in a separate file. However, to make use of this functionality
within other files,
00:01:30 you need some kind of concepts, and these are libraries.
00:01:34 It's showtime. Let's dig into our notebook.
00:01:37 Like every week, open your notebook and we'll have a look at more details.
00:01:42 So again, what's our motivation? Large programs in a single file quickly become confusing
00:01:49 and difficult to maintain. Our program so far just had a few lines.
00:01:55 In reality, software consists of thousands, hundreds of thousands, or millions of lines of code.
00:02:02 It's not possible to really squeeze it into one single file. And if we always have to do it in one
file,
00:02:13 the reusing of code becomes very tricky. You would have to copy and paste it from one file to
another,
00:02:21 and this is really a bad habit. So what is to do?
00:02:30 What we are aiming for is modularization and modularization is something,
00:02:35 what you have done and what you can see outside of software as well. Take, for example, a
car.
00:02:42 A car consists of many independent components or modules. This is why we talk about
modularization.
00:02:52 For example, a car has a chassis, it has an engine, it has battery,
00:02:57 wheels, an entertainment system, and so on. And important, you can do modularization
00:03:04 on several levels. That is you can, for example,
00:03:08 decompose the entertainment system and take subcomponents like a touchscreen,
00:03:14 like speakers, like a computer system, and so on and so on. And what is the advantage of this
modularization?
00:03:24 First thing is these modules can be created and tested independently.
00:03:30 You can, of example, take this entertainment system, develop it outside of the car, test it
00:03:37 and only when it really works, plug it into the car. This entertainment system could be reused.
00:03:48 It could be used within another car. For example, you can develop one entertainment system
00:03:54 and use it in different cars. Maybe your entertainment system gets broken.
00:04:02 Then if it is a component of its own, then you can really take it out of the system of the car
00:04:10 and replace it with a new one. And you can do it not only if it breaks
00:04:16 but you can even do it if you have a new version, a better version of your entertainment
system. So having modules, having components,
00:04:26 makes systems easier to maintain, it makes them more robust.
00:04:33 Why should we build software in modules? Software systems are among the most complex
systems.
00:04:41 We provide a link in here, which lists simply the number of lines of code
00:04:47 for a certain software, like an operating system or like the complete software within a car,
00:04:55 something like that. And you can see simply by the number of line of code
00:05:00 that this software is really, really complex. So this is a very good argument to think about
modules.
00:05:09 Actually, we have used modularization already when we introduced functions.
00:05:17 Each function has a clear task. Each function can be developed and tested independently
00:05:24 from other functions. And each function provides an abstraction
00:05:29 of the implementation details. So from the outside,
00:05:33 you just look at the function name, its parameters. If it is once programmed and implemented,
00:05:39 you don't have to care about the details anymore. However, these functions so far could only
be used within one program,
00:05:48 not from one program to the next program. This is why we need this concept of libraries.
00:05:57 Looking on libraries in Python, there are basically two different types of libraries.
00:06:04 First, there is the Python Standard Library. This is a library you have already installed
00:06:11 when you downloaded the original Python at the very beginning.
00:06:17 Besides that, there is the Python ecosystem. There is thousands of Python developers
00:06:25 who have programmed code and offer this code to others. And so you find lots of different
libraries
00:06:32 focusing on different aspects, like for example, the SciPy library
00:06:39 provides functionality for mathematics, science, and engineering. Whereas Flask offers
functionality
for building Web applications.
00:06:51 Where can you find those libraries? Maybe one should be mentioned,
00:06:56 namely the Python Package Index. This is basically a big repository
00:07:01 that makes it easy to install libraries, to find libraries, and so on. We will provide examples for
different libraries
00:07:10 throughout this week. So, this has been an introduction to libraries.
00:07:16 We have not looked at the code yet, but you should understand right now
00:07:20 why libraries are necessary if you really would like to do real programming.
2
Week 6 Unit 2
00:00:05 Hello, welcome to week six, unit two, importing libraries. In order to use libraries in Python,
00:00:14 you need to import them first. We will see in this unit
00:00:17 that it's possible to import complete libraries or just certain functions from a library.
00:00:23 In addition, we will see what namespaces are, how we can define aliases for namespaces,
00:00:29 and how we can use these namespaces to avoid name conflicts. So again, it's showtime.
00:00:37 Let's jump over to our Jupyter Notebook and see how to import libraries in Python.
00:00:43 In order to import a library in Python, I would use the import statement.
00:00:49 For example, this line here, import math, imports the math library.
00:00:55 If I execute the cell, I don't see any output, but in the background, the math library has been
imported.
00:01:02 I also added a link to the documentation of the math library. You can browse through it
00:01:08 and see what kind of functions and constants are defined in the math library. I will use a few of
them later on in this unit
00:01:18 to demonstrate how to work with libraries. So, what happened after we imported the math
library?
00:01:27 After we imported the math library, the functions and constants inside the library are available
to us.
00:01:33 However, we need to specify the fully qualified name, so we need to call these functions
00:01:41 and constants using their namespace. In this case, the namespace is just the name of the
library,
00:01:49 so in order, for example, to call the cosine function from the math library, I would call the
function using its full name,
00:02:00 namely math, the library name, followed by a dot followed by cosine.
00:02:06 In the same way, I can access the constant pi. Namespace math, followed by a dot, followed
by pi.
00:02:19 So this way, I can calculate, for example, the cosine value of pi.
00:02:24 And that is true basically for everything that's inside the math library.
00:02:29 For example, to access the sin function, I would call math.sin.
00:02:37 Let's execute this cell and we see that the cosine value of pi is -1
00:02:43 and the sine value of pi 1/2 is one. However, sometimes it's a little bit tedious
00:02:52 to always write the whole library name, especially if the library doesn't have a short name like
math
00:02:59 but has a very long name. And therefore, it's possible to define an alias for the namespace.
00:03:05 And this is exactly what's happening here. When importing the math library,
00:03:10 I now define an alias for the namespace, namely m. And this enables us to afterwards access
00:03:19 the different functions inside the math library not using the full qualified name math. function
name,
00:03:29 but instead just using an m. For example, m.sin or m.cos.
00:03:38 So we don't need to print out or write out the full library name anymore.
00:03:43 Let's give this a try. I need to enter a number here.
00:03:47 So let's enter 10. And the value of the result is 1.0.
00:03:55 And what is the result I calculate here? The result I'm calculating here is the Pythagorean
identity.
00:04:04 So sine x squared plus cosine x squared, and that should always result in 1.
00:04:13 So I can enter different numbers as well. Let's try 6,055 and again, the result is one.
3
00:04:21 And you see the important part here. I can access the different functions,
00:04:26 power function or the sine function from the math library using their shortcut name, m,
00:04:34 instead of the fully qualified name. What you should do now as well,
00:04:40 you should pause the video and have a look at the different functions we used
00:04:45 inside the Python documentation. For example, the pow function,
00:04:49 which is a function that calculates x to the power of y. And there's also some information on
when this should be used and when not.
00:05:03 So I won't go into detail of all these functions but I encourage you to pause the video
00:05:07 and have a look around the math library documentation. So far, we have always imported
whole libraries,
00:05:16 but sometimes it makes sense to import just one or two functions from a library.
00:05:21 For example, if we know that we just use the sine function and pi from the math library,
00:05:27 we don't need to import everything else, but instead we can just partially import this library.
00:05:33 And this is exactly what it's done using this syntax here. From the math library I want to import
a list of elements,
00:05:41 in this case just two, sin and pi. And afterwards, I can use these functions
00:05:48 in my program code, and the important part to notice down here
00:05:52 is that I don't need to use the fully qualified name in this case. I can just invoke sin and pi
00:06:00 without specifying math.sin or math.pi. Let's execute this and we see that the result of 3 * pi /
2,
00:06:13 the sine from this is -1.0. I mentioned the term namespace before.
00:06:20 Why are namespaces useful? So if we look at Python,
00:06:24 we have different elements in the language. We have some keywords, something like with, for,
and.
00:06:33 And we have built-in functions, for example, print and input.
00:06:38 These are names that are already taken by the Python language. If we now import a library,
00:06:45 even more names get assigned. For example, if we import the sin function
00:06:50 from the math library, also the name sin is assigned.
00:06:53 And that can lead to conflicts. I've written a pretty stupid little program here,
00:06:59 which creates one of these conflicts. I first import the sine function from the math library,
00:07:06 not inside the namespace but just inside the general namespace.
00:07:12 And then I use it to calculate the sine from zero. Afterwards, I define a sine function myself
00:07:19 and this just prints a result. I don't know how to calculate the sine of something.
00:07:25 And if I now call sin(0) again, you will see what happens.
00:07:33 Let's execute this. The first calculation is performed correctly
00:07:39 and the second calculation, we get the result from my custom sin function.
00:07:46 And what is happening here is that basically the new function I defined here
00:07:53 assigns the name sin to something else. So first, we assigned the name sin
00:07:58 to the sine function from the math library, and through my definition here,
00:08:04 it's assigned to something else. And therefore, the correct sine function from the math library is
not available to us anymore.
00:08:12 And this is exactly one of the conflicts that can happen, especially if you import a large number
of libraries
00:08:18 and therefore, namespaces are useful. We can use different namespaces
00:08:24 to avoid exactly this conflict. And this is what I will show you in the following example.
00:08:32 Now I import the whole math library using an alias m,
00:08:38 and afterwards, I define my new sin function, but I can still access the sin function
4
00:08:46 from the math library using the fully qualified name, including its namespace alias.
00:08:52 And if I now execute this program, you will see I'm still able to calculate the sine from 0.
00:09:00 Therefore, we have seen why namespaces are useful. They help us to avoid naming conflicts.
00:09:08 What I've shown you so far are different forms of import.
00:09:12 And now you could ask the question when to use what. When do I use, for example, a plain
import?
00:09:21 When would I use an import with an alias defined? And when would I import, for example, just
parts of a library?
00:09:33 So that's a good question. Basically, for most of the common libraries you use in Python,
00:09:40 there is a recommended or established way to import the libraries.
00:09:44 For example, each and every time you would see a code snippet using the math plot library
00:09:50 or the NumPy library, you would probably see a statement like this.
00:09:55 Everybody that uses NumPy would usually import it,
00:09:59 import numpy as np. So our recommendation is use the established way to import libraries.
00:10:08 If you just import one library, it's usually okay to import the whole library.
00:10:12 If you import multiple libraries, you might get in a situation
00:10:16 where you get naming conflicts, and therefore, you just choose the established way,
00:10:21 which you can usually find in code snippets or in the documentation of a library.
00:10:28 So let's go back to our slides. What have you learned in this unit?
00:10:31 In this unit, you have seen how to import libraries into your program. And you have seen how
to avoid conflicts using namespaces.
00:10:42 Thanks for watching and see you in one of the next units.
5
Week 6 Unit 3
00:00:05 Hello, welcome to week six, unit three, Math and Statistics Libraries.
00:00:13 In the previous unit, you have learned how to import libraries into your Python code
00:00:17 and how to use these libraries. In this unit, we will work with a few libraries
00:00:25 in the context of mathematics. Namely the math, the random, and the statistics library.
00:00:33 And you will learn how to read the documentation and find functionality that you need inside
the Python documentation.
00:00:43 So instead of a traditional unit, where I would provide some information,
00:00:49 this unit contains just two exercises. We will switch over to the Jupyter Notebooks again.
00:00:56 I will show you the exercise. Then, you will try to solve the exercise yourself
00:01:01 by reading through the documentation and trying different functionalities from these libraries.
00:01:07 And afterwards I, of course, will show one possible solution.
00:01:13 So, showtime, let's switch over to our Jupyter Notebook and I'll show you the first exercise.
00:01:20 As mentioned in the introduction, you will now practice using the math, random, and the
statistics library.
00:01:27 So the first exercise is the following. Using the math library, you should write a program
00:01:33 that performs the following tasks. First, the program should ask the user for number.
00:01:39 Then, the program should calculate the factorial of this number, and the program should also
calculate the natural logarithm
00:01:49 and the logarithm to base 10 for this number. And of course, the results shall be displayed to
the user.
00:01:57 In order to solve this exercise, you will need the math library and you'll need
00:02:03 to look for suitable functions inside the math library, using the documentation of the math
library.
00:02:11 So as always with exercises, I suggest you pause the video now and try to solve this exercise
yourself.
00:02:17 And remember, you have to look up in the documentation useful functions you can use to
solve this exercise.
00:02:28 Welcome back. Let's solve the first exercise together.
00:02:32 So what do we need? We need different functions to solve this exercise.
00:02:37 We need one function to calculate the factorial and two functions to calculate the natural
logarithm
00:02:46 and the logarithm to base 10. Let's have a look at the documentation.
00:02:53 Probably the easiest part is to search for the terms we are looking for inside the
documentation.
00:02:58 So I will now search for the term factorial, and sure enough, there is a function called factorial.
00:03:11 And what does this function do? The function factorial
00:03:18 returns x factorial as an integer. It actually calculates exactly what we need.
00:03:27 There's also a little hint here, deprecated since version 3.9 is the possibility to accept floats.
00:03:36 So it's not possible anymore to invoke this method factorial with a float that also has an integer
value, like for example 5.0,
00:03:46 but instead it's only possible to provide integers. So this can be used to solve the first part of
the exercise.
00:03:54 Let's do this. So what do we need?
00:03:58 We need to ask the user first for a number, "Please enter a number: "
00:04:17 And next we, of course, need to convert this number into an integer again,
6
00:04:22 because we want to work with integers here. And the first thing we want to calculate is a
factorial.
00:04:29 So let's call this number_fac. And we want to use the function from the math library.
00:04:40 Therefore, of course, we need to import the math library, this is what I forgot,
00:04:45 import math, and I use an alias for the namespace m, and now we can use the function
factorial
00:04:59 with our number. And finally, we can print the result,
00:05:08 how do we do this? print(number, "! is ", number_fac)
00:05:21 Let's give it a try. If I enter, for example, the number 6,
00:05:28 I get the result 720. It's not so nicely formatted here
00:05:33 because we get the additional space, but that's okay. Start something else.
00:05:39 5 should be 120, and sure enough, it works. Okay, and now we have solved the first part of the
exercise.
00:05:47 We now also need to find functions to calculate the logarithm. Again, let's do the same.
00:05:57 Let's search for logarithm. And we find here the power and logarithmic functions.
00:06:03 If we scroll down a little bit, we find one function that is called log.
00:06:10 And with one argument, this function returns the natural logarithm of x.
00:06:18 That's exactly what we need to solve the first part of the third step.
00:06:24 And further down, we also see that there is a function that is called log10.
00:06:30 And this returns the base 10 logarithms. Those are the two functions we need.
00:06:38 Here's also a little hint that actually this is the same like calling log with a second parameter
00:06:44 but we will load the log10 function instead. So, let's move back to our Jupiter Notebook
00:06:52 and solve the second part of the exercise. So, we need to calculate the natural logarithm.
00:07:00 Let's call this natural_log, and we can use the log function for that.
00:07:12 And we want to have the logarithm to base 10.
00:07:21 And we use the log10 function for that. And also we need to print the results of,
00:07:41 and we add number here, is log. And let's copy this logarithm,
00:08:05 "The base 10 logarithm of" "is", log_base_10. So this should do.
00:08:17 Now, let's give it a try. We enter the number 5 again,
00:08:22 we know that 120 is the factorial of 5, and we get two values.
00:08:32 Natural logarithm of 5 is 1.6, and the logarithm to base 10 is 0.69 something. Let's try another
number.
00:08:41 Let's try for example, 100. This gives us very large factorial,
00:08:45 but we at least see that the logarithm to base 10 is correct because this is a 2.
00:08:53 Okay, that was the first exercise. And of course, this is just one possibility to solve it.
00:09:01 If you used something different, for example, if you didn't use the log10 function here,
00:09:06 but instead the log function was a second parameter, this is still valid solution.
00:09:11 So, let's move to two different libraries, namely the random and the statistics library.
00:09:20 And the exercise two is the following, use functions from the random and the statistics library
00:09:26 to first generate 100 random numbers between 1 and 10. And then you should use functions
00:09:36 from these two libraries to calculate the mean, the median, and the variance of this list.
00:09:43 In order to solve this exercise, you need to explore two libraries, namely the random library.
00:09:52 As the name suggests, this library can be used to generate pseudo-random numbers. And
also the statistics library, this library contains statistical functions.
00:10:04 So you will find something like, for example, the mean to calculate the mean of some data.
00:10:12 Again, I suggest you pause the video and try to solve this exercise yourself.
00:10:18 Try to find suitable functions inside these two libraries. And once you have a solution,
7
00:10:24 you can continue with the video and I will show you one possible solution. So, welcome back.
00:10:32 Let's try to solve this exercise together. So, we will again, solve this exercise in small steps.
00:10:39 So the first thing we need to do is we need to generate a list of 100 random numbers. To do
this, we have to look at the random library.
00:10:49 There's quite some information about random numbers here. But let's scroll down a little bit.
00:10:57 And here we find function for integers. That is what we need.
00:11:02 There is for example, a randint, which returns a random integer such that
00:11:10 a is smaller than or equal to N and smaller than or equal to b, so a random integer in a certain
range.
00:11:19 And we also see that that's just an alias for another function, randrange.
00:11:24 And this is actually something we can already use. This looks like a good approach.
00:11:31 So, let's go back to here and try to create a list of 100 random numbers.
00:11:37 In order to do this, We will need to import random.
00:11:45 I'll import the whole library and also don't provide an alias.
00:11:52 And let's just first try out how this function works. So random.randint, and we need numbers
between 1 and 10, so we provide 1 and 10.
00:12:06 Let's give this a try. We got a random number 2, random number 8, we get a 10,
00:12:13 so seems to work. And we also get a 1.
00:12:17 So, this random function seems to do what we need. And what we now need to create
00:12:23 is a list of 100 random numbers. We could do this differently.
00:12:27 We could use, for example, a for loop. What I will be using here is a list comprehension.
00:12:33 So our random_numbers is a list, and the list contains a random number for every element,
00:12:46 I don't care, in a range of 100. So what this does, we have a range of 100,
00:12:57 so 100 steps. And 100 times just an element from created
00:13:03 to using randint is added to the list. So in the end, we have a list of lengths, 100,
00:13:09 with just 100 random elements. Let's check this by printing random_numbers afterwards.
00:13:22 And of course I have a typo here. And these are 100 random numbers.
00:13:28 Just to make sure, we should also print the length of this random_numbers, just to be sure we
really created 100 random numbers.
00:13:42 And that's the case. So, here we have now solved the first part of this exercise,
00:13:49 namely, to generate 100 random numbers. The next step we need to do is we need to
calculate
00:13:56 the mean value of this list. And of course, we will look in the statistics library for this
functionality.
00:14:04 And if you read through the statistics library, pretty soon, you will find here a function that's
called mean.
00:14:12 And what does mean do? mean returns the arithmetic mean of some data
00:14:18 and the data can be any sequence. So also a list.
00:14:23 And there is even an example, showing us how to use this function together with a list.
00:14:29 That's exactly what we are trying to do right now. So, let's calculate the mean.
00:14:37 We need to import the statistics library as well. import statistics
00:14:45 And that's too long, I provide an alias for this.
00:14:51 And print("The mean value of the list is" and now we can calculate the mean of our list of
random the numbers.
00:15:10 Gives this a try. No, there is still an error.
00:15:14 I again, have a typo. And the mean right now is 5.81.
00:15:22 And as we have random numbers, whenever we execute this, we get different mean values.
8
00:15:29 But they are always somehow close to 5.5, which would be the expected value.
00:15:35 So now, we also need to calculate the median and the variance of this list.
00:15:41 Let's check again for functions. So, of course, there is also a median function.
00:15:50 The median function again works for a list, so that's nice.
00:15:55 And now we also need to find a variance. And if we scroll down a little bit further,
00:16:02 there is also a variance function. And the variance function also can take a list,
00:16:08 so that's exactly what we need. Let's solve the rest of this exercise using the two functions.
00:16:15 I'll copy this to save a little bit typing. "The median value of the list is",
00:16:23 there is the median. And the last part is the variance.
00:16:38 It's called median, not median value. Variance.
00:16:47 And let's give this a try. And we see the median in our list is 5.
00:16:51 And there is a variance of 7.74. And whenever I execute this, I get different values.
00:16:58 And that's proofs that we, indeed, work with random numbers. So again, this is just one
possible solution.
00:17:09 If you solve this exercise differently, for example, not using a list comprehension,
00:17:15 that's still a valid solution. This is just one possibility.
00:17:21 Let's jump back to the slides. What have you learned in this unit?
00:17:26 You have learned about some basic libraries. You have read the documentation
00:17:31 of these libraries yourself. You have worked with these libraries,
00:17:36 by importing these libraries and applying different functions from those libraries to solve
exercises.
00:17:45 Thanks for watching, and see you in one of the next units.
9
Week 6 Unit 4
00:00:05 Hello, welcome to week six, unit four, other standard libraries.
00:00:11 In this unit, I will give you a glimpse into the functionality that's available
00:00:16 as part of the standard library in the Python programming language.
00:00:21 We will, in particular, look into two libraries, namely the calendar library and the csv library.
00:00:30 Without much introduction, let's jump into our Jupyter Notebook.
00:00:35 So, here we are in the Jupyter Notebook, and the first library we will have a look at
00:00:40 is the calendar library. Let's click on it.
00:00:45 I have here the documentation, and the calendar library from the Python standard library
00:00:51 contains calendar-related functions. Let's see what we can do with the calendar library.
00:00:59 Using the calendar library, it's, for example, possible to calculate
00:01:04 what weekday a certain day in the calendar was. So I import the calendar library,
00:01:11 and then I call the weekday function for the 1st of March, 2022.
00:01:21 And as a result, we get a 1. What does this mean?
00:01:28 We can have a look at the calendar documentation right now, but we can also use the
day_name list
00:01:38 inside the calendar library to get a proper day name for the date we want to see.
00:01:46 So, for example, what day was the 1st of January, 2022? And we see it was a Saturday, yeah?
00:01:56 There is a list inside the calendar module which maps the numbers via the index to a weekday
name.
00:02:07 And using this approach, we could, of course, also find out what's the 1st of March,
00:02:12 what day of the week this would be. And that was a Tuesday -
00:02:16 was or is a Tuesday, depending on when you watch this lecture.
00:02:21 But inside the calendar module there's a lot of other useful functionality.
00:02:27 For example, we can easily, with just one function call,
00:02:31 print a calendar for a whole month, or even for a complete year,
00:02:36 to the terminal directly. So here we have a calendar for a whole month,
00:02:42 for the month of March, but we can also print a calendar for the whole year of 2022
00:02:51 and see all of the different days of the weeks and their corresponding date.
00:02:59 I want to point out something important here. We can actually have a look at the source code
00:03:06 of these Python modules, of these Python libraries from the standard library.
00:03:13 So I linked to the source code of the Python standard library right here,
00:03:17 and let's make this a little bit bigger. And actually where I want to go is this function.
00:03:26 And if you have a look at this function, at the prmonth (print month) function,
00:03:32 you will see that the Python standard library itself reuses other Python functions.
00:03:40 For example, the built-in print. So this is just a hint again
00:03:47 to what we've shown you earlier about combining functions. I guess that was in week five, if I
remember correctly.
00:03:56 And also the Python standard library makes excessive use of this approach
00:04:01 to reuse functionality inside other libraries, for example. And if you want, you can, of course,
00:04:08 have a look at other methods yourself, and see how they're implemented inside the Python
language.
00:04:17 There's one more thing relating to the calendar library. You might remember from week one,
00:04:25 there was a little exercise where you had to calculate if a certain year was a leap year.
10
00:04:32 Before trying to implement such functionality yourself, you should always have a look
00:04:37 if there is a functionality available in the Python standard library
00:04:41 that you can use. And it turns out for calculating the leap year,
00:04:45 actually there is. In the calendar module, there is a function called isleap,
00:04:49 which will tell you if a year is a leap year or not. And in the small example here,
00:04:56 I use this isleap function to check if a certain year is a leap year or not.
00:05:02 And let's give this a try. You might remember that 1900 wasn't a leap year
00:05:09 according to this definition, so the result is,
00:05:14 "It is False that 1900 is/was a leap year." You might also remember that according to the
definition,
00:05:21 2000 was a leap year. In this case, our function returns True.
00:05:27 And we can also execute this with, for example, 2016, which was also a leap year.
00:05:36 So this function calendar.isleap, just as a reminder that you should always look first
00:05:42 if there is a functionality available in the Python standard library
00:05:46 that you can reuse before implementing something yourself. So this leads us to the second
library
00:05:54 we will have a look at, the csv library.
00:05:58 Also for the csv library, I linked to the documentation. So the csv library is a library
00:06:04 for reading and writing CSV files. As you might remember from week four,
00:06:10 there are different CSV formats. So for example,
00:06:13 Microsoft Excel uses a semi-colon in a CSV file. Other programs use, for example, a comma in
a CSV file,
00:06:23 and it can be quite complex to handle all this yourself. And therefore all these different
possibilities
00:06:32 to read and write CSV files are abstracted using the csv library.
00:06:39 Well, using the csv library it's very easy to read data from CSV files.
00:06:46 In order to do this, we have provided a little CSV file, which is called students_excel.
00:06:53 I open this up here so that you see the content of this file.
00:06:57 You see, this is a CSV file, it contains lots and lots of data,
00:07:01 again, of students from Harry Potter. And what I can now do using the csv library,
00:07:10 I can open the file first. We need to open the file,
00:07:14 and then I can create a CSV reader using this file. And afterwards, I can read or work with
00:07:22 every row from the reader. And you will see when I execute this
00:07:28 that the csv library takes care of all the handling of the data in the file.
00:07:37 So for each and every element, I get, for example here, a list containing the data.
00:07:48 The one thing you might have noticed, which is not so nice right now,
00:07:52 we have here, so to say, a header line. The first line in our CSV file basically describes the
data.
00:07:58 So the first element is the surname, the second element is the first name, and so on.
00:08:04 But also this functionality is available in the csv library,
00:08:11 so we can use a dictionary reader instead of the reader we just used.
00:08:16 And the dictionary reader creates a dictionary for every line in the CSV file,
00:08:21 except it uses the first line as the keys for the dictionary.
00:08:27 And let's see how this looks. Again, I open the file.
00:08:33 Now I create a dictionary reader. And after that, I can work through all the elements
00:08:40 in this dictionary reader, and let's see how they look.
00:08:44 You see right now we get, for each and every line of our input file,
11
00:08:51 a dictionary, which maps, for example, the surname, the first name, or the mail address
00:09:01 to different elements of a dictionary. So it's very easy right now to, for example,
00:09:06 get the surnames of all the elements in our list. We could go through it row by row
00:09:13 and just select the element "surname". Well, if we actually look
00:09:18 at the documentation of the DictReader, we see that there is just one required parameter,
00:09:27 which is the file. But there are different other parameters,
00:09:31 in particular, for example, the "dialect" parameter, which has certain default values.
00:09:36 The default value for the "dialect" parameter is "excel" So that was the reason why our reader
00:09:42 was able to immediately work with the Excel CSV file.
00:09:50 There are different dialects, different versions, and that's what you can use the "dialect"
argument for.
00:09:58 If you have a different kind of CSV version, you can still work with it using the DictReader,
00:10:04 only you have to specify the correct dialect. Which different dialects are available?
00:10:12 You can have a look at the documentation, and it will describe what are the properties
00:10:16 of these different dialects. Then what we do in this little program,
00:10:23 just to work with the csv library a little bit more, we again read, using a DictReader, our student
file.
00:10:33 And afterwards, for each and every student, we delete the matriculation number after reading
it,
00:10:41 and create another dictionary where we map now the matriculation number
00:10:46 to the dictionary of students. And if we execute this,
00:10:52 we get something which is not very readable, but what you see in here is, for example,
00:10:57 that this matriculation number is mapped to Harry Potter. And that's the entry of Harry Potter,
00:11:06 and we removed the matriculation number from the student record.
00:11:12 This is similar to the approach we took in week three, unit two. Just to show you this once
again.
00:11:20 But using the csv library, it's not only possible to read data from CSV files,
00:11:27 but also to write data to CSV files. And this is what we will be doing next.
00:11:32 We will be using the csv library to write CSV data. And how do we do this?
00:11:39 We use, again, our students from earlier, so the Harry Potter characters.
00:11:46 And then we ask the user for a few matriculation numbers, in this case five, so we do this five
times.
00:11:56 And for each and every matriculation number that the user enters,
00:12:01 we will later on read this data from our dictionary
00:12:08 and write it to a file using a DictWriter. So the dictionary writer is down here.
00:12:16 We have to define which file we need to write to, and open it.
00:12:22 That's up here. Open a file, give it as a name.
00:12:25 And of course we need to open it for writing. Then we specify which field names
00:12:33 we want to write to the file. And then we create our dictionary writer
00:12:41 that takes the output files and the field names. And later on we can just provide a dictionary of
students,
00:12:48 and the DictWriter will take care of writing this data to a file.
00:12:53 So what I will do right now, I will comment this in,
00:12:57 so that we see what's happening inside the program. And I need to look up some matriculation
numbers first.
00:13:08 Let's use, for example, 412620. Execute this.
00:13:16 412610, I guess. Let's try some more.
12
00:13:24 I don't know which matriculation numbers are present in our list.
00:13:30 So I'll just enter a few. And I didn't get any data.
00:13:41 So that was what I was expecting. So let's copy over
00:13:47 a correct matriculation number. Execute this once again.
00:13:56 We just use that one all the time. And sure enough, we get Harry Potter.
00:14:05 Only Harry Potter, so we would now expect that to this file, "some_students",
00:14:12 only the data of Harry Potter is written five times. Let's check this out.
00:14:17 I reload here. We see we have now a new file, "some_students"
00:14:23 And if we open this file, we see only Harry Potter in it. And especially what's important,
00:14:29 that only the fields we wanted to write, namely the surname, the first name, and the email
address,
00:14:37 have been written. And all of this was possible
00:14:39 by just using the dictionary writer DictWriter from the csv library.
00:14:46 So let's switch back to our slides. What have you learned in this unit?
00:14:50 In this unit, I gave you a little overview of other libraries, other than the math libraries,
00:14:56 namely the calendar and the csv libraries. We have played around with a little of the
functionality
00:15:04 in these libraries. Actually, already in these libraries,
00:15:07 there is much more functionality. And if you look in the Python standard documentation,
00:15:13 you will find even more libraries with lots and lots of additional functionality.
00:15:19 So what I showed you in this unit, it's just a small glimpse.
00:15:22 I really encourage you to have a look at the Python documentation
00:15:26 and see which kind of functionality is available. Thanks for watching,
00:15:31 and see you in one of the following units.
13
Week 6 Unit 5
00:00:05 So far, you have used only the standard Python libraries.
00:00:11 What about these additional libraries? First, of course, you have to find the right one.
00:00:18 And there is more than a hundred thousand available libraries in the Python universe.
00:00:26 When you have installed Python at the very beginning, only these standard libraries were
installed.
00:00:33 So again, where can you find these additional libraries? And how can you install them later
on?
00:00:41 Of course one possibility is always to google. If you have a certain problem,
00:00:48 you, for example, would like to do things with the world wide Web and Python,
00:00:53 then maybe you simply start googling for "Python library world wide Web".
00:00:59 An alternative would be to directly jump into this Python Package Index, the PyPI org server.
00:01:07 Here, most of the libraries are collected and can be downloaded from there.
00:01:15 To install actually the library, you have to use this pip command.
00:01:21 Maybe you remember in week zero, you already used this pip command
00:01:26 to install Jupyter Notebooks. It's showtime.
00:01:33 Let's have a brief look into our notebook. Actually the notebook for this unit is quite small.
00:01:40 We simply would like to provide a few links to other sources where you can find libraries.
00:01:48 There is recommendations like the 20 greatest Python libraries
00:01:53 or the 30 best Python libraries you have to know. Google and try to find maybe other lists
00:02:01 of recommended Python libraries. However, we would like to dig
00:02:05 into this Python Package Index. And later on, I will show using the pip command
00:02:11 how libraries can be installed. Let's first have a look at the Python Package Index.
00:02:21 That's the landing page. And what you can see is there are more than 300,000 projects.
00:02:27 Maybe not each project has developed a library of its own,
00:02:32 but yes, that's the number we are talking about. You could browse for these projects.
00:02:38 So if you're searching for a certain topic, you could go in here and you see, for example,
00:02:45 if you would like to program something with communication, email/conferencing,
00:02:50 then you can simply click on those ones. If you prefer, on the other hand,
00:02:56 to do something with the Internet or with games, then this might be the right option.
00:03:04 However, you could, if you have a certain topic already, like, for example, wordcloud,
00:03:11 if you're looking for wordcloud libraries, then you actually just type it in,
00:03:17 and you now see there are quite a few. You could go now into those
00:03:24 and check what information is available. And actually you will find already here how to install it.
00:03:31 If you use a standard configuration which uses pip, then you have simply to type "pip install
wordcloud".
00:03:40 Now I have opened my command shell. You could use your PowerShell as well.
00:03:44 If you use an Apple computer, you open up a terminal. And here you have already used this
pip command,
00:03:51 and let's go a little bit into the details, "pip help". You can use lots of different commands
together with pip.
00:04:01 So far you have used pip install. However, there's other options like uninstall,
00:04:09 config, index, list, and so on and so on. So let's first check if maybe
00:04:16 this wordcloud library is already available. If I type in "pip list",
00:04:22 I'll get a list of all Python libraries which are currently installed on my computer.
14
00:04:28 You see the name of the library on the left, and you see the version number on the right.
00:04:33 And I have chosen wordcloud because it's, from an alphabetical point of view,
00:04:37 at the very end. So it should be somewhere here.
00:04:40 It's not. So let's give it a try:
00:04:43 pip install wordcloud Now our computer checks if this is available.
00:04:53 And as this library is quite small, it really just took a few seconds to install it.
00:05:00 However, if you would like to install big libraries, like, for example, Pandas,
00:05:05 then it can take some minutes to download everything. Again, let's have a look at our list, pip
list.
00:05:14 You see now wordcloud is available in the version 1.8.1. That's what we have seen on this
PyPI server as well.
00:05:25 And we can have a closer look: pip show wordcloud
00:05:33 And you see you will get some additional information. Yeah, it's wordcloud.
00:05:38 This is the version. It's small summary of what it's all about.
00:05:44 Actually some more libraries are required, which have been installed already,
00:05:49 otherwise installing wordcloud, these libraries would have been installed in addition.
00:05:57 So what have you learned? There's lots of nonstandard libraries.
00:06:03 Most of them you could find, for example, using this pypi.org server, or just Google.
00:06:11 And if you have found it, then you still have to install it using pip.
15
Week 6 Unit 6
00:00:05 Welcome to unit six. Now we are talking about non-standard libraries.
00:00:12 In unit five, you have learned how to download and to install these libraries.
00:00:18 Now we'll have a look at a few of those. Some of these libraries are that big
00:00:25 that they justify a complete course of their own. So we can only dig a little bit into these ones.
00:00:33 The first one will be requests, which is a library to communicate with Web servers.
00:00:40 and BeautifulSoup works hand in hand with requests, because BeautifulSoup supports you
00:00:46 in analyzing HTML Web pages. Then, we'll have a look on tkinter.
00:00:54 tkinter is a library which allows you to define and create graphical user interfaces, GUIs.
00:01:02 And finally, we'll have a look at Pandas and Matplotlib. Pandas is a library to really work with
big amounts of data.
00:01:12 For example, if you would like to go for machine learning projects,
00:01:16 then you typically have to handle big amounts of data, and Matplotlib is able to visualize this
data.
00:01:26 So it's showtime. Let's go into the notebook
00:01:30 and we'll see what we can find there. So as said, let's start with requests.
00:01:38 I have before installed and downloaded all these libraries we are talking now about,
00:01:45 and have used the pip command for that. What you can see is here requests,
00:01:51 and requests offers a method get, and we have a parameter, which is a URL,
00:01:58 a uniform resource locator, namely a Web address for a certain Web page.
00:02:03 What you can see is we are pointing at Wikipedia and we are looking for the article about
Python,
00:02:09 and then we do a print with f string, and we simply count how often the word Python is used in
this article.
00:02:20 Let's run this. And you see, the article can be found 1,352 times.
00:02:32 If you're trying out this cell, maybe you will come to a different result.
00:02:39 That's because the article of Python might have changed when you run this code.
00:02:46 If you would like to make a little deeper analysis of this Web page or of any other Web page,
00:02:52 then maybe BeautifulSoup is the library of choice. So you can see here, I have both imported
requests and BeautifulSoup,
00:03:03 and I'm now downloading the title page, the homepage of The Guardian, an English
newspaper,
00:03:11 and then I'm looking for all h3 texts. So when working with the BeautifulSoup,
00:03:19 it is of course of help if you have an idea of how HTML, how hypertext markup language
works.
00:03:30 In there, we have text, for example h3, to indicate here the heading is 3.
00:03:35 And in The Guardian, all these headings, or most of the headings,
00:03:39 are labeled with this h3 text, so we can search for it.
00:03:44 And if you go through it, then you can see you get lots of titles. These are the titles of today's
articles in The Guardian,
00:03:54 and you can see it's about the Russia and Ukraine war. In here, it's lots of corona,
00:04:02 it's lots of politics, sports, but these are all these titles
00:04:07 which you can find on The Guardian today. Again, if you run this code, this will have changed
00:04:15 because, of course, The Guardian updates their articles day by day. Let's have a look on
tkinter.
00:04:26 tkinter is a library to enable you to build graphical user interfaces.
16
00:04:32 So let's first run it. So you see now it's running.
00:04:37 However, the graphical user interface is hiding in the back. So I'm trying to find it.
00:04:44 Here it is. And what have I defined?
00:04:47 You see, I have a graphical user interface with two buttons. If I click on this button, now you
can see in here,
00:04:56 button was pressed, some line is written. If I press it more often, then all the time we enable
00:05:03 a print command. And if I press on quit,
00:05:07 the graphical user interface disappears. Actually, implementing a graphical user interface
00:05:15 is quite easy. The code is a little bit lengthy
00:05:18 but it's not too complicated. What you can see here, here the buttons are defined.
00:05:25 And here, I have defined two functions which are called once the button is pressed.
00:05:35 Let's continue with Pandas. Pandas is good for analyzing a big amount of data.
00:05:43 So let's first run this cell, and you see it takes a little while
00:05:48 because actually, this CSV file contains 100,000 lines of data.
00:05:57 So what you can see here is with info, I first do get an overview of the files.
00:06:04 These are the different columns, I have 100,000 non-null attributes which are of type float64.
00:06:14 I don't want to go into those details. Then I see the head - the first two lines are given in here.
00:06:20 So this is a little bit small now. That's why these two lines, 0 and 1, are printed with a line
break.
00:06:33 You could as well select, let's say, only one column of this data frame
00:06:40 and you can see here, we have the first five entries,
00:06:44 and here we have the last five of the 100,000 entries. So not going into detail on what these
are all about.
00:06:52 And of course, Pandas enables you to not only show the data, but to do calculations.
00:06:58 So what I've done in here is I'm calculating the average, the mean values of all these columns,
00:07:06 and you see the results. Maybe you would not only like to show the results,
00:07:13 but you would like to visualize it, and then Matplotlib is the library of choice.
00:07:20 Let's go in here and run this code, and now you can see from our data frame we have had
above,
00:07:28 we can see one column with all the values. So you can see a bar chart where the values
00:07:36 are simply printed. Actually, the current picture is not so much of interest to details,
00:07:45 it's more that you can simply with a few clicks and with a few lines of code produce such kind
of windows.
00:07:54 Finally, let's have a look on our last Matplotlib example. We import random and we import
Matplotlib once again
00:08:04 to simply show a Gaussian distribution. So what you can see here is I have produced
00:08:13 in here a histogram. I have 100 bins,
00:08:19 and I have had a range of 100,000 random values. If I lower the number of bins and rerun the
code,
00:08:30 you can see now, I have just 10 different bins so the curve doesn't, or the shape doesn't look
00:08:36 as good anymore. If I increase the number of bins,
00:08:40 for example, I go up to let's say 500, it takes a little while.
00:08:49 You see, however, that some of these bars are a little bit... The curve is not that flattened.
00:08:57 The shape is not that flattened. So maybe if we increase the number in here,
00:09:02 we have not 100,000 but 1 million random values, then the average values will flatten down
00:09:11 so we have in our average a finer shape. This is what you can do with all these Matplotlib
17
00:09:20 and with random and with Pandas and so on. So what you can see is there is a multitude of
opportunities
00:09:29 which you can go for. So what did you learn?
00:09:34 I have just tried to show you that there are really powerful libraries outside
00:09:41 and many of them are worth exploring. I could just dig a little bit into it,
00:09:47 but now, for you the world is open. The world of programming, the world of exploring
00:09:53 many more libraries. Go and find and make your own project.
18
Week 6 Unit 7
00:00:05 Congratulations, you have made it up to the last unit of week six. Of course, this week's and
the final assignment
00:00:14 probably still are open and need to be done. But, now you are able to handle the basics of
programming.
00:00:22 You know all about variables and assignments, about control structures, data types, and
whatever.
00:00:30 You are able to structure program with functions. And, on a bigger scale, you are able to use
libraries.
00:00:39 In the last videos, we have shown that now the doors are open
00:00:43 to start real programming projects. So, what is your next project?
00:00:51 I hope if you wanted to get a basic understanding of programming
00:00:54 you have achieved this goal. But maybe you have become fascinated with programming
00:01:00 and now you are looking for more. Then, the world of programming is now open for you.
00:01:07 And programming is not work. Programming is entertaining.
00:01:12 It is like solving a Sudoku or a crossword puzzle. And maybe it's even more interesting.
00:01:19 So, think about your next project. So, what could be a challenge for you?
00:01:24 Maybe to access an open database? Or have you ever thought about implementing your own
Web crawler?
00:01:32 Or how about diving into the big world of machine learning? So, a first step would be that you
look maybe for more exercises.
00:01:42 This usually helps you to sharpen your programming skills and also helps you to understand
00:01:48 how you formalize a problem so you can solve it using a Python program.
00:01:53 And a good start here could be the Advent of Code. This is a competition that takes place
00:02:01 every year before Christmas, during Advent time. You will get a new exercise, day by day,
00:02:09 and people from all over the world are competing. You can find the old competitions,
00:02:16 they are still open, so you can start solving the exercises.
00:02:21 The difficulty range is from medium to difficult, but you will always find hints on the Internet.
00:02:29 So, maybe I have a look there. Before we finish, I have two more things.
00:02:35 Firstly, I would ask a favor. If you haven't filled in the questionnaire for our research project,
00:02:40 it would be great if you could do so. There was a questionnaire at the beginning of the course
00:02:44 and there is now a questionnaire at the end. If you fill in this questionnaire,
00:02:48 it greatly had helps us with our research project. And in addition to that,
00:02:53 we would like that you give us frank feedback about the course.
00:02:57 Please turn to the "I Like, I Wish" section of the course and tell us what you have liked,
00:03:03 but also tell us what were the issues, and what needs to be improved.
00:03:07 If you want, you can also request additional courses for the future,
00:03:11 which might be developed by openSAP. So, that's it from our side.
00:03:17 Thank you, and bye bye. Thank you from my side, as well, and bye.
00:03:21 And hope to see you somewhere.
19
www.sap.com/contactsap
The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors.
National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affi liated companies shall not be liable
for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements
accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality
mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platform directions and functionality are
all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation
to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are
cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other
countries. All other product and service names mentioned are the trademarks of their respective companies. See www.sap.com/trademark for additional trademark information and notices.