Search

Python Programming Filter

How To Run Your Python Scripts

If you are planning to enter the world of Python programming, the first and the most essential skill you should learn is knowing how to run Python scripts and code. Once you grab a seat in the show, it will be easier for you to understand whether the code will actually work or not.Python, being one of the leading programming languages, has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. Also, it is the language of choice for working with large datasets and data science projects. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.What is the difference between Code, Script and Modules?In computing, the code is a language that is converted from a human language into a set of ‘words’ which the computer can understand. It is also referred to as a piece of statements together which forms a program. A simple function or a statement can also be considered a code.On the other hand, a script is a file consisting of a logical sequence of instructions or a batch processing file that is interpreted by another program instead of the computer processor.In simple terms, a script is a simple program, stored in a plain file text which contains Python code. The code can be directly executed by the user. A script is also called a top-level-program-file. A module is an object in Python with random attributes that you can bind and reference.Is Python a Programming Language or a Scripting Language?Basically, all scripting languages are considered to be programming languages. The main difference between the two is that programming languages are compiled, whereas scripting languages are interpreted. Scripting languages are slower than programming languages and usually sit behind them. Since they only run on a subset of the programming language, they have less access to a computer’s local abilities. Python can be called a scripting language as well as a programming language since it works both as a compiler and an interpreter. A standard Python can compile Python code into bytecodes and then interpret it just like Java and C.However, considering the historical relationship between the general purpose programming language and the scripting language, it will be more appropriate to say that Python is a general-purpose programming language which works nicely as a scripting language too.The Python InterpreterThe Interpreter is a layer of software that works as a bridge between the program and the system hardware to keep the code running. A Python interpreter is an application which is responsible for running Python scripts.The Python Interpreter works on the Read-Eval-Print-Loop (REPL) environment.Reads the command.Evaluates the command.Prints the result.Loops back and process gets repeated.The interpreter terminates when we use the exit() or quit() command otherwise the execution keeps on going.A Python Interpreter runs code in two ways— In the form of a script or module.In the form of a piece of code written in an interactive session.Starting the Python InterpreterThe simplest way to start the interpreter is to open the terminal and then use the interpreter from the command-line.To open the command-line interpreter:On Windows, the command-line is called the command prompt or MS-DOS console. A quicker way to access it is to go to Start menu → Run and type cmd.On GNU/Linux, the command-line can be accessed by several applications like xterm, Gnome Terminal or Konsole.On MAC OS X, the system terminal is accessed through Applications → Utilities → Terminal. Running Python Code InteractivelyRunning Python code through an interactive session is an extensively used way. An interactive session is an excellent development tool to venture with the language and allows you to test every piece of Python code on the go.To initiate a Python interactive session, type python in the command-line or terminal and hit the ENTER key from the keyboard.An example of how to do this on Windows:C:\users>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>>The >>> on the terminal represents the standard prompt for the interactive mode. If you do not see these characters, you need to re-install Python on your system.The statements you write when working with an interactive session are evaluated and executed immediately:print('HELLO WORLD!') HELLO WORLD! 2 + 3 5 print('Welcome to the world of PYTHON') Welcome to the world of PYTHON The only disadvantage is when you close the interactive session, the code no longer exists.Running Python Scripts by the InterpreterThe term Python Execution Model is given to the entire multi-step process to run Python scripts.At first, the statements or expressions of your script are processed in a sequential manner by the interpreter. Then the code is compiled into a form of instruction set called the bytecode.Basically, the code is converted into a low-level language known as the bytecode. It is an intermediate, machine-independent code which optimizes the process of code execution. So, the interpreter ignores the compilation step when executing the code for the next time.Finally, the interpreter transfers the code for execution.The Python Virtual Machine (PVM) is the ultimate step of the Python interpreter process. It is a part of the Python environment installed in your system. The PVM loads the bytecode in the Python runtime and reads each operation and executes them as indicated. It is the component which actually runs your scripts.Running Python Scripts using Command-LineThe most sought after way of writing a Python program is by using a plain text editor. The code written in the Python interactive session is lost once the session is closed, though it allows the user to write a lot of lines of code. On Windows, the files use the .py extension.  If you are at the beginning of working with Python, you can use editors like Sublime or Notepad++ which are easy-to-use or any other text editors.Now you need to create a test script. In order to do that, open your most suited text editor and write the following code:print('Hello World!')Then save the file in your desktop with the name first_script.py or anything you like. Remember you need to give the .py extension only.Using python commandThe most basic and the easy way to run Python scripts is by using the python command. You need to open a command-line and type the word python followed by the path to your script file, like this:python first_script.py Hello World!Then you hit the ENTER button from the keyboard and that's it. You can see the phrase Hello World! on the screen. Congrats! You just ran your first Python script. However, if you do not get the output, you might want to check your system PATH and the place where you saved your file. If it still doesn’t work, re-install Python in your system and try again.Redirecting outputWindows and Unix-like systems have a process called stream redirection. You can redirect the output of your stream to some other file format instead of the standard system output. It is useful to save the output in a different file for later analysis.An example of how you can do this:python first_script.py > output.txtWhat happens is your Python script is redirected to the output.txt file. If the file doesn’t exist, it is systematically created. However, if it already exists, the contents are replaced.Running modules with the -m optionA module is a file which contains the Python code. It allows you to arrange your Python code in a logical manner. It defines functions, classes, and variables and can also include runnable code.If you want to run a Python module, there are a lot of command-line options which Python offers according to the needs of the user. One of which is the command  python -m <module-name>. It searches the module name in the sys.path and runs the content as __main__:python -m first_script Hello World!Note that the module-name is a module object and not any string.Using Script FilenameWindows makes use of the system registers and file association to run Python scripts. It determines the program needed to run that particular file. You need to simply enter the file-name containing the code.An example on how to do this using command prompt:C:\Users\Local\Python\Python37> first_script.py Hello World!On GNU/Linux systems, you need to add a line before the text— #!/usr/bin/env python. Python considers this line nothing but the operating system considers it everything. It helps the system to decide what program should it use to run the file.The character combination #! known as hashbang or shebang is what the line starts with, which is then followed by the interpreter path.Finally, to run scripts, assign execution permissions and configure the hashbang line and then simply type the filename in the command line:#Assign the execution permissions chmod +x first_script.py #Run script using its filename ./first_script.py Hello World!However, if it doesn’t work, you might want to check if the script is located in your currentworking directory or not. Otherwise, you can use the path of the file for this method. Running Python Scripts InteractivelyAs we have discussed earlier, running Python scripts in an interactive session is the most common way of writing scripts and also offers a wide range of possibilities.Using importImporting a module means loading its contents so that it can be later accessed and used. It is the most usual way of invoking the import machinery. It is analogous to #include in C or C++. Using import, the Python code in one module gets access to the code in another module. An implementation of the import:import first_script Hello World!You can see its execution only when the module contains calls to functions, methods or other statements which generate visible output.One important thing to note is that the import option works only once per session. This is because these operations are expensive.For this method to work efficiently, you should keep the file containing the Python code in your current working directory and also the file should be in the Python Module Search Path (PMSP). The PMSP is the place where the modules and packages are imported.You can run the code below to know what’s in your current PSMP:import sys for path in sys.path: print(path)\Users\Local\Python37\Lib\idlelib \Users\Local\Python37\python37.zip \Users\Local\Python37\DLLs \Users\Local\Python37\lib \Users\Local\Python37 \Users\Local\Python37\lib\site-packagesYou’ll get the list of directories and .zip files where your modules and packages are imported.Using importlibimportlib is a module which is an implementation of the import statement in the Python code. It contains the import_module whose work is to execute any module or script by imitating the import operation.An example to perform this:import importlib importlib.import_module('first_script') Hello World! <module 'first_script' from '\Users\Local\Python37\first_script.py'>importlib.reload() is used to re-import the module since you cannot use import to run it for the second time. Even if you use import after the first time, it will do nothing. importlib.reload() is useful when you want to modify and test your changes without exiting the current session.The following code shows that:import first_script #First import Hello World! import first_script import importlib #Second import does nothing importlib.reload(first_script) Hello World! <module 'first_script' from '\Users\Local\Python37\\first_script.py'>However, you can only use a module object and not any string as the argument of reload(). If you use a string as an argument, it will show a TypeError as follows:importlib.reload(first_script)Traceback (most recent call last): ... ...   raise TypeError("reload() argument must be a module") TypeError: reload() argument must be a moduleUsing runpy.run_module() and runpy.run_path()The Python Standard Library has a module named runpy. run_module() is a function in runpy whose work is to execute modules without importing them in the first place. The module is located using import and then executed. The first argument of the run_module() must contain a string:import runpy runpy.run_module(mod_name='first_script') Hello World! {'__name__': 'first_script',     ... '_': None}}Similarly, runpy contains another function run_path() which allows you to run a module by providing a location.An example of such is as follows:import runpy runpy.run_path(file_path='first_script.py') Hello World! {'__name__': '<run_path>',     ... '_': None}}Both the functions return the globals dictionary of the executed module.Using exec()Other than the most commonly used ways to run Python scripts, there are other alternative ways. One such way is by using the built-in function exec(). It is used for the dynamic execution of Python code, be it a string or an object code.An example of exec() is:exec(open('first_script.py').read()) Hello World!Using py_compilepy_compile is a module which behaves like the import statement. It generates two functions— one to generate the bytecode from the source file and another when the source file is invoked as a script.You can compile your Python script using this module:import py_compile py_compile.compile('first_script.py'  '__pycache__\\first_script.cpython-37.pyc' The py_compile generates a new subdirectory named "__pycache__" if it doesn’t already exist. Inside the subdirectory, a Compiled Python File (.pyc) version of the script file is created. When you open the .pyc file, you can see the output of your Python script.Running Python Scripts using an IDE or a Text EditorAn Integrated Development Environment (IDE) is an application that allows a developer to build software within an integrated environment in addition to the required tools.You can use the Python IDLE, a default IDE of the standard Python Distribution to write, debug, modify, and run your modules and scripts. You can use other IDEs like Spyder, PyCharm, Eclipse, and Jupyter Notebook which also allow you to run your scripts inside its environment.You can also use popular text editors like Sublime and Atom to run Python scripts.If you want to run a Python script from your IDE or text editor, you need to create a project first. Once it is created, add your .py file to it or you can just simply create one using the IDE. Finally, run it and you can see the output in your screen.Running Python Scripts from a File ManagerIf you want to run your Python script in a file manager, all you need to do is just double-click on the file icon. This option is mainly used in the production stage after you have released the source code.However, to achieve this, some conditions must be met:On Windows, to run your script by double-clicking on them, you need to save your script file with extension .py for python.exe and .pyw for pythonw.exe.If you are using the command-line for running your script, you might likely come  through a situation where you’ll see a flash of a black window on the screen. To avert this, include a statement at the tail of the script — input(‘Enter’). This will exit the program only when you hit the ENTER key. Note that the input() function will work only if your code is free of errors.On GNU/Linux and other Unix-like systems, your Python script must contain the hashbang line and execution permissions. Otherwise, the double-click trick won’t work in a file manager.Though it is easy to execute a script by just double-clicking on the file, it isn’t considered a feasible option because of the limitations and dependency factors it comes with, like the operating system, the file manager, execution permissions, and also the file associations.So it is suggested to use this option only after the code is debugged and ready to be in the production market.ConclusionWorking with scripts has its own advantages like they are easy to learn and use, faster edit and run, interactivity, functionality and so on. They are also used to automate complex tasks in a simplified manner.In this article, you have learned to run your Python scripts using:The terminal or the command-line of the operating system.The Python Interactive session.Your favorite IDE or text editor.The system file manager.Here, you have gathered the knowledge and skills of how to run your scripts using various techniques.You will feel more comfortable working with larger and more complex Python environments which in turn will enhance the development process and increase efficiency. You can learn more about such techniques as KnowledgeHut offers Python Certification Course.

