Python is an interpreted language developed in 1990, characterized by its scripting language nature, dynamic typing, and platform independence. It offers a wide range of libraries, leading to high productivity.
A Python module is a file containing variables, functions, and classes. The `import` keyword is used to bring in other modules and utilize their functionalities. Modules help modularize common features, improving code reusability and productivity.
You can create your own modules to use in your projects. Modules can contain various functions and variables, leading to efficient code management and reusability.
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.
1. What is Python?
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:
Scripting language: Python uses an interpreter that executes the source code line by line.
Dynamic typing: Python doesn't require you to specify data types when declaring variables. From my experience, Python seems quite sensitive to data types, so be careful when using variables; otherwise, you'll encounter errors frequently.
Platform-independent: Python doesn't need to be compiled for each operating system. Once you write the code, you can run it on any operating system.
Python supports a wide variety of libraries, making it highly productive compared to other languages.
2. What are Python Modules?
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: