Python — 5

Himashi Karunathilake
4 min readDec 9, 2019

--

Hi guys… 😍

In this post I will be continuing our Python series by covering a few complex topics. I hope you guys have completed the previous tutorial which was a little tough (but I provided the answers, anyway) and hope that you guys are thorough with everything I’ve taught before. In case you need to refresh your memory, here are the links to the previous posts:
Python — 4 → https://himashikarunathilake.medium.com/python-4-137dce453aa3
Python — 3 → https://himashikarunathilake.medium.com/python-3-493c1be17a06
Python — 2 → https://himashikarunathilake.medium.com/python-2-5d3b48c8da68
Python — 1 → https://himashikarunathilake.medium.com/python-1-ca8f34d2ee79

Now, let’s get coding!

Modules

A module is a piece of software that is responsible for a specific function. Each module can be separately edited. The name of the module will be the name of the file with .py extension. These modules can have functions, classes and variables, all defined and implemented. In order to include these modules, we can use the following command at the beginning of a file:

Since a module may have several objects, we can always specify a particular object to be imported using the following command:

If we need to import all objects from a module, we can use the wildcard * instead of the object_name. But note that, this will be a little risky as changes in the module might affect the module which imports it.

We can also draw the modules using any name that we prefer using the following command:

There are other ways to load modules as well. One such method is to use the environmental variable, PYTHONPATH, so that you can specify additional directories as well.

Another method to load modules will be to use the sys.path.append(“/path”) function.

Python also has built-in modules that can be used when and where necessary.
e.g., urllib, math

Packages

Packages contain multiple packages and modules themselves. Each package have a special file called __init__.py. This file can be empty as well. All it does is indicate that the said directory contains a Python package and that it can be imported the same way a module is imported.

If we need to load a module from a package, we can do it as follows:

The __init__.py file can also specify which modules to be exported as follows:

Numpy Arrays

These are great alternatives to Python lists as it allows the user to perform calculations across the entire array. To work with numpy arrays, the numpy package should be always imported using import numpy as give_a_preferred_name.

To convert lists to numpy arrays, you can follow the given example below:

If you need to know the type of the numpy array used, you can use the type(array_name) function.

Lambda Function

This is a small anonymous function that can take any number of arguments, but can have only one expression. The syntax of a lambda expression is as follows:

Comparison Between a Normal Function and a Lambda Function

So guys this will actually mark the end of the Python series that I was doing. I made it a point to only cover the most important and basic Python lessons so that you too can do your own research. You can obtain the tutorial for this lesson under the folder Python — 5 from https://github.com/Himashi-Karunathilake/Python. The tutorial for this lesson will actually be a bit harder since now you have to do your own research to code. This tutorial will comprise of several mini projects that you are required to do and the answers will be given as well. So do your best and good luck!

Until I see you in the next one then… Happy Coding! 🥰

Ciao… 👋

Originally published at http://mysnowfrostpersonal.wordpress.com on December 9, 2019.

--

--

Himashi Karunathilake
Himashi Karunathilake

Written by Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!

No responses yet