How To Run Your Python Scripts

6520
How To Run Your Python Scripts

If you are planning to enter the world of Python programming, the first and the most essential skill you should learn is knowing how to run Python scripts and code. Once you grab a seat in the show, it will be easier for you to understand whether the code will actually work or not.

Python, being one of the leading programming languages, has relatively easy syntax which makes it even easier for the ones who are in their initial stage of learning the language. Also, it is the language of choice for working with large datasets and data science projects. Get certified and learn more about Python Programming and apply those skills and knowledge in the real world.

What is the difference between Code, Script and Modules?

In computing, the code is a language that is converted from a human language into a set of ‘words’ which the computer can understand. It is also referred to as a piece of statements together which forms a program. A simple function or a statement can also be considered a code.

On the other hand, a script is a file consisting of a logical sequence of instructions or a batch processing file that is interpreted by another program instead of the computer processor.

In simple terms, a script is a simple program, stored in a plain file text which contains Python code. The code can be directly executed by the user. A script is also called a top-level-program-file

A module is an object in Python with random attributes that you can bind and reference.

Is Python a Programming Language or a Scripting Language?

Is Python a Programming Language or a Scripting Language?

Basically, all scripting languages are considered to be programming languages. The main difference between the two is that programming languages are compiled, whereas scripting languages are interpreted

