Monday, February 28, 2022

Standard modules

Python comes with a library of over 200 standard modules. The exact number varies from one distribution to the other. These modules can be imported into your program. The list of these modules is very extensive but only a few commonly used modules are mentioned here as an example of standard modules:• math: This module provides mathematical functions for arithmetic operations.• random: This module...
Share:

Sunday, February 27, 2022

Loading and initializing a module

Whenever the Python interpreter interacts with an import or equivalent statement, it does three operations, which are described in the next sections.Loading a moduleThe Python interpreter searches for the specified module on a sys.path (to be discussed in the Accessing packages from any location section)...
Share:

Saturday, February 26, 2022

Absolute versus relative import

We have fairly a good idea of how to use import statements. Now it is time to understand absolute and relative imports, especially when we are importing custom or project-specific modules. To illustrate the two concepts, let's take an example of a project with different packages, sub-packages, and modules, as shown next:project├── pkg1│ ├── module1.py│ └── module2.py (contains a function called func1...
Share:

Friday, February 25, 2022

Using the __import__ statement and importlib.import_module statement

The __import__ statement is a low-level function in Python that takes a string as input and triggers the actual import operation. Low-level functions are part of the core Python language and are typically meant to be used for library development or for accessing operating system resources, and are not commonly used for application development.We can use this keyword to import the random library in...
Share:

Thursday, February 24, 2022

Specific import

We can also import something specific (variable or function or class) from a module instead of importing the whole module. This is achieved using the from statement, such as the following:from math import piAnother best practice is to use a different name for an imported module for convenience or sometimes when the same names are being used for different resources in two different libraries. To illustrate...
Share:

Wednesday, February 23, 2022

Using the import statement

The import statement is a common way to import a module. The next code snippet is an example of using an import statement:import mathThe import statement is responsible for two operations: first, it searches for the module given after the import keyword, and then it binds the results of that search to a variable name (which is the same as the module name) in the local scope of the execution. In the...
Share:

Tuesday, February 22, 2022

Modularization to Handle Complex Projects

When you start programming in Python, it is very tempting to put all your program code in a single file. There is no problem in defining functions and classes in the same file where your main program is. This option is attractive to beginners because of the ease of execution of the program and to avoid managing code in multiple files. But a single-file program approach is not scalable for medium-...
Share:

Saturday, February 12, 2022

Memoization

 Basically in memoization we maintain a look up table where solutions arestored so that we don’t have to solve the same sub problem again and again.Instead we solve it once, and store the values so that they can be reused.We know that Fibonacci sequence is:F(n) = F(n-1)+F(n-2) if n>1= n if n...
Share:

Friday, February 11, 2022

Types of recursion

 Recursion can be classified as direct recursion or indirect recursion. If a function makes a call to itself, then it is an example of direct recursion.For example: in the factorial function, the find_factorial() function made a call to itself. Therefore, we can call it an example of direct recursion....
Share:

Thursday, February 10, 2022

Recursion

Recursion in programming can be applied to solve a problem whose solution depends on the solutions to smaller instances of the same problem. We can therefore say that Recursion refers to a function, which can calculate the right answer by first solving a smaller version of its own self and then using that result along with some more computation to get the final answer.Let’s have a look at our example...
Share:

Wednesday, February 9, 2022

More on Lambda functions

The answer to the assignment from previous post isaddFunc = lambda a,b: a+bprint(addFunc(5,2))Now we have a list of countries as:countries = ['India','Mauritius','France','Turkey','Kenya','Hungary']Your task is to use lambda function to print the length of each string in the list countries. This is how you can do this -print(list(map(lambda x: len(x),countries)))Now let us see how to use sort() with...
Share:

Tuesday, February 8, 2022

Lambda functions

Lambda functions are Python’s anonymous functions, that is, they are defined without a name. These functions are defined using lambda keyword instead of def. The syntax for lambda function is as follows:lambda arguments: expressionThe most interesting feature about lambda functions is that it can have any number of arguments but only one expression. The expression is evaluated, and the value is returned....
Share:

Monday, February 7, 2022

Scopes and namespace

Namespace is a container that has all the names (of variables/functions/classes) that you define. You can define same names in different namespaces. A name or a variable exists in a specific area of the code which defines its scope. The information regarding binding between the variables/objects is stored in the namespace. There are three types of namespaces or scopes.1. Built-in Namespace: These...
Share:

Sunday, February 6, 2022

The return statement

The return keyword is used at the end of the function when there is a need to send back the result of the function back to the caller. Software programming is not about printing results all the time. There are some calculations that must be performed behind the scene, hidden from the users. These values are further used in calculations to get the final output that the users desire. The value obtained...
Share:

Friday, February 4, 2022

*args and **kwargs

*args is used when you don’t have any idea about how many arguments you will use. If you don’t know how many parameter you require, then * args is the way to go. Suppose you have decided to go shopping, but you don’t know how many items you are going to buy or how much you are going to spend. So, the expenditure is undecided. You have no idea about how many products you will buy so a function cannot...
Share:

Thursday, February 3, 2022

Functional arguments

Python programmers often use parameters and arguments interchangeably as both are in a way quite similar but as a Python developer, you must understand the difference between the two. Parameters are declared in the function, and arguments are the values passed to a function when it is called. There are five types of functional arguments in Python.Positional argumentsIn positional arguments, that is,...
Share:

Wednesday, February 2, 2022

Functions and Recursion in Python

A function is block of reusable code that is defined to carry out one particular task. Once you understand the concept of function, you will learn about scenarios where a problem can be solved by a function making call to itself. This is known as Recursion.Functions form the most important aspect of programming in Python and is the core topic for any object-oriented programming language. Functions...
Share:

Tuesday, February 1, 2022

Exploring machine learning software

Before we start developing models, we need to few tools to help us. Regardless of whether you are using a Mac, PC, or Linux, almost everything we use is compatible with all platforms. There are three main items we need to install: a language to develop our models in, a database to store our data in, and a cloud computing space to deploy our models in. There is a fantastic technology stack ready to...
Share: