Showing posts from May, 2020

Collections — Container Datatypes - Python

Collection Module in Python This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.NameDescriptionnamedtuple()factory function for creating tuple subclasses with named fieldsdeque()list-like container with fast appends and pops on either endChainMap()dict-like class for creating a single view of multiple mappingsCounter()dict subclass for counting hashable objectsOrderedDict()dict subclass that remembers the order entries were addeddefaultDict()dict subclass that calls a factory function to supply missing valuesUserDict()wrapper around dictionary objects for easier dict subclassingUserList()wrapper around list objects for easier list subclassingUserString()wrapper around string objects for easier string subclassing EXAMPLE               namedTuple()The python namedtuple() function returns a tuple-like object with names for each position in the tuple. It was used to eliminate the problem of…

with statement in Python

with block in Python The with statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I'll discuss the statement as it will commonly be used. In the next section, I'll examine the implementation details and show how to write objects for use with this statement. EXPLANATIONThe expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods.

The object's __enter__() is called before with-block is executed and therefore can run set-up code. It also may return a value that is bound to the name variable, if given. (Note carefully that variable is not assigned the result of expression.)

After execution of the with-block is finished, the object's __exit__() method is called, even if the block raised an exception, and can therefore run clean-up code. SAMPLE PROGRAM """File Handling"""
file = …

Polymorphism in Python

Polymorphism is one of the main pillars of OOPs.Polymorphism means having multiple or many forms.It means Same Function name performing Different Actions

Ways of Achieving Polymorphism
We can Achieve Polymorphism in Python by for ways:- Duck TypingOperator Overloading Method OverloadingMethod Overriding
Duck Typing Duck Typingis a Type System of theDynamically Typed Programming Languages.Programming Languages Like Python,Ruby,Perl and Ruby are some of the popular examples ofDynamically Typed Language.

Using Duck Typing, we do not check types at all. Instead, we check for the presence of a given method or attribute.There is a popular phrase on Duck Typing.
“If it looks like a duck and quacks like a duck, it’s  a duck” class String:
return 2893
# Driver's code
if __name__ == "__main__":
str1 = String()
Method OverloadingMethod Overloading is a powerful technique in which we can use functions with different actions by having different type of data or di…

Working with PDF Documents in Python?

First of all We need to understand that what is a PDF Document.It's not a new term to you I think but may be for someone it's a new topic.The PDF acronym stands for Portable Document Format.It is simply a format for Sharing Contents Digitally.Now how can we work with PDF files in Python.

Since Python is a very versatile Programming Language and to deal with PDF Documents it provides a Module called PyPDF2.This Module is purely written in Python Programming Language.

Installing PyPDF2 ModuleC:\Users\Your Name>pip install pypdf2
Extracting MetadataPDF Metadata is basically the data that provides more information about a certain PDF file. PDF metadata often includes information like - creation date, author, capacity and application that created the files. EXAMPLE
from PyPDF2 import PdfFileReader
def pdfMeta(name):
withopen(name, 'rb') as file:
pdf = PdfFileReader(file)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
print("Written By: \t",

Command Line Arguments in Python?

Command Line Arguments in PythonThe arguments given to a program in the Command Line(cmd or Terminal) of the Operating System are called Command Line Arguments. What are ways by which we can use Command Line Arguments in a Python Program??sys.argvThe list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string. To loop over the standard input, or the list of files given on the command line, see the fileinput module. getopt.getopt(args, shortopts, longopts=[])Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means sys.argv[1:]. shortopts is the string of option letters that the sc…

What is Recursion in Python?

Recursion VS Iteration A program is called Recursive when an entity calls iteself and A program is called Iterative , the programs contains Loops...While dicussing recursion,a basic question comes in the mind that which is better ? -Recursion or Iteration.The answer to this question depends upon what we are trying to do...Below I have listed some properties of Recursion and Iteration.
RecursionA Recursive call terminates when the base case is reached.A Recursive Program requires extra memory that an Iterative Program.Some Problems like finding the factorial of a number can be easily solved by using Recursion.Recursion has Smaller Sizes of Code i.e. less lines of code.In Recursion,the time complexity is very high.IterationAn Iteration stops when the specified condition is proven to be false.It does not require any extra space like in Recursion.Some problems are not obvious as of a Recursive Solution.Size of Code is larger in comparison to Recursion.Time Complexity is relatively lower.Fro…

Popular posts from this blog

5 Best Java frameworks

Top 20 Awesome Projects in Python