Scripting languages are slower than programming languages and usually sit behind them. Since they only run on a subset of the programming language, they have less access to a computer’s local abilities. 

Python can be called a scripting language as well as a programming language since it works both as a compiler and an interpreter. A standard Python can compile Python code into bytecodes and then interpret it just like Java and C.

However, considering the historical relationship between the general purpose programming language and the scripting language, it will be more appropriate to say that Python is a general-purpose programming language which works nicely as a scripting language too.

The Python Interpreter

The Interpreter is a layer of software that works as a bridge between the program and the system hardware to keep the code running. A Python interpreter is an application which is responsible for running Python scripts.

The Python Interpreter works on the Read-Eval-Print-Loop (REPL) environment.

  • Reads the command.
  • Evaluates the command.
  • Prints the result.
  • Loops back and process gets repeated.

The interpreter terminates when we use the exit() or quit() command otherwise the execution keeps on going.

A Python Interpreter runs code in two ways— 

  • In the form of a script or module.
  • In the form of a piece of code written in an interactive session.

Starting the Python Interpreter

The simplest way to start the interpreter is to open the terminal and then use the interpreter from the command-line.

To open the command-line interpreter:

  • On Windows, the command-line is called the command prompt or MS-DOS console. A quicker way to access it is to go to Start menu  Run and type cmd.
  • On GNU/Linux, the command-line can be accessed by several applications like xterm, Gnome Terminal or Konsole.
  • On MAC OS X, the system terminal is accessed through Applications → Utilities → Terminal

Running Python Code Interactively

Running Python code through an interactive session is an extensively used way. An interactive session is an excellent development tool to venture with the language and allows you to test every piece of Python code on the go.

To initiate a Python interactive session, type python in the command-line or terminal and hit the ENTER key from the keyboard.

An example of how to do this on Windows:

C:\users>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>

The >>> on the terminal represents the standard prompt for the interactive mode. If you do not see these characters, you need to re-install Python on your system.

The statements you write when working with an interactive session are evaluated and executed immediately:

print('HELLO WORLD!')
HELLO WORLD!
2 + 3
5
print('Welcome to the world of PYTHON')
Welcome to the world of PYTHON 

The only disadvantage is when you close the interactive session, the code no longer exists.

Running Python Scripts by the Interpreter

The term Python Execution Model is given to the entire multi-step process to run Python scripts.

  1. At first, the statements or expressions of your script are processed in a sequential manner by the interpreter. 
  2. Then the code is compiled into a form of instruction set called the bytecode.
    Basically, the code is converted into a low-level language known as the bytecode. It is an intermediate, machine-independent code which optimizes the process of code execution. So, the interpreter ignores the compilation step when executing the code for the next time.
  3. Finally, the interpreter transfers the code for execution.
    The Python Virtual Machine (PVM) is the ultimate step of the Python interpreter process. It is a part of the Python environment installed in your system. The PVM loads the bytecode in the Python runtime and reads each operation and executes them as indicated. It is the component which actually runs your scripts.

Running Python Scripts using Command-Line

The most sought after way of writing a Python program is by using a plain text editor. The code written in the Python interactive session is lost once the session is closed, though it allows the user to write a lot of lines of code. On Windows, the files use the .py extension.  

If you are at the beginning of working with Python, you can use editors like Sublime or Notepad++ which are easy-to-use or any other text editors.

Now you need to create a test script. In order to do that, open your most suited text editor and write the following code:

print('Hello World!')

Then save the file in your desktop with the name first_script.py or anything you like. Remember you need to give the .py extension only.

Using python command

The most basic and the easy way to run Python scripts is by using the python command. You need to open a command-line and type the word python followed by the path to your script file, like this:

python first_script.py
Hello World!

Then you hit the ENTER button from the keyboard and that's it. You can see the phrase Hello World! on the screen. Congrats! You just ran your first Python script. 

However, if you do not get the output, you might want to check your system PATH and the place where you saved your file. If it still doesn’t work, re-install Python in your system and try again.

Redirecting output

Windows and Unix-like systems have a process called stream redirection. You can redirect the output of your stream to some other file format instead of the standard system output. It is useful to save the output in a different file for later analysis.

An example of how you can do this:

python first_script.py > output.txt

What happens is your Python script is redirected to the output.txt file. If the file doesn’t exist, it is systematically created. However, if it already exists, the contents are replaced.

Running modules with the -m option

A module is a file which contains the Python code. It allows you to arrange your Python code in a logical manner. It defines functions, classes, and variables and can also include runnable code.

If you want to run a Python module, there are a lot of command-line options which Python offers according to the needs of the user. One of which is the command 

 python -m <module-name>. 

It searches the module name in the sys.path and runs the content as

 __main__:
python -m first_script
Hello World!

Note that the module-name is a module object and not any string.

Using Script Filename

Windows makes use of the system registers and file association to run Python scripts. It determines the program needed to run that particular file. You need to simply enter the file-name containing the code.

An example on how to do this using command prompt:

C:\Users\Local\Python\Python37> first_script.py
Hello World!

On GNU/Linux systems, you need to add a line before the text— #!/usr/bin/env python. Python considers this line nothing but the operating system considers it everything. It helps the system to decide what program should it use to run the file.

The character combination #! known as hashbang or shebang is what the line starts with, which is then followed by the interpreter path.

Finally, to run scripts, assign execution permissions and configure the hashbang line and then simply type the filename in the command line:

