Heap Queue (heapq) in Python

Pradeep Elance
Updated on 17-Oct-2019 13:12:58

333 Views

Heap queue is a special tree structure in which each parent node is less than or equal to its child node.In pythin it is implemented using the heapq module. It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing. TheCreate a HeapA heap queue is created by using python’s inbuilt library named heapq. This library has the relevant functions to carry out various operations on heap data structure. Below is a list of these functions.heapify − This function converts a regular list to a heap. In the resulting heap ... Read More

Guide to Install MongoDB with Python in Windows

Pradeep Elance
Updated on 17-Oct-2019 13:08:55

681 Views

Get MongoDBTo install MongoDB on Windows, first, download the latest release of MongoDB from https://www.mongodb.org/downloads. Below is an example of selecting a 64-bit version for windows as a msi installer.Install MongoDBNext, we follow the below steps to install MongoDB. As it is a windows installation using the msi installer, the steps are very straight forward. We choose a complete installation instead of a custom installation.Running the installerHere we run the installer which has been downloaded to our system. The installation starts asking for various steps of confirmation.Choosing a Service ConfigurationWe choose to run the service as a network service user. Clicking ... Read More

Graph Plotting in Python

Pradeep Elance
Updated on 17-Oct-2019 13:03:28

1K+ Views

Python has the ability to create graphs by using the matplotlib library. It has numerous packages and functions which generate a wide variety of graphs and plots. It is also very simple to use. It along with numpy and other python built-in functions achieves the goal. In this article we will see some of the different kinds of graphs it can generate.Simple GraphsHere we take a mathematical function to generate the x and Y coordinates of the graph. Then we use matplotlib to plot the graph for that function. Here we can apply labels and show the title of the ... Read More

Global Keyword in Python

Pradeep Elance
Updated on 17-Oct-2019 12:58:23

965 Views

Different variables in a python program have different scope. Depending on where it is declared, the variable may or may not be accessible inside a function. Sometimes we will need to modify a variable that is present inside a function from outside its current scope. In such a scenario we use the global keyword with the variable name.Following are the key points about the global keywordA variable declared outside a function is a global variable by default.We use a global keyword for a variable which is inside a function so that it can be modified.Without the global keyword, the variable ... Read More

Get Password and Username in Python Without Echo

Pradeep Elance
Updated on 17-Oct-2019 12:53:09

1K+ Views

When you create some python programs that need password protection before it can run, we take the help of the getpass() and getuser() modules. They have a variety of functionalities that can be used to manage password protection as well as password retrieval etc. In this article, we will see how to type in the password with and without echoing it back on the screen. Below are the different ways it is handled.With PromptThe below code is saved to a file (logon.py). The getpass() function prints a prompt then reads input from the user until they press returnExample Live Demoimport getpass ... Read More

Common Mistakes When Using Python Dictionary

Pradeep Elance
Updated on 17-Oct-2019 12:41:09

291 Views

Dictionaries in python are a type of data structure that map keys to values as a key-value pair. They are one of the frequently used data structures and have many interesting properties. They are presented by enclosing them in a pair of curly brace like below.dict = {'day1':'Mon' ,'day2':'Tue', 'day3':'Wed'}The elements or key-value pairs in the dictionary are represented within single quotes and separated by a colon.Creating a DictionaryWe create a dictionary by assigning values written in the form of a ke.ExampleDict1 = {'day1':'Mon' ,'day2':'Tue', 'day3':'Wed'} print(type(dict1)) print(dict1) # Using the dict() method dict2 =dict({('day1', 'Mon'), ('day2', 'Tue'), ('day3', ... Read More

Enumerate in Python

Pradeep Elance
Updated on 17-Oct-2019 12:35:02

1K+ Views

When using the iterators, we need to keep track of the number of items in the iterator. This is achieved by an in-built method called enumerate(). The enumerate() method adds counter to the iterable. The returned object is a enumerate object. Its syntax and parameters are described below.enumerate(iterable, start=0) iterable - a sequence, an iterator, or objects that supports iteration start – is the position in the iterator from where the counting starts. Default is 0.ExampleIn the below example we take a dictionary and apply enumerate to it. In the result the default start is from 0 and we get ... Read More

Datagram in Python

Pradeep Elance
Updated on 17-Oct-2019 12:24:35

641 Views

Chunks of data move between the clients and servers using the User Datagram Protocol or UDP protocol. The two communicating endpoints need the IP address and port number to establish communication. One endpoint is known as the sender and the other is known as the receiver. In this protocol, the sender does not keep track of the sent packets and it is up to the receiver to accept or not all the packets.Sender ProgramThe below python program uses the socket module to create the sender’s program. We declare variables for IP address and Port. Then add a message to it. ... Read More

Comprehensions in Python

Pradeep Elance
Updated on 17-Oct-2019 12:10:37

4K+ Views

We can create new sequences using a given python sequence. This is called comprehension. It basically a way of writing a concise code block to generate a sequence which can be a list, dictionary, set or a generator by using another sequence. It may involve multiple steps of conversion between different types of sequences.List ComprehensionIn this method, we create a new list by manipulating the values of an existing list. In the below example we take a list and create a new list by adding 3 to each element of the given list.Examplegiven_list = [x for x in range(5)] print(given_list) ... Read More

Colorsys Module in Python

Pradeep Elance
Updated on 17-Oct-2019 12:07:25

1K+ Views

This module allows bidirectional conversions of color values between colors expressed in the RGB (Red Green Blue) and other color spaces. The three other color spaces it uses are YIQ (Luminance (Y) In-phase Quadrature), HLS (Hue Lightness Saturation) and HSV (Hue Saturation Value). All the coordinates can be between 0 and 1 except the I and Q values in the YIQ color space.The below tables shows the functions and their purpose.FunctionPurposePermitted Valuesrgb_to_yiqfrom RGB coordinates to YIQ coordinates0 to 1rgb_to_hlsfrom RGB coordinates to HLS coordinates0 to 1rgb_to_hsvfrom RGB coordinates to HSV coordinates0 to 1yiq_to_rgbfrom YIQ coordinates to RGB coordinates-1 to 1hls_to_rgbfrom ... Read More

Advertisements