Found 35163 Articles for Programming

Python Heap Queue Algorithm

Chandu yadav
Updated on 30-Jul-2019 22:30:23

254 Views

The heap data structures can be used to represents a priority queue. In python it is available into the heapq module. Here it creates a min-heap. So when the priority is 1, it represents the highest priority. When new elements are inserted, the heap structure updates. To use this module, we should import it using − import heapq There are some heap related operations. These are − Method heapq.heapify(iterable) It is used to convert an iterable dataset to heap data structure. Method heapq.heappush(heap, element) This method is used to insert the element into the heap. After ... Read More

Python Container Datatypes

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

In the collections there are some container datatypes, which are alternatives to python’s general purpose built-in containers like dict, list, set etc.Some of the containers are −Sr.No.Container & Description1namedtuple()Used to create tuple subclass with the name field2dequeQueue using list type data3CounterSubclass of dict to count the hash-table objects4ChainMapUsed to create single view of multiple mappings5OrderedDictSubclass of dict, where data are added in ordered manner6UserListWrapper for list to easier access.To use this module, we should import it using −import collectionsDeque ObjectThe Deque is basically a generalization of stack and queue structure, where it is initialized from left to right. It uses ... Read More

Python Basic date and time types

George John
Updated on 30-Jul-2019 22:30:23

6K+ Views

To manipulate dates and times in the python there is a module called datetime. There are two types of date and time objects. The types are naïve and the aware. In the naïve object, there is no enough information to unambiguously locate this object from other date-time objects. In this approach it uses Coordinate Universal Time (UTC). In the aware type objects there are different information regarding algorithmic and political time adjustments. This type of objects is used to represent some specific time moments. To use this module, we should import it using − import datetime There are ... Read More

Python Completion function for GNU readline

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

258 Views

Unix readline module has tab completion mechanism. To get these features, we have to use rlcompleter module. It can be used in python’s interactive mode. To use this module, we should import it using − import rlcompleter There is a class called Completer class − Method Completer.complete(text, state) This method is used to return the tab completion output. If there is a ‘.’ in the text, then it will try to get all related members of that command. When there is no dot ‘.’ it will just complete the text. Example Code import rlcompleter import sys ... Read More

Python Helpers for Computing Deltas

AmitDiwan
Updated on 11-Aug-2022 11:02:59

164 Views

The difflib module is used in Python to compute deltas. It is used to compare files, and can produce information about file differences in various formats, including HTML and context and unified diffs. We need to first import the difflib module before using it − import difflib Class (difflib.SequenceMatcher) This class is used to compare two sequences of any type. It has different methods. Some of the methods − set_seqs(a, b) − Set the sequence files which will be compared. It computes and caches detailed information about the second file. So for matching multiple files, we should set ... Read More

Python GNU readline Interface

Chandu yadav
Updated on 30-Jul-2019 22:30:23

575 Views

The readline is UNIX specific module. It defines a number of functions to read and write history files in easier way from python interpreter. We can use this module directly or using the rlcompleter module. This module settings may affect the built-in input() method prompt and also the interactive prompt. For the MAC based system (on MAC OS X) this readline module can be implemented using the libedit library. The libedit configuration is different from GNU readline. To use this module, we need to import the readline module in the python code import readline Some methods of ... Read More

Python Internet String Preparation

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

222 Views

To identify different things in the internet, it is necessary to compare different identification for equality. The comparison procedure depends on the application domain. For an example, some things are case-insensitive etc. To check these kind of information stringprep is used. The RFC 3454 defines the procedure to prepare the Unicode strings before transmitting through the wire. After going through the preparation procedure, they have a certain normalized form. The RFC defines a set of tables; these tables can be combined into profiles. For an example there is a profile of stringprep is nameprep. In the nameprep, there are internationalized ... Read More

Python Unicode Database

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

301 Views

The unicodedata module is used to access all of the Unicode characters using Unicode character databases. In this database, there are character properties of all characters. To use this modules, we need to import the unicodedata module in our code. import unicodedata Unicode Database Methods Some modules of the unicodedata module are described here. Module (unicodedata.lookup(name)) − This method is used to lookup the characters by name. When the name is valid, it should return the character. Otherwise it will raise the KeyError. Module (unicodedata.name(chr[, default]))− This method is used to return the name of the given ... Read More

Python Text Wrapping and Filling

George John
Updated on 30-Jul-2019 22:30:23

4K+ Views

In python the textwrap module is used to format and wrap plain texts. There are some options to format the texts by adjusting the line breaks in the input paragraph. To use these modules, we need to import the textwrap module in our code. import textwrap The Textwrapper instance attributes of the constructors are as follows − Sr.No. Attribute & Description 1 width The maximum length of lines. Default value is 70 2 expand_tabs If the value of this attribute is true, then all tabs will be replaced by spaces. Default value ... Read More

String Operations in Python

Chandu yadav
Updated on 25-Jun-2020 13:48:21

588 Views

In python, there is a standard library, called string. In the string module, there are different string related constants, methods, classes are available.To use these modules, we need to import the string module in our code.import stringSome string constants and their corresponding values are as follows −Sr.No.String Constant & Values into it1string.ascii_lowercase‘abcdefghijklmnopqrstuvwxyz’2string.ascii_uppercase‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’3string.ascii_lettersConcatenation of asci_lowwecase and ascii_uppercase ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’4string.digits‘0123456789’5string.hexdigits‘0123456789abcdefABCDEF’6string.octdigits‘01234567’7string.punctuation‘!"#$%&\'()*+, -./:;?@[\]^_`{|}~’8string.printableAll printable ASCII characters. It is collection of asci_letters, punctuation, digits and whitespace.‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+, -./:;?@[\]^_`{|}~ \t\r\x0b\x0c’9string.whitespace‘\t\r\x0b\x0c’Example Code Live Demoimport string print(string.hexdigits) print(string.ascii_uppercase) print(string.printable)Output0123456789abcdefABCDEF ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+, -./:;?@[\]^_`{|}~String FormattingThe built-in string class in python supports different complex variable substitution and value formatting by the format() method.To format ... Read More

Advertisements