📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #16 – Модули в языке Питон. Создание и работа с модулями

Школа itProger / Программирование21:05

Transcription

Hello! In this lesson, we will talk about modules in the Python language. We will learn how to create our own modules and explore the built-in modules of Python.

Before we begin, I would like to recommend the website "idiprovercheck.com." On this site, you will find your homework and a lot of other useful information. The link to this lesson will be in the description of this video.

The Python language itself is a powerful tool, but its full potential is revealed when we start using modules. A module is essentially a file or a set of files that we can include in our project, thereby adding new functionality to it.

In Python, we can work with modules in different ways. First, we can create our own file or set of files, which will serve as our module. We can then connect this module to our current project or future projects. In this lesson, we will do just that: we will create a new file and then connect it in another file. In the future, you will be able to connect this same file in various other projects.

The second way is that, in addition to our own modules, we can also use built-in modules that exist in Python. Just as there are functions like `print` in Python, there are many built-in modules. We will familiarize ourselves with some of the main modules during this lesson, learning how to connect and use their functionalities.

The third way to work with modules is to use various libraries, which are essentially the same modules developed by third-party developers. You can find all these modules, libraries, and frameworks on the official website "pypi.org," which is the official site for the package manager pip. The package manager is an application that is installed along with Python, allowing you to quickly install various libraries and frameworks into your project.

During this lesson, we will also try to install something in our project using the pip package manager and even attempt to use a main library for our purposes. But let's take it step by step. First, we will look at how to use built-in modules, then we will explore how to create our own modules, and finally, we will see how to use various modules and libraries that can be installed via the package manager.

There are indeed a vast number of built-in modules in Python, and if we start reviewing them all, we wouldn't even have enough time in a one-hour video to list them. Some modules are used very rarely, while others are used quite frequently. For example, we will study some commonly used modules that you should know about.

Of course, we won't cover all the functionalities within these modules, but I will explain what these modules do and what useful information we can extract from them.

Let's start by looking at a module that allows us to work with time. This module is called `time`. To import it, we first need to specify the import directive, and then we simply state what we are importing. Since this module is built-in, there is no need to download or install anything additional; we just specify that we are importing the module called `time`.

Additionally, there is another module called `datetime`, which is similar but allows us to work not only with time but also with dates. To access any module, you need to refer to it by name. In my case, that would be `time`. After that, you need to use a dot to access all the functions and values within the module.

As you can see, there are many different values available. Let's try using a function called `sleep`. This function allows us to pause the program for a specified number of seconds. Let's say I want to pause for three seconds. Below, I will try to print some information, for example, "Hello." If I run this now, nothing will be displayed immediately, and only after three seconds will the text appear. So, the `sleep` function allows us to freeze the program for a specified number of seconds. You can specify how many seconds you want, and then the program will continue to run. This is a very useful function that is often used.

In a similar way, you can access other functions. Now, let's try using the `datetime` module, as it is even more interesting. The `datetime` module allows us to get the current date, and it will be displayed in the correct format. We can also get the current time.

So, I imported the `datetime` module. To refer to this module, I will need to type out the long name each time, but I would prefer to use a shorter alias. To do this, I will import the `datetime` module and specify an alias using the keyword `as`. I will use the alias `dt`.

Now, instead of referring to `datetime`, I can simply use `dt`. This makes it much easier to access the necessary data. Let's try to get not just the date but also the time. We will refer to `dt` and use a function that allows us to get the current date and time.

Of course, we want to print this in the terminal, so we will use `print`. If I run this, I will see the current time displayed, including milliseconds. Similarly, you can extract just the date or any specific part of the date, like the current day or month.

The `time` module allows us to work with both the current date and time, as well as with future or past dates and times. There are many different functions available, but we won't cover all of them right now. Instead, let's look at other modules and how to connect them.

Now, suppose you are interested in getting information about the user. To do this, you can use modules like `os` and `sys`. To import them, you can create a new line of code and write `import os, sys`. This way, you can import multiple modules at once.

You can also shorten this by importing everything in one line. For example, I can import `datetime` with an alias, import `sys` without an alias, and also import `os`. Now, I can work with each of these modules easily.

Let's say we want to access the `path` property from the `sys` module. This property gives us the full path to the current file. If I run this, I will see the complete path to my current project and file. If you are on Windows, it will show something like `C:\` followed by folders.

To get information about the computer's characteristics, we can use the `platform` module. For example, we can access the `system` function to get the name of the current operating system. If I run this, it will show the name of the OS I am using.

If you need to find a random number, you can import the `random` module. If you need to work with arrays, you can import the `array` module. For mathematical functions, you can use the `math` module, and so on. All these main modules are listed in the official documentation, and you can also find them through Google.

If you want to import only specific functions from a module, you can do that too. For example, if I want to import only the `sqrt` function from the `math` module, I can write `from math import sqrt`. This way, I can use `sqrt` directly without referring to the entire module.

Now, let's create our own module. We can simply create a new file or a folder with new files, and then we can import this file or folder into our main file, effectively connecting our own module.

Let's create a new file called `my_module.py`. In this file, we can define some values, like a variable `name`, and create a function called `hello` that prints "Hello" along with the value of `name`. We can also create another function that takes three parameters and checks if they are not equal to zero before returning their sum.

Now, to import this module, we will write `import my_module`. If needed, I can also add an alias, like `m`. Now, I can access the variable `name` or call the `hello` function from my module.

Let's also try importing specific parts of the module. I can use the `from` directive to import just the `add_numbers` function from `my_module`. This way, I can use it directly without referring to the entire module.

Finally, let's work with the pip package manager. As I mentioned, pip is installed automatically with Python. You can use it to install various libraries and frameworks developed by third-party developers. You can find these libraries on the official site "pypi.org."

For example, if you want to develop websites using Python, you might need a framework called Django. You can find the installation command and a brief description on its page.

To test the installation process, I chose a fun library called `cow-say`, which simply makes a cow say something. To install it, I will run a command in the terminal. If you encounter an error, you might need to add `--user` to the command.

Once the library is installed, I can import it and use it in my project. For example, I can make the cow say "Hello."

In conclusion, we have covered how to use built-in modules, create our own modules, and work with the pip package manager to install libraries.

I hope you enjoyed the lesson! If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video. That's all for now. See you soon! Goodbye!