Functions and Modules in Python
What are the Functions?
In programming, a function is usually a piece of code set together to perform a single task. Working with a complex problem requires a set of instructions to perform different tasks. Functions are independent code that runs when called for a unique input.
Moreover, a function accepts parameters or arguments to work on and provides the solution in return. In Python coding, functions play a significant role when it comes to an understanding of the program. They reduce the code to increase the useability. Furthermore, using a specified function for a similar problem increases the modularity of the program as well.
Functions in Python
Like other programming, languages python uses defined protocols to write a function. It uses the def keyword to declare a function before its name. The body of the function consists of logical statements for problem-solving. And the most important thing is the indentation to avoid any ambiguity. Also, the return statement is optional.
Furthermore, there are mainly two types of functions applicable in Python language. These are built-in functions and UDF. The built-in functions mean the functions already designed for python languages. These can also prove useful in importing numerous python libraries. On the other hand, UDFs are user-defined functions. These are customized by users to solve problems with their solving techniques.
Example
An example to define functions in Python is as follows.
def string_test(s): d={"UPPER_CASE":0, "LOWER_CASE":0} for c in s: if c.isupper(): d["UPPER_CASE"]+=1 elif c.islower(): d["LOWER_CASE"]+=1 else: pass print ("Original String : ", s) print ("No. of Upper case characters : ", d["UPPER_CASE"]) print ("No. of Lower case Characters : ", d["LOWER_CASE"]) string_test('Hello Mr. Rogers, how are you this fine Tuesday?')
The above example is of a function string_test. It is a simple function that takes a testing string and calculates the number of upper and lower case characters. The output of the above function is as follows.
Original String : Hello Mr. Rogers, how are you this fine Tuesday? No. of Upper case characters : 4 No. of Lower case Characters : 33
Another exciting thing in this function is the keyword pass. Pass proves helpful while working with an unimplemented solution or where there is no need to do anything. This keyword also proves immensely helpful in avoiding any unnecessary error in the function.
Modules in Python
Like libraries, there are also Modules in Python. When the user is working with a specific type of problem, a piece of code having specified functionality proves extremely valuable. Therefore, the users can create or use a pre-existing module, which is essentially a file that consists of independent entities that could be functions, variables, classes, or stored databases. Python saves these files using the .py extension and a provided name.
Moreover, the Modules in Python help organize similar code for better usability in future complex tasks. Even python libraries are a set of modules. These libraries consist of modules often written in C language or Python language.
Example
A piece of code written in Python and saved as a .py extension creates a module file, as mentioned above. For example, the users can turn the above-defined function into a module. The function is first written in a separate file and saved with the extension .py.
For instance, the user saves the code in a file named string_test_module.py. Once the system has saved the module, the user can easily use it in his program. Additionally, when the user has imported the whole file, the functions it contains are accessed by using a dot (.) before their name. For example:
Import string_test_module string_test_module.string_test('Hello Mr. Rogers, how are you this fine Tuesday?')
This imported function also produces the same output, as mentioned above. Customized models are quickly built-in Python to make libraries and store them for future usability.
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- PyGame
- PyTorch
- Python & Libraries