- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10805 Articles for Python
485 Views
The atexit module in the standard distribution of Python has two functions – register() and unregister(). Both functions take some existing function as an argument. Registered functions are executed automatically when the interpreter session is terminated normally.If more than one functions are registered, their execution is in the reverse order of registration. It means, functions f1(), f2() and f3() are registered one after other, their order of execution will be f3(), f2() and f1().The unregister() function removes the specified function from list of functions to be automatically invoked.Following code shows how a function is registered for automatic execution upon termination ... Read More
3K+ Views
Many computer applications need random number to be generated. However, none of them generate a truly random number. Python, like any other programming technique, uses a pseudo-random generator. Python’s random generation is based upon Mersenne Twister algorithm that produces 53-bit precision floats. The technique is fast and thread-safe but not suitable from cryptographic purpose.Python’s standard library contains random module which defines various functions for handling randomization.random.seed() − This function initializes the random number generator. When random module is imported, the generator is initialized with the help of system time. To reseed the generator, use any int, str, byte or bytearray ... Read More
1K+ Views
The pprint module (lib/pprint.py) is a part of Python’s standard library which is distributed along with standard Python distribution. The name pprint stands for pretty printer. The pprint module’s functionality enables aesthetically good looking appearance of Python data structures. Any data structure that can be correctly parsed by Python interpreter is elegantly formatted. The formatted expression is kept in one line as far as possible, but breaks into multiple lines if the length exceeds the width parameter of formatting. One unique feature of pprint output is that the dictionaries are automatically sorted before the display representation is formatted.The pprint module ... Read More
773 Views
The term object serialization refers to process of converting state of an object into byte stream. Once created, this byte stream can further be stored in a file or transmitted via sockets etc. On the other hand reconstructing the object from the byte stream is called deserialization.Python’s terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module available in Python’s standard library provides functions for serialization (dump() and dumps()) and deserialization (load() and loads()).The pickle module uses very Python specific data format. Hence, programs not written in Python may not be able to deserialize the encoded (pickled) ... Read More
253 Views
In programming, operator is generally a symbol (key) predefined to perform a certain operation such as addition, subtraction, comparison etc. Python has a large set of built-in operations divided in different categories such as arithmetic, comparison, bit-wise, membership etc.The operator module in python library consists of functions corresponding to built-in operators. Names of the functions are analogous to type of corresponding operator. For example, add() function in operator module corresponds to + operator.Python’s Object class has dunder (double underscore before and after name) methods corresponding to operator symbols. These dunder methods can be suitably overloaded in user defined classes to ... Read More
805 Views
Even though marshal module in Python’s standard library provides object serialization features (similar to pickle module), it is not really useful for general purpose data persistence or transmission of Python objects through sockets etc. This module is mostly used by Python itself to support read/write operations on compiled versions of Python modules (.pyc files). The data format used by the marshal module is not compatible across Python versions (not even subversions). That’s why a compiled Python script (.pyc file) of one version most probably won’t execute on another. The marshal module is thus used for Python’s internal object serialization.Just as ... Read More
176 Views
As in most programming languages Python provides while and for statements to form a looping construct. The for statement is especially useful to traverse the iterables like list, tuple or string. More efficient and fast iteration tools are defined in itertools module of Python’s standard library. These iterator building blocks are Pythonic implementations of similar tools in functional programming languages such as Haskell and SML.Functions in itertools module are of three types.Infinite iteratorsFinite iteratorsCombinatoric iteratorsFollowing functions generate infinite sequences.count() − This function returns an iterator of evenly spaced values from start value. The function can have optional step value to ... Read More
1K+ Views
Floating point numbers are represented in the memory as a base 2 binary fraction. As a result floating point arithmetic operations can be weird at times. Addition of 0.1 and 0.2 can give annoying result as follows −>>> 0.1 + 0.2 0.30000000000000004In fact this is the nature of binary floating point representation. This is prevalent in any programming language. Python provides a decimal module to perform fast and correctly rounded floating-point arithmetic.The decimal module is designed to represent floating points exactly as one would like them to behave, and arithmetic operation results are consistent with expectations. The precision level of ... Read More
765 Views
Very often we need to pass arguments to Python script when executing from command line. However, the script raises exception when parameters needed are not provided in equal number or type or order. That’s where the need to properly parse the command line argument occurs.The argparse module provides tools for writing very easy to use command line interfaces. It handles how to parse the arguments collected in sys.argv list, automatically generate help and issues error message when invalid options are given.First step to desing the command line interface is to set up parser object. This is done by ArgumentParser() function ... Read More
328 Views
The cmd module contains only one class called Cmd. This is used as base class for a user defined framework for line oriented command line interpreters.CmdAn object of this class or its subclass provides the line oriented interpreter framework. Important methods of this class inherited by the subclass are listed below.cmdloop()This method sends the object in a loop, accepts inputs and sends the same to appropriate command handler method in the class.As the loop starts an introductory message (give as parameter to cmdloop() method) will be displayed with a default (cmd) prompt which may be customized by prompt attribute.The interpreter ... Read More