Found 10784 Articles for Python

What is a Python module? How is it different from libraries?

Vikram Chiluka
Updated on 22-Sep-2022 12:45:41

9K+ Views

It can be difficult for beginners to grasp the concept of Python modules and libraries. You can tell from the zoom-out content that each of them is a collection of codes. But there is a significant difference between them. In this article, we will show you the major difference between python modules and libraries. Python Modules and Libraries Real-world programs are complicated. Even simple software contains thousands of lines of code. Because of this, writing code in continuous flow is difficult for programmers and developers to grasp. Developers utilize modular programming to facilitate learning and make it logically separated. It ... Read More

What are compound data types and data structures in Python?

Vikram Chiluka
Updated on 23-Aug-2023 03:39:55

4K+ Views

In this article, we will explain what are the compound datatypes and data structures in python. Variables have so far only stored one value. What if we wish to save many related values? We could simply create distinct variables for each. But what if we don't know how many values will be present? What if we wish to use these values within a loop? Compound data structures are data types that can store a large number of values. In Python, there are various types of compound data structures. We will mostly concentrate on Lists. In the end, we will ... Read More

How do I access the serial (RS232) port in Python?

AmitDiwan
Updated on 20-Sep-2022 11:56:47

19K+ Views

To access the serial port in Python, use the pyserial module, which Python Serial Port Extension for Win32, OSX, Linux, BSD, Jython, IronPython. Let us see the features - Access to the port settings through Python properties. Support for different byte sizes, stop bits, parity and flow control with RTS/CTS and/or Xon/Xoff. Working with or without receive timeout. The files in this package are 100% pure Python. The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. To install pyserial, use pip pip install pyserial At first import the required libraries. import ... Read More

How do I find the current module name in Python?

AmitDiwan
Updated on 20-Sep-2022 11:24:17

10K+ Views

A module can find out its own module name by looking at the predefined global variable __name__. If this has the value '__main__', the program is running as a script. Example def main(): print('Testing…...') ... if __name__ == '__main__': main() Output Testing…... Modules that are usually used by importing them also provide a command-line interface or a selftest, and only execute this code after checking __name__. The__name__ is an in-built variable in python language, we can write a program just to see the value of this variable. Here’s an ... Read More

How do I parcel out work among a bunch of worker threads in Python?

AmitDiwan
Updated on 20-Sep-2022 11:18:19

108 Views

To parcel out work among a bunch of worker threads, use the concurrent.futures module, especially the ThreadPoolExecutor class. With that an alternative, if you want fine control over the dispatching algorithm, you can write your own logic manually. Use the queue module to create a queue containing a list of jobs. The Queue class maintains a list of objects and has a .put(obj) method that adds items to the queue and a .get() method to return them. The class will take care of the locking necessary to ensure that each job is handed out exactly once. Example Following is an ... Read More

How do you implement persistent objects in Python?

AmitDiwan
Updated on 20-Sep-2022 11:17:02

678 Views

To implement persistent objects in Python, use the following libraries. shelve pickle The shelve module A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. It is having some key methods − shelve.open() − Open a persistent dictionary. The filename specified is the base filename for the underlying database. As a side-effect, an extension may be ... Read More

How do I create documentation from doc strings in Python?

AmitDiwan
Updated on 20-Sep-2022 11:15:24

580 Views

To create documentation from doc strings, we can use the following packages and modules − Pydoc Epydoc Sphinx Let us understand them one by one − Pydoc The pydoc module can create HTML from the doc strings in your Python source code. The pydoc module automatically generates documentation from Python modules. The documentation can be presented as pages of text on the console, served to a web browser, or saved to HTML files. For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the __doc__ attribute) of the object, and recursively of its ... Read More

How can I remove Python from my Windows Machine?

AmitDiwan
Updated on 20-Sep-2022 11:13:52

5K+ Views

To complete remove Python from the Windows machine, uninstall it, remove the path and the remaining files if still present on the system. Uninstall Python To uninstall Python, go to START, type Python. Click Uninstall − After clicking Uninstall, the following screen is visible. To uninstall Python, you need to uninstall these two − Right-click each one by one and press Uninstall − It is uninstalling − Python uninstalled successfully Remove Files Even after uninstalling Python, sometimes the installation files remain in the system itself. Do not worry about them. Reach the folder wherein we ... Read More

Python - Can’t we get rid of the Global Interpreter Lock?

AmitDiwan
Updated on 20-Sep-2022 11:09:56

539 Views

The Global Interpretor Lock is a mutex in Python. Let’s first understand what is a Global Interpreter Lock (GIL) − What is a GIL? The global interpreter lock, or GIL, is a mutex that − Protects access to Python objects, Prevents multiple threads from executing Python bytecodes at once. Prevents race conditions. Ensures thread safety. The Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there’s a global lock, called the global interpreter lock or GIL. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two ... Read More

What WWW tools are there for Python?

AmitDiwan
Updated on 20-Sep-2022 11:08:55

127 Views

With Python, we can also create Web Applications. Python provides multiple frameworks for the web development. Let us see some them which are widely used. Django Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. Django is an MVT web framework that is used to build web applications. The huge Django webframework comes with so many batteries included that developers often get amazed as to ... Read More

Advertisements