Subject
- #Python Modules
- #Python
Created: 2024-03-27
Created: 2024-03-27 19:56
At my company, I had the opportunity to migrate a PHP-based program to Python.
As the amount of data we needed to process increased, PHP's processing speed became a bottleneck. We decided to migrate to Python because of its advantage in faster data processing.
Python is a language created by Guido van Rossum (born January 31, 1956) in 1990. It's an interpreted language.
Here are some of Python's features:
Python supports a wide variety of libraries, making it highly productive compared to other languages.
In Python, a module is a file containing variables, functions, and classes. When coding in Python, it's convenient to use modules created by others or to group commonly used variables and functions for easier use.
You can use modules with the `import` keyword.
Let's look at how to use the `random` module as an example.
This example shows how to import the `random` module and call the `randrange` function it provides.
It takes two numbers as parameters and returns a random number between them.
You can also create your own modules. Grouping commonly used variables and functions into a common module can reduce code length and improve productivity.
Let's create `module.py` (the module) and `main.py` (which imports the module) to see how this works.
Running this code will output 6.
Modules can contain multiple functions and variables.
To illustrate this, let's examine an example using a module called "wallet".
The `wallet` module has a `balance` variable and three functions: `income`, `spend`, and `getBalance`. The `use_wallet` module uses these three functions. The `global` keyword indicates that the function will use the global variable.
Today, we briefly learned about Python modules.
You can find the code examples from above in the GitHub repository below:
Comments0