Found 10400 Articles for Python

Python Functions creating iterators for efficient looping

Nikitasha Shrivastava
Updated on 10-Jun-2025 19:01:35

242 Views

In this article, we will talk about creating iterators for efficient looping in Python functions. An iterator is something that helps you go through all the items one by one, like going through pages of a book one page at a time. In Python, we can use lists, tuples, dictionaries, and sets as iterators. So we will explore Python functions that generate iterators for efficient looping. Creating Iterators with Functions Python provides different ways to create iterators using functions. So let us see some examples of creating iterators for efficient looping in Python functions - Using Generator Functions Using ... Read More

Decimal fixed point and floating point arithmetic in Python

Nikitasha Shrivastava
Updated on 10-Jun-2025 19:07:53

1K+ Views

This article will explain how decimal fixed-point and floating-point arithmetic work in Python, which is useful for performing accurate calculations in various applications. Numbers in Python can be stored in two ways: floating-point and decimal fixed-point. Floating-point numbers are fast but can sometimes be incorrect because of how computers store them. On the other hand, Decimal fixed-point numbers are more accurate and useful when working with precise calculations.  By the end of this article, you will understand how both types of numbers work, when to use them, and how to write programs using them. Floating-Point Arithmetic The following example will ... Read More

Random access to text lines in Python (linecache)

Chandu yadav
Updated on 25-Jun-2020 13:36:20

1K+ Views

Purpose of linecache module in Python’s standard library is to facilitate random access to any text file, although this module is extensively used by Python’s traceback module to generate error trace stack. Further prettyprints of reading are held in a cache so that it saves time while reading lines repeatedly.The most important function in this module is getline() which reads a specified line number from given file. Following is the list of functions −getline(file, x)This function returns xth line from file. If not present it will return empty string. If the file is not present in current path, function ties ... Read More

Iterate over lines from multiple input streams in Python

Ankith Reddy
Updated on 25-Jun-2020 13:43:08

650 Views

Python’s built-in open() function opens one file in read/write mode and read/write operations on it. To perform processing on multiple files in a batch, one has to use fileinput module of Python’s standard library. This module provides a Fileinput class with functionality of iterating over files. The module also defines helper functions for the same purpose.Primary interface to this module is input() function. This function returns instance of Fileinput class.fileinput.input(files, inplace, mode)The files parameter is name of one or more files to be read one by one. Each file acts as a generator and using a for loop it can ... Read More

Object-oriented filesystem paths in Python (pathlib)

Arjun Thakur
Updated on 25-Jun-2020 13:47:38

864 Views

The pathlib module provides an object oriented approach to handling filesystem paths. The module also provides functionality appropriate for various operating systems. Classes defined in this module are of two types – pure path types and concrete path types. While pure paths can only perform purely computational operations, concrete paths are capable of doing I/O operations too.pathlib module defines following classes −Sr.No.Module & Description1PurePathThe base class for all other classes2Pathsubclassed from PurePath. This is a concrete class that represents filesystem path.3PosixPathPath subclass for non-Windows OS4WindowsPathPath subclass for Windows systems5PurePosixPathPurePath subclass for non-Windows systems6PureWindowsPathPurePath subclass for Windows systemsWhen instance of Path ... Read More

Python object persistence (shelve)

George John
Updated on 22-Jan-2025 13:49:50

5K+ Views

Python Object Persistence  The shelve module in Python's standard library is a simple yet effective tool for persistent data storage when using a relational database solution is not required. The shelf object defined in this module is a dictionary-like object persistently stored in a disk file. This creates a file similar to dbm database on UNIX-like systems. The only string data type can be used as a key in this special dictionary object, whereas any pickable object can serve as value. The shelve module defines three classes as follows − ... Read More

How to install Python MySQLdb module using pip?

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

3K+ Views

To install python MySQLdb module, we need to install Python current version i.e. 3.7 We need to find the location of Python Scripts where pip command is located. First, open the cmd and reach the location of Python Scripts. To open cmd, press “Windows+R” and type cmd. Here is the snapshot − Now reach where scripts are located. We will now install “MySQLdb” module. The steps are displayed in the following screenshot.

Keywords in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

5K+ Views

Like other languages, Python also has some reserved words. These words hold some special meaning. Sometimes it may be a command, or a parameter etc. We cannot use keywords as variable names. The Python Keywords are True False class def return if elif else try except raise finally for in is not from import global lambda nonlocal pass while break continue and with as yield del or assert None The True & False Keywords The True and False are the truth ... Read More

Precision Handling in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Python can handle the precision of floating point numbers using different functions. Most functions for precision handling are defined in the math module. So to use them, at first we have to import the math module, into the current namespace. import math Now we will see some of the functions for precision handling. The trunc() function The trunc() method is used to remove all fractional part from a floating point number. So it returns only the integer part from the number. The ceil() function The ceil() method is used to return the Ceiling value of a ... Read More

Formatted text in Linux Terminal using Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

302 Views

In this section, we will see how to print formatted texts in Linux terminal. By formatting, we can change the text color, style, and some special features. Linux terminal supports some ANSI escape sequences to control the formatting, color and other features. So we have to embed some bytes with the text. So when the terminal is trying to interpret them, those formatting will be effective. The general syntax of ANSI escape sequence is like below − \x1b[A;B;C A is the Text Formatting Style B is the Text Color or Foreground Color C is the Background ... Read More

Advertisements