Kiran P has Published 125 Answers

How to make the argument optional in Python

Kiran P

Kiran P

Updated on 10-Nov-2020 05:32:52

Introduction..Sometimes, the programs require optional arguments when supplied will use them else go back to default declarations. We will see in this example on how to use them.The parameters that start with dashes (--) are identified as optional, so they can be left out, and they may have default values.Parameters ... Read More

How to combine multiple graphs in Python

Kiran P

Kiran P

Updated on 10-Nov-2020 05:28:29

IntroductionMatplotlib allows to add more than one plot in the same graph. In this tutorial, I will show you how to present data in the same plot, on two different axes.How to do it..1. Install matplotlib by opening up the python command prompt and firing pip install matplotlib.import matplotlib.pyplot as ... Read More

How to match text at the start or end of a string in Python?

Kiran P

Kiran P

Updated on 10-Nov-2020 05:24:29

Problem..Assume you need to check the start or end of a string for a specific text patterns. The common patterns might be filename extensions but can also be anything. I will show you few methods on how you can do this.Startswith() methodA simple way to check the beginning of a ... Read More

How to Search and Replace text in Python?

Kiran P

Kiran P

Updated on 10-Nov-2020 05:22:42

ProblemYou want to search for and replace a text pattern in a string.If we have a very simple literal patterns, using the str.replace() method is an optimal solution.Exampledef sample(): yield 'Is' yield 'USA' yield 'Colder' yield 'Than' yield 'Canada?' text = ' '.join(sample()) print(f"Output \n {text}")OutputIs USA Colder Than ... Read More

How to Implement Priority Queue in Python?

Kiran P

Kiran P

Updated on 10-Nov-2020 05:20:13

Introduction...The queue module provides a first-in, first-out (FIFO), Last-in, First out (LIFO) data structure suitable for multi-threaded programming. Queues can be used to pass data or any wide range of information e.g. session details, paths, variables, .. between creator and consumer threads safely. Locking is generally handled for the caller.Note ... Read More

How to Find The Largest Or Smallest Items in Python?

Kiran P

Kiran P

Updated on 10-Nov-2020 05:10:16

This article is aimed at developers who want to find the largest or smallest items with Python. I will show a few methods touse and will conclude the best method for you.Method – 1: Slice approach on a ListIf you are simply trying to find the single smallest or largest ... Read More

How to Identify Most Frequently Occurring Items in a Sequence with Python?

Kiran P

Kiran P

Updated on 10-Nov-2020 05:03:25

ProblemYou need to identify the most frequently occurring items in a sequence.SolutionWe can use counter to keep track of the items in a sequence.What is a Counter ?The “Counter” is a mapping that holds an integer count for each key. Updating an existing key adds to its count. This Objectis ... Read More

How to improve file reading performance in Python with MMAP function?

Kiran P

Kiran P

Updated on 09-Nov-2020 11:02:12

Introduction..MMAP abbreviated as memory mapping when mapped to a file uses the operating systems virtual memory to access the data on the file system directly, instead of accessing the data with the normal I/O functions. There by improving the I/O performance as it does not require either making a separate ... Read More

How to read text files using LINECACHE in Python

Kiran P

Kiran P

Updated on 09-Nov-2020 10:59:40

Solution..The linecache module implements cache which holds the contents of files, parsed into separate lines, in memory. linecache module returns line/s by indexing into a list, and saves time over repeatedly reading the file and parsing lines to find the one desired.lincecache module is very useful when looking for multiple ... Read More

How to compare files in Python

Kiran P

Kiran P

Updated on 09-Nov-2020 10:57:07

Problem.You need to compare files in Python.Solution..The filecmp module in python can be used to compare files and directories. 1.cmp(file1, file2[, shallow])filecmp Compares the files file1 and file2 and returns True if identical, False if not. By default, files that have identical attributes as returned by os.stat() are considered to ... Read More

Advertisements