#Assign the execution permissions
chmod +x first_script.py
#Run script using its filename
./first_script.py
Hello World!

However, if it doesn’t work, you might want to check if the script is located in your current

working directory or not. Otherwise, you can use the path of the file for this method. 

Running Python Scripts Interactively

As we have discussed earlier, running Python scripts in an interactive session is the most common way of writing scripts and also offers a wide range of possibilities.

Using import

Importing a module means loading its contents so that it can be later accessed and used. It is the most usual way of invoking the import machinery. It is analogous to #include in C or C++. Using import, the Python code in one module gets access to the code in another module. 

An implementation of the import:

import first_script
Hello World!

You can see its execution only when the module contains calls to functions, methods or other statements which generate visible output.

One important thing to note is that the import option works only once per session. This is because these operations are expensive.

For this method to work efficiently, you should keep the file containing the Python code in your current working directory and also the file should be in the Python Module Search Path (PMSP). The PMSP is the place where the modules and packages are imported.

You can run the code below to know what’s in your current PSMP:

import sys
for path in sys.path:
print(path)
\Users\Local\Python37\Lib\idlelib
\Users\Local\Python37\python37.zip
\Users\Local\Python37\DLLs
\Users\Local\Python37\lib
\Users\Local\Python37
\Users\Local\Python37\lib\site-packages

You’ll get the list of directories and .zip files where your modules and packages are imported.

Using importlib

importlib is a module which is an implementation of the import statement in the Python code. It contains the import_module whose work is to execute any module or script by imitating the import operation.

An example to perform this:

import importlib
importlib.import_module('first_script')
Hello World!
<module 'first_script' from '\Users\Local\Python37\first_script.py'>

importlib.reload() is used to re-import the module since you cannot use import to run it for the second time. Even if you use import after the first time, it will do nothing. importlib.reload() is useful when you want to modify and test your changes without exiting the current session.

The following code shows that:

import first_script #First import
Hello World!
import first_script
import importlib #Second import does nothing
importlib.reload(first_script)
Hello World!
<module 'first_script' from '\Users\Local\Python37\\first_script.py'>

However, you can only use a module object and not any string as the argument of reload(). If you use a string as an argument, it will show a TypeError as follows:

importlib.reload(first_script)
Traceback (most recent call last):
...
...
  raise TypeError("reload() argument must be a module")
TypeError: reload() argument must be a module

Using runpy.run_module() and runpy.run_path()

The Python Standard Library has a module named runpy. run_module() is a function in runpy whose work is to execute modules without importing them in the first place. 

The module is located using import and then executed. The first argument of the run_module() must contain a string:

import runpy
runpy.run_module(mod_name='first_script')
Hello World!
{'__name__': 'first_script',
    ...
'_': None}}

Similarly, runpy contains another function run_path() which allows you to run a module by providing a location.

An example of such is as follows:

import runpy
runpy.run_path(file_path='first_script.py')
Hello World!
{'__name__': '<run_path>',
    ...
'_': None}}

Both the functions return the globals dictionary of the executed module.

Using exec()

Other than the most commonly used ways to run Python scripts, there are other alternative ways. One such way is by using the built-in function exec(). It is used for the dynamic execution of Python code, be it a string or an object code.

An example of exec() is:

exec(open('first_script.py').read())
Hello World!

Using py_compile

py_compile is a module which behaves like the import statement. It generates two functions— one to generate the bytecode from the source file and another when the source file is invoked as a script.

You can compile your Python script using this module:

