This video belongs to the openHPI course Python – schnell und intensiv Programmieren lernen. Do you want to see more?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:00Hi, welcome back. We've already seen a bit of how to
- 00:04import modules. These are additional packages with more Python code than what is now
- 00:10basic one is just included. And there are quite a few that are already
- 00:14built in. So they are preinstalled when you install Python. That means
- 00:20standard library and so they don't need to be installed. Here is a
- 00:24list of the things that are included. But if I want to use them in a script
- 00:29I have to load them. There are two things: install, so download and
- 00:33install and then load. I do the loading with the import function. I
- 00:38import the module math for mathematics. And then I can use a function
- 00:43from it with the module name and a dot math.pi, as you can see here
- 00:49in the code. And I would like to show this code briefly in a demo.
- 00:54I have already prepared that so far. If I run the whole script
- 00:59it asks me for a radius. I can say that I have a
- 01:03radius of four meters. I'm making a large pool in my garden. And then
- 01:08I see that the circumference is 25 meters. That means I need quite a lot of
- 01:12pool wall material. I can also do things like making the pool one meter round,
- 01:16round. round is a function that doesn't come from the math module,
- 01:21it's always been there. And when I start typing it,
- 01:25VS Code also gives me a lot of information. This is especially helpful for later functions
- 01:31And if I do that now and say that was too big for me, my
- 01:35pool is only 3.5 meters, then I see that I now have 22 meters as a circumference.
- 01:44There is a bit of good practice to follow. You sometimes see something like this:
- 01:50just import everything from the math module. And I would highly
- 01:54recommend not to use that. Because it makes the code very
- 01:58difficult to read for other people. Python can handle it, but
- 02:03other people can't. And I always try to write my code for other people
- 02:06And that includes me in three hours and especially
- 02:10three weeks or three years. That means the better method is then
- 02:15at least say, from the math module, import Square Root, so
- 02:20root and pi and things like that. But I don't think that's so good either
- 02:25because I can't see directly in the code that square root is
- 02:29a function from the math module. This is still okay for this example, but
- 02:34especially with later functions that will come from all possible packages,
- 02:37it quickly becomes opaque. That means the ideal solution is to say, import
- 02:44module name dot function name.
- 02:48Now module names are sometimes a bit long and what has been established in
- 02:53Python is that you say, import the module as short name,
- 02:58for example m for math and then I can use m dot pi.
- 03:02This is used a lot, for example, with pandas as pd. In addition to the
- 03:11built-in modules, there are also external modules. I've brought my picture here of
- 03:15a universe of things that exist there.
- 03:18Popular things are, for example, pandas for data science or for
- 03:22Machine Learning, there are many things, even more than what is just there.
- 03:27SciPy is used very often when I do statistical analysis
- 03:31or Django if I want to do some web stuff and then for
- 03:37data visualization things like Matplotlib and seaborn also there.
- 03:41There are more options.
- 03:43These modules can be installed from PyPi, Python Package Index, using a
- 03:50program called pip.
- 03:52This comes with Python already included.
- 03:55You do this in the console or command line, command, terminal, shell,
- 04:01Bash. There are many words that end up meaning roughly the same thing.
- 04:06I've highlighted this in yellow here with pip install and then for example numpy.
- 04:11For macOS, I don't use pip install, but pip3.
- 04:15And then with pip list, I can see which modules I
- 04:19computer.
- 04:21In reality, it makes sense to use a virtual environment, but that
- 04:25is a bit beyond the scope of this course, because we're not going to do anything with
- 04:28external modules.
- 04:29But it makes sense to have heard it now.
- 04:33I also do that on the MacBook with R, because I somehow couldn't get it to work
- 04:37with Python, but I think that's really a last resort.
- 04:40But if you already have R and you know how to use it, then of course it's an option.
- 04:45If I get an ImportError, I usually think that I
- 04:49spelled the name wrong and usually I am right and have to
- 04:51then I have to write it correctly.
- 04:54I have brought a few examples of package usage.
- 04:57For example, I can generate random numbers.
- 05:00To do this, I import the module random and then there is a function random, which
- 05:05generates a decimal number between 0 and 1.
- 05:08There is also a function called randint, which then generates whole numbers.
- 05:121, 2, 3, 4, 5, 6.
- 05:13So this is handy for rolling dice when you don't have a dice handy.
- 05:17I can also count things.
- 05:19There is a module for that, collections.
- 05:21That's built in too.
- 05:22I can import it directly, without installing.
- 05:25And if I have a list of different colors, then I can pass this list
- 05:29to Counter and I can pass that on to something like most
- 05:34common to see which are the most common and I might just want to
- 05:37see the three most common ones and the result looks like this.
- 05:40There are three blue, two red and one yellow.
- 05:45Besides modules, I can also read and execute local files.
- 05:49And the syntax is the same.
- 05:51So this fits in nicely.
- 05:54If I have a working directory, a path, a folder or a
- 05:58current working directory, I can see that with os for operating system,
- 06:04getcwd, get the current working directory.
- 06:11I always have a working directory, but if I want to know,
- 06:14I can do that.
- 06:16Then I can also say that when I'm on Windows, which uses a backslash
- 06:19as a path separator, which doesn't work at all,
- 06:23then we simply replace it with a normal slash.
- 06:26And then I already have an output like that where I am.
- 06:29If I have a file in this location,
- 06:33I have now called mine script.py as an example.
- 06:36Most of the time you have real filenames.
- 06:38And in there is a bit of code called number equals 25.
- 06:43Then I can import the number from my script
- 06:47and then I can continue calculating with it. Print number plus two.
- 06:50So in reality, I would have a longer piece of code in this script,
- 06:54that comes out at the end with a number.
- 06:57And I can use the other syntax as well.
- 07:01Import myscript and then myscript dot count.
- 07:04In both cases, I don't actually specify the file name.
- 07:08The file name has a period at the end.
- 07:11This is omitted during import.
- 07:12You only need to know this once.
- 07:16These Python files may contain various objects
- 07:19and I want to look at them without opening the file.
- 07:22Then I can import my script
- 07:24and I can query the directory with you,
- 07:30that is, the objects and things in this script.
- 07:35And to make it look nice, I'll join them with a dot join.
- 07:40We'll go into that a bit more in the next lesson.
- 07:42But then you've seen it once and then output it with Print.
- 07:46And then I see I have a bunch
- 07:50of stuff that has some underscores in it,
- 07:52we can ignore those for now.
- 07:54But then the actual things in this script are things like anzahl, job,
- 07:58random, simuliere_job.
- 08:01simulate_job already sounds like a verb.
- 08:03Maybe it's a function.
- 08:05Let's take a look at that.
- 08:06With Type, we see that this is a function.
- 08:10Function names with a verb are always good.
- 08:13I told you so.
- 08:15So I can use that.
- 08:16From my script point simulate_job exactly the same system
- 08:20as module point function.
- 08:22And then I find out whether I'm a coder or a doctor.
- 08:26I'm not, but it would be nice if I were.
- 08:30I can import another module, inspect.
- 08:33And with that, I can use the getsource function
- 08:37to view the source code of simuliere_job.
- 08:40All without opening my script, because it might be a very long
- 08:44and detailed script and I really just want to look at the code of this function
- 08:47simulate_job function.
- 08:49And in this case it looks like this.
- 08:50I have a list of jobs and then the random module is used to
- 08:54select one at random.
- 08:58If there are arguments, which is not the case now when simulating a job,
- 09:02then the following code is very useful for looking at the arguments.
- 09:05getfullargspec, so get the full or query
- 09:10the full argument specifications.
- 09:13In this case, a bit of empty and uninformative output is now coming out.
- 09:18So we have seen a lot in this lesson.
- 09:21We can import modules and scripts.
- 09:24Import package as pak, if I want to, with an abbreviation.
- 09:28And then I can use the pak dot function, so module name dot function to work with it.
- 09:34I can also import a function from a package.
- 09:38Then I can use the function directly.
- 09:40But in longer scripts, this makes it a bit more difficult
- 09:42find out which package it comes from, because then I always have to go to the top,
- 09:45where it was imported.
- 09:48With pip install I can install external packages.
- 09:51I do that in the terminal or the console, which is by default in VS Code
- 09:56at the bottom, the thing where the output is usually.
- 09:59And then I can import an object from a local file
- 10:03with both options that are available.
- 10:05And we have seen that inspect get_source can show me the source code
- 10:09of the local function, which is very convenient.
- 10:13As usual, we have tasks for this.
- 10:16If you have any questions, please feel free to ask them in the forum.
- 10:20We are happy to improve it if something is unclear.
- 10:23And as always, you can mark the content of this lesson in your RefCard.
- 10:28It always looks so nice and colorful.
- 10:31I have it here again as a reminder.
- 10:33So that's a pleasure.
- 10:36And I wish you the same now when you work on the tasks.
- 10:38Good luck.
To enable the transcript, please select a language in the video player settings menu.
About this video
Ich hatte es in der letzen Lektion nicht wirklich gesagt, daher nochmal explizit: Im Idealfall sind Funktionsnamen Verben - eine Funktion tut etwas.