Python Programming 101 - Built & External Modules | f-Strings | Dunder Variable | Requirement.txt | Virtual Env

Python Programming 101 - Built & External Modules | f-Strings | Dunder Variable | Requirement.txt | Virtual Env

Python Programming 101 - Built & External Modules | f-Strings | Dunder Variable | Requirement.txt | Virtual Env

Python Programming 101 - Built & External Modules | f-Strings | Dunder Variable | Requirement.txt | Virtual Env

Join our telegram channel and group :)

Telegram Group:
Telegram Channel:

In this post, we will study a few more short topics regarding python programming which will be used in the future at some point in time. At times, when we are writing big complex codes where different python dependencies required then at that time requirement.txt, virtualenv, modules, and package knowledge seems useful. f-Strings are used for formatting in a simpler way and dunder variables are known as the magic variables (defines for a special purpose).


Writing python code into different files in order to maintain reusability.


1. The code is reusable.
2. Easy to manage small chunks of code rather than big complex code.
3. No need to write code from scratch or copying it from the file. Import makes things much simpler and manageable.


Different usage of python modules is mentioned below.

Creation of our own module

1. Write the python program
2. Saved it in a file extension with .py
3. Import the module in another program.
4. Call the function with passing parameter
5. All the steps are implemented in the below example.

Python Programming - Creation of our own module
Step 1,2

Python Programming External Modules
Step 3,4

Importing module
Accessing the properties of various python modules is possible with the help of import. We can import any python properties using the import keyword.

There are different ways to import a module in python are mentioned below:

1. Regular import using the import statements.


import math


2. Renaming the module using "as" keyword in import.


import DemoModule as dm


3. Accessing specific functions from the module using from keyword.


from DemoModule import calc


4. Accessing all functions from the module using * symbol.


from DemoModule import *


Note: Avoid using step 4 because it sometimes creates conflict between different identifiers. 

Built-in Modules: 

Modules which are already pre-built in python are called as built-in modules. 

Features of builtin modules are:

1. Modules are ready to use.
2. Modules can be used for different functions such as file management, os management, computational purposes, and many more.
3. Easy to import and use.

List of Important Built-in Modules:

1. OS module - related to os related tasks such as file creation and directory creation, deletion, and many more.
2. Sys module - related to system operation and python runtime environment.
3. Maths module -  it will help in mathematical related operation such as trigonometric, algebraic expression, and many more.
4. Statistics module - it will help in computation related to statistics processing.
5. Collections module - provides an alternative of the tuple, list, and many more.
6. Random module - it will generate a random number ( mostly used ) required in different parts of a program

Example of the built-in module:

Random Module

Python Programming Random Module

OS Module

Python Programming OS Module


f-Strings are newer way of formatting string in a simple way. 

Things to Ponder: 

1. Need to use curly brackets {variable_name} when defining variable in f-strings.
2. In order to use f-strings, always start the printing statement with prefix "f".
3. We can also use another function from a module in f-strings.

Example of f-Strings:

Python Programming f-strings

Dunder Variables:

Dunder variables are the special variables in the python mostly used for operator overloading. 

Dunder variables can be easily identified within program because double underscores are present in the suffix and prefix of a variable. 

Sometimes they are also called as magic methods because we do not need to call these type of methods explicitly. 

For example:
1.  __add__() called automatically when any type of addition performed either between numbers and between strings.
2. __init__() called automatically and it initialized the object automatically without any explicit function calling for initialization.

For complete list of dunder methods ( special methods ) click here.


Python virtualenv ( or virtual environment) is an important concept because at times in the future we need to deal with a different number of python projects in one organization and each and every project has different sets of requirements and dependencies and to fulfill these dependencies of one project might affect another project. 

What to do in that case? 

One thing is possible in that case to maintain complete isolation between these projects.

Does it increase cost?

No, not at all. 
We can use the apply virtual environment concept in this type of scenario.

How to achieve complete isolation using Python VirtualEnv?

Python virtualenv (or virtual environment) creates a virtual environment for a specific project and maintain the complete isolation from the existing project .i.e. nothing will affect another project if we update or install a different version of python packages as per the requirement.

Python virtualenv (or virtual environment) works on directory structure methods implies that at the time of creating virtual env for a specific project it will create a separate directory for that project and in that directory all python dependencies are present. 

Follow the below set of steps carefully.

1. Installing python virtualenv

pip install virtualenv

2. Verifying the installation

virtualenv --version

3. Creation of a virtual environment

virtualenv random_env

4. Activating the created virtual environment

virtualenv random_env/bin/activate

5. Install the dependency in a virtual environment

Under the random_env virtual environment installing dependencies

random_env [root@linux#] pip install django==1.10

6. Deactivating the virtual environment

random_env [root@linux#] deactivate

All set Python Virtual Environment setup successfully :) !!!


I must say that requirements.txt is a good practice that every developer should follow if someone willing to upload code in an open-source community for code feedback and reviews.

You all have seen requirements.txt file in most of the GitHub users repository. What's this all about? The requirements.txt is a simple text file where all the python dependency is present that is required to run a specific python project. Why it is so important? As we all aware of python virtual env concept, in which we installed specific dependency to a specific project but at times we need to replicate the project to different servers then at that time instead of installing all the dependencies manually we can easily install all the dependency ( Python package ) at once with the help of requirement.txt file.

It is a two-step process.

1. Create a requirements.txt file using the freeze module of the virtual environment of your choice.

pip freeze > requirements.txt

2. Installing multiple packages using requirements.txt

pip install -r requirements.txt

1. requirements.txt file is used in the case where we are dealing with the different virtual environments and want to deploy our code in other servers or want to share the code with someone.
2. Avoid creating requirements.txt file outside the virtual environment otherwise, it will create a dependency file of complete whole python that is installed on your system.

In the next tutorial, we will study file handling.

Click here for the next tutorial.

For Complete Python Programming 101 tutorial series ( Click here ). 

For Queries and Contact there are multiple ways:

1. From telegram search @randomskoolQnA and @randomskool 
2. From Contact Us Page
3. From Comment 

For Daily Updates Please subscribe to the feed. 

For Job-related Updates Please fill the subscriber feedback form.

Join our telegram channel and group :)

Telegram Group:
Telegram Channel:

Till then happy coding !!!

0 Response to "Python Programming 101 - Built & External Modules | f-Strings | Dunder Variable | Requirement.txt | Virtual Env"

Post a Comment

Hey Random,

Please let me know if you have any query :)