import py_compile
py_compile.compile('first_script.py' 
'__pycache__\\first_script.cpython-37.pyc' 

The py_compile generates a new subdirectory named "__pycache__" if it doesn’t already exist. Inside the subdirectory, a Compiled Python File (.pyc) version of the script file is created. When you open the .pyc file, you can see the output of your Python script.

Running Python Scripts using an IDE or a Text Editor

An Integrated Development Environment (IDE) is an application that allows a developer to build software within an integrated environment in addition to the required tools.

You can use the Python IDLE, a default IDE of the standard Python Distribution to write, debug, modify, and run your modules and scripts. You can use other IDEs like Spyder, PyCharm, Eclipse, and Jupyter Notebook which also allow you to run your scripts inside its environment.

You can also use popular text editors like Sublime and Atom to run Python scripts.

If you want to run a Python script from your IDE or text editor, you need to create a project first. Once it is created, add your .py file to it or you can just simply create one using the IDE. Finally, run it and you can see the output in your screen.

Running Python Scripts from a File Manager

If you want to run your Python script in a file manager, all you need to do is just double-click on the file icon. This option is mainly used in the production stage after you have released the source code.

However, to achieve this, some conditions must be met:

  • On Windows, to run your script by double-clicking on them, you need to save your script file with extension .py for python.exe and .pyw for pythonw.exe.

If you are using the command-line for running your script, you might likely come  through a situation where you’ll see a flash of a black window on the screen. To avert this, include a statement at the tail of the script — input(‘Enter’). This will exit the program only when you hit the ENTER key. Note that the input() function will work only if your code is free of errors.

  • On GNU/Linux and other Unix-like systems, your Python script must contain the hashbang line and execution permissions. Otherwise, the double-click trick won’t work in a file manager.

Though it is easy to execute a script by just double-clicking on the file, it isn’t considered a feasible option because of the limitations and dependency factors it comes with, like the operating system, the file manager, execution permissions, and also the file associations.

So it is suggested to use this option only after the code is debugged and ready to be in the production market.

Conclusion

Working with scripts has its own advantages like they are easy to learn and use, faster edit and run, interactivity, functionality and so on. They are also used to automate complex tasks in a simplified manner.

In this article, you have learned to run your Python scripts using:

  • The terminal or the command-line of the operating system.
  • The Python Interactive session.
  • Your favorite IDE or text editor.
  • The system file manager.

Here, you have gathered the knowledge and skills of how to run your scripts using various techniques.You will feel more comfortable working with larger and more complex Python environments which in turn will enhance the development process and increase efficiency. You can learn more about such techniques as KnowledgeHut offers Python Certification Course.

Priyankur

Priyankur Sarkar

Data Science Enthusiast

Priyankur Sarkar loves to play with data and get insightful results out of it, then turn those data insights and results in business growth. He is an electronics engineer with a versatile experience as an individual contributor and leading teams, and has actively worked towards building Machine Learning capabilities for organizations.

Join the Discussion

Your email address will not be published. Required fields are marked *

1 comments

Grace 20 Aug 2020

i am not sure where you are getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for excellent info I was looking for this information for my mission.

Suggested Blogs

Python Map Vs List Comprehension

Python has many inbuilt methods, data structures, and modules to get the desired output. When we write a huge code consisting of many lines for a detailed problem statement, it is difficult to debug the code. Python provides the features we need to write code with a fewer number of lines.  Python Map and Python list comprehension are features that work differently but have some similarities. Their perfomance varies with the parameters we are using. Let's discuss what these maps and list comprehension features are, how they work, and their performances. Map function:-  The map function takes an Expression and an Iterable. The output will be an Iterable object where the expression will work on each element of the given Iterable. The output of each expression will be an element of the resultant Iterable.  Syntax  map( expression, iterable) Parameters  It accepts two parameters as arguments the "expression maps "and  "iterable". expression- It is the formula you need to calculate the result. Iterable- It is the iterable whose each element will be calculated on from the expression. Return type  It returns an object of the iterable where the given formula calculates each element (as of 1st argument) applied on the elements of given iterable (2nd argument) of the map function. The output can be converted to an iterable. Example Example 1: Map ( ) using the function as the first argument. #This code will add "2" to each element of the list  def add_two(n):  #parameter is "n"   return n + 2             #add "2" to "n"  #application of formula "add_two( )" on every element of "num"  num = [1, 2, 3, 4] #2nd argument  added_two = map(add_two, num)      #returns "added_two" as object  print(list(added_two))       #object "added_two" is converted to list. #Output: added_two = [3, 4, 5, 6] Example 2: Using Lambda expression as first argument  # Double up all numbers through map and lambda   num = [1, 2, 3, 4] #input list, 2nd argument  added_two = map(lambda i: i + 2, num)      #output list object  print(list(result))        #object is converted to list Iterable  #Output: added_two = [3, 4, 5, 6] map vs. for loop python speed  Map and For Loop can produce the same output, but they work differently. Inside "for loop", we iterate a variable fed to an expression within an Iterable. Whereas "Map" is used to map a function to all the elements of the given Iterable. It is a one-lined code configuration of "for loop". So, if we give the same input and write code to get the same output using "map" and "for loop", we will see from the time of execution on the IDE that their speed of execution is different. Run the below codes in your Python IDE and you will see that "map" is faster than "for loop". So, in map vs for loop python speed , map wins. Example solution with "map" :  #definition of function to cube a given number   def numCube (n) :   return n * n * n   num = [0, 1, 2, 3, 4]   cubedNum = map(cubedNum, num)  #object of iterable  print(list(cubedNum)) #object is converted to Iterable list #output:         [0, 2, 8, 27, 64] Example solution with "for loop" :  num = [0, 1, 2, 3, 4]  #input list  for n in num :   cubedNum = n * n * n         #expression  print(cubedNum)  #output:      #         0  #         1  #         8  #         27  #         64 List Comprehension  Python List Comprehension is used for creating a list where each element is generated by applying a simple formula on the given list.  Syntax  resultant_list = [ for in ]                or resultant_list = [ for in if ]  Parameters  It takes two argument including a list. variable_expression- It is the formula containing a variable and the formula is used on the variable. variable- It is a variable to access each element.  input_list- It is the given list from where each element will be accessed as variable and applied on the formula.  condition- After calculating the expression, if it matches the given Cindy, then only it will appear in the resultant_list. Return type  It always returns a list. Example  Example 1: Python List comprehension without condition # a list comprehension to print cube  num= [-2, -1, 0, 1, 2]  cubedNum = [n**3 for n in num]   print(cubedNum)  #output:      [ -8, -1, 0, 1, 8]  Example 2: Python List comprehension with condition # a list comprehension to print cube if cube is positive  num= [-2, -1, 0, 1, 2]  cubedNum = [n**3 for n in num if n**3 >= 0]   print(cubedNum)      #output list contains only positive cubes #output:  [ 0, 1, 8]  python any list comprehension (use of "any" keyword) The "any ( )" function is very useful tool which works on iterables, and returns true if any of the element in the iterable is true. As list compression generates a list, we can use any() function with a python list comprehension.  Example: Print "not allowed" if any one of the ages is less than 18 ages= [22, 15, 20, 25, 34]   if any([a < 18 for a in ages]):  print("At least one is under-aged and they all are not allowed")  # there is one age 15 which is less than 18  #output:.    At least one is under-aged, and they all are not allowed python reduce vs list comprehension Reduce- Used to reduce the list where a formula is applied recursively on the elements of a list. It accepts two arguments, including expression as the first argument and Iterable as the second argument. This function can be defined in the "functools" module.  Firstly it took the first two elements of the Iterable and applied them to the expression. The calculated result and next element are applied to the expression. It is traversed till the last element of the Iterable, and the final output is thus generated.  Example 1: Reduce function using lambda #importing functools module where reduce( ) is defined  import functools    num = [ 1 , 2, 5, 3, 9 ]   #summation of elements using functools( )  print("The sum of all list elements is : ")   print(functools.reduce(lambda a,b : a+b, num))  #output: The sum of all list elements is: 20  How to reduce function works?  Step 1: 1 + 2 = 3   Step 2: 3 + 5 = 8   Step 3: 8 + 3 = 11   Step 4: 11 + 9 = 20  Example 2: Reduce function using operator module #importing functools module where reduce( ) is defined  import functools     #importing operator module where numerical operators are defined as function  import operator    num = [ 1 , 2, 5, 3, 9 ]   print ("The sum of the list elements is : ")   # "add" function is used as '+' operator  print (functools.reduce(operator.add, num))  #output: The sum of all list elements is : 20  How does reduce function with operator work? Step 1:    1 add 2 = 3   Step 2:      3 add 5 = 8   Step 3:      8 add 3 = 11   Step 4:       11 add 9 = 20  Python List comprehension- Used to create another list where a formula is applied once on each element of the list. So, we cannot use reduce in place of list comprehension.  We have seen no possible answer to python reduce vs list comprehension because these two can replace each other. Python filter vs list comprehension In python, a filter is a function that is used to filter out some elements from Iterable based on a certain condition. It returns an Iterable object. We can visualize that after printing it, and converting it to a list. It can be done by using list comprehension to create a list with the "if" condition.  When we are using a list with a small number of elements, then the list comprehension and filter method have almost the same execution speed. But when the list size is huge, then list comprehension is slightly faster than a filter. This is because list comprehension directly generates a list, whereas filter function returns an iterable object, which is then converted to a list. However, if we don't convert the object to a list, then the execution time is almost negligible. But to visualize the output, we need a list, not an object.  So, python list comprehension is better than a filter. Example 1: Using list comprehension to print positive numbers from a list. num= [-2, -1, 0, 1, 2]  positive Num = [n for n in num if n > 0]   print(positiveNum)      #output list contains only positive cubes #output: [ 1, 2]  Example 2: Using filter function to print positive numbers from a list. num= [-2, -1, 0, 1, 2]  print(list(filter(lambda n: n>0, num))) # output:    [1, 2] So, in python filter vs. list comprehension, list comprehension has better performance. Python Map VS List Comprehension  Assume that we have a formula. We want to apply that formula to each element of a list and thereby create a new list. Now, using for loop, we can traverse through that given list, pick each item, apply them into the formula and append it to the resultant list. That is a long process that consists of many lines of code. So, in one line, we can use the map function as discussed above.  Now, python list comprehension is a tool for creating a new list. We can use the functionality of lambda expression, filter method, and map function together using only this list comprehension.  List comprehension has a simpler configuration than the map function.  List comprehension can be used together with if condition as replacement of filter method. Map function has no such functionality. However, we can feed the map function output to the filter function.  List comprehension returns a list, whereas the map function returns an object of Iterable.  List comprehension execution is faster than that of map function when the formula expression is huge and complex. Map function is faster than list comprehension when the formula is already defined as a function earlier. So, that map function is used without lambda expression. Comparing Execution Time  Now to examine the execution performance of list comprehension and map function, we will import a module "timeit" to check the execution time. Let us see the result with a variety of methods. Without lambda: Map is faster than List Comprehension when function is already defined in case of map function. Example code 1: This code will print the time taken to evaluate numbers from 1 to 50. Map function is used without lambda. import timeit  # list comprehension  l1 = timeit.timeit( '[ l for l in range(50)]' , number = 999999)  print (l)   #map function  f= 'def num( ) : print (n)'  m1 = timeit.timeit( ' map (num, range(50))' , number = 999999, setup = f )   print (m) With lambda in map: List comprehension is better than map function when we don't define the function beforehand and use lambda expression inside map.   Example code 2: This code will print the time taken to add a number to itself and this is applied for each element of the list. The expression is defined as lambda expression. import timeit  # list comprehension  l2 = timeit.timeit( '[ n+n for n in range(50)]' , number = 999999)  print (l)  #map function  m2 = timeit.timeit( ' map (lambda a: a+a, range(50))' , number = 999999, setup = f )   print (m) Write the above codes on your IDE. You will see that for the first case, m1 is very less than l1. That means the map works faster than list comprehension. For the second case, m2 is greater than l2, implying that list comprehension is faster than map function when map function is used with Lambda expression.  ConclusionThere are no clear answers about which is the better option, in Python Map Vs List comprehension. We should know what the problem statement is, then use the method which suits us better because our main objective is to calculate the solution with optimal time and space consumption. 
7363
Python Map Vs List Comprehension

Python has many inbuilt methods, data structures,... Read More

Why use the map() function?

The map() in Python is a built-in function. For each item of the iterable (e.g. list, tuple, etc.), the map() calls the defined function and returns a list of results. The first argument in the map() is that you need to pass a function without calling it. This means that there are no parentheses. The operation performed by map() is usually known as a mapping, as it maps every item in an iterable input to an iterable new item. This is achieved by applying map() to all things in the input iterable. Python uses the map concept from functional programming. The map operation considers the mapping function and the data vector as arguments and returns the new vector, which results in the development of the mapping function on each vector object. Syntax: The syntax of map() in python is: map(function, iterable) Let’s assume that you want to use a list of operations or functions to construct a new list for each variable and use those outputs. An example would be to have a list of numbers and to create a new list with their cubes. One way to do this is by going through the list of numbers you are using for loop, and using a function that returns the cube of each number or part. Following is a simple program using the map function in python that returns the cube of a number: # defining a function that returns the cube of a number  def cube(number):    return number**3  # original list  number_list = [1,2,3,4,5,6]  # list that will contain the squared numbers  number_list_cube = []  # using a for loop to iterate over our number_list and create a new list with the cubic values  for number in number_list:    number_list_cube.append(cube(number))  print(number_list_cube)   # output [1,4,9,16,25,36We have a list of numbers, number_list, and we want to create a new list, number_list_cube, that contains the squares of number_list. We use a for loop to loop over number_list, and append the square of each number or number to our number_list_cube list. The built-in python function called map is another way to do this. There are two arguments in the map function: the function we want to apply and the object or sequence to which we want to apply it (such as a list in this case). In other words, the map function maps or applies it to each element in the sequence. Working of map() function in Python: Two inputs as a function and an iterable object used by the map(). The function given to map() is a normal function and is iterated over all the values in the given object. For example, making a list of numbers, and finding the cube for each number. The function is required that will return the double of the number. The function will be as follows: def double(number):  return number+number Now we will create the list of items: number_list = [1,2,3,4,5] Now we will use the map() function to find the double of each item in the number-list.  def double(number):      return number*2  number_list = [1,2,3,4,5]  number_list = map(double, number_list)  print(list(number_list)) Output: [2, 4, 6, 8, 10] We have used list() in the code to iterate the output from the map. The map() function helps to get the double of each number. map() in Python with built-in functions: The map() in Python is a built-in function and can also be used with other built-in functions available in Python. In the example, we are going to make use of the Python pow() built-in function. It is given to map two list objects, one for each base and index parameter. The result is a list containing the power of each number in bases raised to the corresponding number in the index. Example: We have two lists, one is base and another is index. base= [10, 20, 30, 40, 50]  index= [1, 2, 3, 4, 5]  We will use pow() as the function to map().  base=[10, 20, 30, 40, 50]  index=[1, 2, 3, 4, 5]  powers=list(map(pow, base, index))  print(list(powers)) Output: [10, 400, 27000, 2560000, 312500000] map() in Python with Tuple: Tuples in Python are an ordered collection of objects. They are defined by enclosing the elements in parentheses (()). In certain instances, the tuple is identical to the list in terms of indexing, nested objects and repetition, but the tuple is immutable, unlike the lists. Even though tuples are defined using brackets, you still use square brackets to index and slice tuples, just like strings and lists. Example: In the following example we will use the function that will convert the values of the tuple into uppercase. def upperFunc(values):      return values.upper()  values_tuple = ('swift','java','python','go','kotlin')  new_list = map(upperFunc, values_tuple)  print(list(new_list)) Output: ['SWIFT', 'JAVA', 'PYTHON', 'GO', 'KOTLIN'] map() in Python with Dictionary: Dictionaries are the implementation by Python that is more generally known as an associative array. A dictionary is a collection of key-value pairs. It is defined by curly brackets({}). Dictionaries are both mutable and dynamic. They can be updated and deleted as needed. Dictionaries differ from lists as Dictionary elements are accessed via keys while List elements are accessed by their position in the list, via indexing. The dictionary is an iterator, you can use it inside the map() function. Example: def dictFunc(values):      return values*10  values_dict = {1,2,3,4,5}  finalitems = map(dictFunc, values_dict)  print(list(finalitems)) Output: [10, 20, 30, 40, 50] map() in Python with Set: Sets in Python are unordered and the elements of a set are unique i.e. they cannot be the same. You can modify a set, but the elements in the set must be immutable. Like dictionary set() is also an iterator, you can use it inside the map() function. Example: def setFunc(values):      return values*10  values_set = {1,2,3,4,5}  finalitems = map(setFunc, values_set)  print(list(finalitems)) Output: [10, 20, 30, 40, 50] map() in Python with Lambda function: A Lambda Function is an anonymous function that means a function without a name. A Lambda function contains multiple arguments with one expression. We use the def keyword to define a normal function in Python. Similarly, we use the lambda keyword to define an anonymous function in Python. Lambda functions are syntactically restricted to a single expression. We can use the lambda function inside the map() function in Python. Example: numbers = (1,2,3,4,5)  result = map(lambda x: x+x, numbers)  print(list(result)) Output: [2, 4, 6, 8, 10] map() in Python with Other Functional Tools: We can perform more complex transformations on iterables by using map() together with functional tools like filter() and reduce(). map() and filter(): In some problems we need to process an input iterable and return another iterable by filtering unnecessary values in an iterable input. The filter() of Python is a good choice in this situation. Filter() returns the iterable input items for which the function returns true. If no function is passed, filter() uses the identity function. This implies filter() monitors each item in iterable for its true value and filters all falsy items out. Example: If we want to calculate the square root of all the values in a list and if our list have negative values, we’ll get an error because the square root isn’t defined for negative numbers. In order to prevent this problem, filter() is used to filter out all negative values and then find the square root of the remaining positive values. import math  def is_positive(num):  return num >= 0  def filtered_sqrt(numbers):  filtered_iter = map(math.sqrt, filter(is_positive, numbers))  return list(filtered_iter)  filtered_sqrt([100, 9, 225, -36, 0]) map() and reduce(): Another function that exists in functool module of Python is reduce() in the standard library of Python. Reduce() is another key functional method in Python that is helpful when an iterable feature needs to be added to and reduced to a single cumulative value. This form of action is generally called reducing or folding. Example: We will use map() and reduce() to calculate the total size of all the files that live in your home directory. import functools  import operator  import os  import os.path  files = os.listdir(os.path.expanduser(“~”))  functools.reduce(operator.add, map(os.path.getsize, files)) We will call os.path.expanduser("~") to get the path to your home directory. Then you call os.listdir() on that path to get a list with the paths of all the files that live there. The call to map() uses os.path.getsize() to get the size of each file. You eventually use reduce() to obtain the total sum of the size of each file using operator.add(). The end result is the total file size in bytes of your home directory. Generator Expressions instead of map() in Python: map() in python returns a map object, an iterator that returns items on request. The natural substitution for map() is therefore a generator expression. Generator expressions return generator objects, which are also iterators, which yield items on request. A tiny difference occurs between a list comprehension and a generator expression. The list uses a pair of square brackets ([]) while generator expressions use a pair of parentheses (()). If you want to turn a list comprehension into a generator expression, you just need to replace the square brackets with parentheses. def square(number):  return number ** 2  numbers = [1, 2, 3, 4, 5]  gen_exp = (square(x) for x in numbers)  print(list(gen_exp)) Output: [1, 4, 9, 16, 25] Generator expressions are commonly used as arguments in function calls.  Conclusion:map() in python allows to perform operations on iterables. Map() usually helps you to convert and process iterables without directly using a loop. Python map() is a built-in-function that applies a function to all the input items of an iterator. For example, a list, tuple, etc. can be an iterator, and it returns a map object that can be iterated. The map() function will add all the items inside the iterator to the specified function, and then return an iterable map object, i.e. tuple, list, etc. map() in Python is an embedded function that can also be used in Python with other integrated functions. 
9564
Why use the map() function?

The map() in Python is a built-in function. Fo... Read More

What is nested lambda function? What are its characteristics give an example?

The lambda function was initially introduced in the Python programming language because of the demand from LISP programmers. The term lambda originates from the Lambda Calculus, a form of mathematical calculus invented by Alonzo Church.If the mention of Calculus has scared you, relax! You don’t have to master Calculus for using lambda functions. Lambda expressions are simple, concise and easy-to-understand – even a newbie can get the lambda syntax easily.SyntaxLambda functions are one-liner functions. Every lambda function you define in python has three essentials – the keyword lambda, arguments and the expression. Here is the syntax of how we define a lambda function in python:lambda arguments: expressionKeyword: lambdaArguments: The user can define the number of arguments separated by a commaExpression: The expression is an arithmetic expression that uses the arguments defined by the userWhat Is a Lambda Function?A lambda function is an anonymous function, i.e., a function without a name. It allows writing functions without naming them. Unlike the regular way of defining python functions, where we use the keyword def, we use the lambda keyword to define a lambda function.# a function in python defined using the keyword – lambda lambda a, b: a + bis similar to writing a regular function in python,# a function in python defined using the keyword – def  def sum(a, b):     return a + bAdding two numbers using Python lambda functions:sum = lambda a, b: a + b print("Result = ", sum(6,4))Output:Result = 10Characteristics of Lambda functions in Python:Lambda functions can take multiple arguments but return just one value in the form of an expression.The lambda functions are throw-away functions, i.e., they are required where they have been created, and called anywhere in a function when required.Syntactically, they are a one-line version of a function and hence cannot contain multiple-line expressions.Lambda expressions can be passed as arguments to other functions.Lambda functions do not have an explicit return statement, but always contain an expression that is executed and returned.Attention: You can use lambda functions wherever there is a need for function objects. However, they cannot access variables other than those present in their parameter list. Also, global variables cannot be accessed using the lambda function.What is a nested Lambda Function?A lambda function inside a lambda function is called a nested lambda function. Python allows lambda nesting, i.e., you can create another lambda function inside a pre-existing lambda function. For nesting lambdas, you will need to define two lambda functions – an outer and an inner lambda function. When the outer lambda is called, the inner lambda creates a function. The outer lambda then returns the called function.Here is a small code to illustrate the nested lambda function:Adding two numbers using Python lambda functions:add = lambda a=20: lambda b: a + b x = add() print(x) print(x(30))Output:50Here is a step-by-step explanation of the above code.Step-1: Define an outer lambda function (lambda a =10) & an inner lambda function (lambda b: a + b)Step-2: Assign both the lambda functions in a variable name sum.Step-3: Call the outer lambda function, and assign it to a variable xStep-4: Print x by assigning a value.The assigned value of 30 is added up with the outer lambda function and finally, sum=50 is displayed in the output.Where Are Nested Lambda Functions Useful?Nested lambda functions are significant when there is a need for function objects. Nesting allows hiding the mechanics of iteration operations, especially when working on complex projects. Nested lambdas can be easily embedded in places where def functions are restricted. Syntactically, they are useful in places where python wouldn’t allow a function to be defined using a regular def expression. Besides, nested lambda statements are also used along with sort methods to arrange the list elements in a sequence.Attention: With nested lambdas, recursion can occur and may also result in a runtime error.RuntimeError: maximum recursion depth exceeded errorHow to Use Nested Lambda with the Sort MethodIn general, sort() in python has two parameters, key, and reverse. The key value is None, and the reverse value is False, by default. Using the lambda function in the sort method, the key extends its capabilities and becomes more versatile. Here is a simple code to make you understand the importance of the lambda function with the sort method.Sorting without using lambda function:check_list = [[4, "Cherry"], [3, "Banana"], [1, "Apple"], [2, "Dragon Fruit"]] check_list.sort() print(check_list)Output:[[1, 'Apple'], [2, 'Dragon Fruit'], [3, 'Banana'], [4, 'Cherry']]Look at both the codes carefully. The inner list doesn’t get sorted using the regular sort() method but gets sorted using the lambda functions in alphabetical order. Therefore, the lambda function is of the utmost importance when there is a need to sort the inner list sequentially in a pre-defined list.Lambda functions are also used with python in-built functions like filter(), map(), and reduce(). Let’s have a look at each in detail:Use of Filter Function with lambda – filter ()The filter () function in Python is an excellent way of filtering out all the elements of a sequence for which the function is True or False. The function includes only those values that satisfy the result and thus constructs a new list from those elements of the list for which the function returns True. Here is a small code that returns the list of even numbers from an input list:# Python code to illustrate filter() with lambda() # Finding the even numbers from a given list li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] result = list(filter(lambda x: (x%2 ==0), li)) print(result)Output:[2, 4, 6, 8, 10, 12, 14]From the output of the above program, we see the output returning only the true values. Only those values in the range that are even numbers i.e., divisible by 2, are included in the new list, and the rest are explicitly removed.  Attention Please: If the sequence is a string, tuple, or Unicode, the output will be the respective same type, otherwise, the result is always a list.Use of Map Function with lambda – map()The map() function in Python applies a unary function to each element in the list. After applying the specified function for each item in the list, the map() function returns the new list. The function calls each item in the sequence and returns a list of return values containing the results, in the same order.Here is a small code that returns the list of cubes from an input list:# Python code to illustrate map() with lambda() # Cubing the list of numbers from a given list nums= [1, 3, 5, 6, 4, 2] result = list(map(lambda x: x**3, nums)) print(result)Output:[1, 27, 125, 216, 64, 8]From the output of the above program, we can see that all the elements have been evaluated for each of the elements in the list. The corresponding cubes of all the elements in the list are added to the new list sequentially.Attention Please: Map() function also allows to pass more than one sequence if required. Just keep in mind two things when passing multiple sequences. Firstly, the number of arguments must be identical to the number of sequences. Secondly, each argument must be called from the corresponding item from each sequence. Use of Reduce Function with lambda – reduce()The reduce() function combines the sequence elements using a binary function. It returns a single value generated by calling the function on the first two elements in the sequence. Once the two have been called, the result then gets added to the next item, and so on. The process continues until all the elements in the sequence get evaluated.Pro-tip: Don’t forget to import reduce from the functools module.Here is a small code that returns the list of the sum from an input list:# Python code showing the use of reduce() with lambda() # Finding the sum from a given list lis = [1, 2, 3, 4, 5] result = reduce ((lambda a, b: a + b), lis) print(result)Output:15The result of the first-two elements gets added to the next element. Here is what goes behind the scene à ((((1+2) + 3) + 4) + 5) = 15. The process is continued till the last element of the sequence is reached.Attention Please: If there is a single-value sequence, then the value of the sequence is returned. However, an exception is raised in an empty list.How to Test Lambda FunctionsLambda functions, no matter how simple they sound, also need to be tested at times. Testing a python lambda function is the same as testing a regular function. Unittest and doctest – python standard libraries are used to test lambda functions.Python Lambda FunctionsLambda functions in python are an effective way to define functions anonymously. The lambda functions being executed and returned in a single-line add a significant advantage with throw-away functions. However, lambda functions are not recommended if the expressions become complicated to read or interpret. If it is strenuous to get all the logic inside lambdas, use a def instead.
3259
What is nested lambda function? What are its chara...

The lambda function was initially introduced in th... Read More

20% Discount